使用split将字符串拆分为整数值

时间:2013-07-19 05:16:48

标签: java string split

代码编译得很好所以我认为我这样做是正确的,但是当我调用toString时,我从no-argume构造函数中得到了一堆null和零值,当我调用方法totalScoresOfHomeworks看看它有什么价值时会回来,我得到错误

"FException in thread "main" java.lang.NullPointerException
at GradeApplication.getTotalScoresOfHomeworks(Student.java:119)
at Student.main(Student.java:55)"

但这不是编译错误 我如何使用“分裂”

可能有或没有错

这是我的代码:

import java.util.Scanner; 
import java.io.*;

public class Student
{
    public static void main(String[] args) throws IOException 
    {
        Scanner keyboard = new Scanner(System.in);

        //Define variables
        String name;
        int id;
        String homework;
        String labs;
        String tests;
        //int[] quizzesArray= new int[3];//**NO QUIZ GRADES
        double[] homeworkArray=new double[10];
        double[] labsArray= new double[6];
        double[] testsArray=new double[3];
        double project;
        double discussion;

        System.out.print("What is your name? ");
        name=keyboard.nextLine();

        System.out.print("\nWhat is your student ID? ");
        id=keyboard.nextInt();

        //Create strings to tokenize homework grades
        homework = keyboard.nextLine();
        System.out.println("\nPlease enter homework grades separated by spaces:");
        homework = keyboard.nextLine();
        //creat object to pass all info to class

        System.out.println("Please enter lab grades separated by spaces:");
        labs = keyboard.nextLine();

        System.out.println("Please enter test grades separated by spaces:");
        tests = keyboard.nextLine();

        System.out.println("Please enter project grade:");
        project = keyboard.nextDouble();

        System.out.println("Please enter discussion grade:");
        discussion = keyboard.nextDouble();

        System.out.println("\nResults: ");
        //Call toString method
        GradeApplication ga = new GradeApplication(name,id,homework,labs,tests,project,discussion);
        System.out.print(ga.toString());    

        System.out.print(ga.getTotalScoresOfHomeworks());
    }
}

class GradeApplication
{

    //Define variables
    String name;
    int id;
    String homework;
    String labs;
    String tests;
    double project;
    double discussion;

    //No argument constructor
    public GradeApplication()
    {
        name="";
        id=0;
        homework="";
        labs="";
        tests="";
        project=0.0;
        discussion=0.0;

    }

    //Parameter constructor
    public GradeApplication(String nme, int ID, String hw, String lbs, String tsts, double proj, double disc)
    {
        String name=nme;
        int id=ID;

        String homework=hw; 
        String labs=lbs;    
        String tests=tsts;

        double project=proj;
        double discussion=disc;
    }

    //Attributes
    private double totalScoresOfHomeworks;
    private double totalScoresOfLabs;
    private double totalScoresOfTests;
    private double scoreOfProject;
    private double scoreOfDiscussion;

    //Methods
    public double getTotalScoresOfHomeworks()
    {
        double[] homeworkArray=new double[10];
        double sum1=0;
        String[] tokens1;
        tokens1 = homework.split(" ");
                        for (int i=0;i<9; i++)
                        {
                            homeworkArray[i]=Double.parseDouble(tokens1[i]);
                            sum1+=homeworkArray[i];
                        }
        return sum1;
    }
    public void setTotalScoresOfHomeworks(double hwk)
    {
        totalScoresOfHomeworks=hwk;
    }

    public double getTotalScoresOfLabs()
    {
        double[] labsArray= new double[6];
        double sum2=0;
        String[] tokens2;
                    tokens2 = labs.split(" ");
                        for (int j=0;j<9; j++)
                            {
                                labsArray[j]=Double.parseDouble(tokens2[j]);    
                            }           
        return sum2;
    }
    public void setTotalScoresOfLabs(double labs)
    {
        totalScoresOfLabs=labs;
    }

    public double getTotalScoresOfTests()
    {
        double[] testsArray=new double[3];
        double sum3=0;
        String[] tokens3;
                {
                    tokens3 =tests.split(" ");
                        for (int k=0;k<9; k++)
                            {
                                testsArray[k]=Double.parseDouble(tokens3[k]);   
                            }                   
        return sum3;
    }
    public void setTotalScoresOfTests(double tests)
    {
        totalScoresOfTests=tests;
    }

    public double getScoreOfProject()
    {
        return project;
    }
    public void setScoreOfProject(double project)
    {
        scoreOfProject=project;
    }

    public double getscoresOfDiscussion()
    {
        return discussion;
    }
    public void setScoresOfDiscussion(double disc)
    {
        scoreOfDiscussion=disc;
    }

    public double getTotalScores()
    {
        return (totalScoresOfHomeworks+totalScoresOfLabs+totalScoresOfTests+scoreOfProject+scoreOfDiscussion);
    }

    public double getPercentage()
    {
        return 100*getTotalScores()/680;
    }

    public char getGrade()
    {
        char grade=0;
            if (getPercentage() >90 )
                grade='A';
            else if (getPercentage()>80)
                grade='B';
            else if (getPercentage()>70)
                grade='C';
            else if (getPercentage()>60)
                grade='D';
            else if (getPercentage() <= 60)
                grade='F';
        return grade;
    }

    public String toString()
    {
        String str="Student name:\t"+ name +
                        "\nStudent ID:\t" + id +
                        "\nTotal scores:\t" +getTotalScores()+
                        "\nMax scores:\t" + 680 +
                        "\nPercentage:\t"+ getPercentage() +
                        "\nGrade:\t" + getGrade();
        return str;
    }
}

4 个答案:

答案 0 :(得分:2)

在构造函数中,您不需要再次声明变量

public GradeApplication(String nme, int ID, String hw, String lbs, String tsts, double proj, double disc)
    {
        name=nme;
        id=ID;

        homework=hw; 
        labs=lbs;    
        tests=tsts;

        project=proj;
        discussion=disc;
    }

答案 1 :(得分:1)

在参数化构造函数中,您将参数分配给局部变量。所有类变量都不是由此设置的。因此抛出空指针异常。尝试将参数分配给实际的类变量。像

public GradeApplication(String nme, int ID, String hw, String lbs, String tsts, double proj, double disc) {
    name = nme;
    id = ID;

    homework = hw;
    labs = lbs;
    tests = tsts;

    project = proj;
    discussion = disc;
} 

答案 2 :(得分:0)

这里的homework变量没有得到任何值,所以它不能拆分任何值。它是null。因此,您在此方法tokens1 = homework.split(" ");中的public double getTotalScoresOfHomeworks()处获得NullPointerException。

答案 3 :(得分:0)

问题在于此代码:

public double getTotalScoresOfHomeworks() {
        double[] homeworkArray = new double[10];
        double sum1 = 0;
        String[] tokens1;
        **tokens1 = homework.split(" ");**
        for (int i = 0; i < 9; i++) {
            homeworkArray[i] = Double.parseDouble(tokens1[i]);
            sum1 += homeworkArray[i];
        }
        return sum1;
    }

homework变量为null,您试图将其拆分。所以它产生了错误。

第二个问题是

public GradeApplication(String nme, int ID, String hw, String lbs,
            String tsts, double proj, double disc) {
        String name = nme;
        int id = ID;

        String homework = hw;
        String labs = lbs;
        String tests = tsts;

        double project = proj;
        double discussion = disc;
    }

你再次声明这些变量,但在代码中你使用的是全局变量,所以都是null。从中删除声明部分。

一旦你解决了这个问题,你将再次遇到问题,

public double getTotalScoresOfHomeworks() {
        double[] homeworkArray = new double[10];
        double sum1 = 0;
        String[] tokens1;
        tokens1 = homework.split(" ");
        for (int i = 0; i < 9; i++) {
            homeworkArray[i] = Double.parseDouble(tokens1[i]);
            sum1 += homeworkArray[i];
        }
        return sum1;
    }

token1是字符串数组,你试图用double解析它。所以它会导致运行时异常。

线程“main”中的异常java.lang.NumberFormatException:对于输入字符串:“eng”     at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)     在java.lang.Double.parseDouble(未知来源)     在GradeApplication.getTotalScoresOfHomeworks(Student.java:107)     在Student.main(Student.java:52)

彻底检查您的代码。