0

I'm trying to count up the values stored in an array, here is my code:

    public void printScores()
{

    String sep = ":";
    File inputfile = new File ("P:/SD/Assignment1/fbScores.txt");

        String [] stringArr;
        String line = "";



        try {
            Scanner filescan = new Scanner(inputfile);
            while(filescan.hasNext())
            {
                line = filescan.nextLine();


                stringArr = line.split(sep);
                if(stringArr.length ==  4)
                {

                    System.out.println(stringArr[0]+"\t [" +stringArr[2]+"]\t|" + stringArr[1]+"\t["+ stringArr[3]+" ]\n");

                }

                else
                {
                    throw new IllegalArgumentException("String " + line + " does not contain " + sep);
                }



             }
            filescan.close();


        }
            catch (FileNotFoundException e)
            {
                System.out.println("problem " +e.getMessage());
            }

        }
    public void totalGoals()
    {
        int count;
        for (int i = 0; i<stringArr.length; i++)
        {
            //int num[i] = Integer.parseInt(stringArr);

        }
    }

}

Basically I only want to add the numbers up that are stored in [2] and [3], my totalGoals method at the bottom is where I started, but can't figure out how to change from a string to an integer, any help would be much appreciated!

UPDATE:

public void totalGoals() {

        int[] num = new int[stringArr.length]; 
        int count = 0;
        for (int i = 0; i<stringArr.length; i++)
        {
            num[i] = Integer.parseInt(stringArr[i]);
            count = count + num[i];
            System.out.println(count);
        }
    }

3 Answers 3

1

You want to parse each string individually - you seem to be trying to do the whole array

int[] num = new int[stringArr.length]; //don't forget to declare your num[] array

for (int i = 0; i<stringArr.length; i++)
{
    num[i] = Integer.parseInt(stringArr[i]); // stringArr[i] instead of stringArr
}

Note what this code is actually doing when you break it out of the for loop format.

int[] num = new int[4];
num[0] = Integer.parseInt(stringArr[0]); //the for loop is starting at 0 and stopping 
num[1] = Integer.parseInt(stringArr[1]); //when it hits 4
num[2] = Integer.parseInt(stringArr[2]);
num[3] = Integer.parseInt(stringArr[3]);

In truth, you only need 2 and 3.. and all you need to do is add them together. You already have a count variable you could use instead of num

So lets say you only want to loop through 2 and 3... look at the for loop

for (int i = 0; i < stringArr.length; i++)

int i = 0             //your loop is starting at 0
i < stringArr.length; //its ending when i is the array's length (should be 4 judging by your other code)
i++                   // i increases by 1 at the end of each time through

So if 0 and 1 are useless, try using int i = 2

You have a count variable. Maybe it should start at 0.. and rather than setting the num variable, you could add the value to your count variable.


About your method.. By itself it should be working.

I'm also not sure which line is line 73

If it is the first line: int[] num = new int[stringArr.length]; Then either your stringArr doesn't exist (for this method) or your stringArr isn't initialized (again, for this method)

Looking at the code you've shown me, I'm guessing the problem is on that first line.

public void printScores() {
    //other code

    String[] stringArr;

    //other code
}

public void totalGoals() {
    int[] num = new int[stringArr.length]; 
    int count = 0;
    for (int i = 0; i<stringArr.length; i++)
    {
        num[i] = Integer.parseInt(stringArr[i]);
        count = count + num[i];
        System.out.println(count);
    }
}

Done simply like this, you wouldn't even be able to compile. stringArr would not exist for totalGoals. The two methods are separate and cannot "see" each other's variables.

If you have your code like below - then you are declaring stringArr twice and you have two separate variables named the same thing!

{
    String[] stringArr; //This is what totalGoals would be using - it is never assigned

    public void printScores() {
        String[] stringArr; //This one is used and assigned within printScores
                            //but totalGoals cannot see/use it
    }

    public void totalGoals() {

    }
}
Sign up to request clarification or add additional context in comments.

11 Comments

Cool, so this has converted all the elements into an integer, however I can't seem to understand how to get a loop going so it will count up the numbers
You already have the loop going ;) I'll add some details that should help
Thankyou! New to Java so appreciate the help
I've tried adjusting the method like you said, however when I call it in the main method "readScores.totalGoals();" it returns: Exception in thread "main" java.lang.NullPointerException at readFile.totalGoals(readFile.java:73) at main.main(main.java:14)
Updated - tried to make it so the count would add num[i] as it loops round and print it, however this error is returned: Exception in thread "main" java.lang.NullPointerException at readFile.totalGoals(readFile.java:73) sorry if i'm missing something
|
0

Use Integer.parseInt(String s) or Integer.parseInt(String s, int radix) to convert your Strings to Integers or primitive ints.

For instance:

try {
    String s = "42";
    int i = Integer.parseInt(s);
}
catch (NumberFormatException nfe) {
    nfe.printStackTrace();
}

You can also use Scanner.nextInt if that suits your context better.

Comments

0

Use integer.parseint to parse string to int

     if(stringArr.length ==  4)
                        {
        for(int i=0;i<stringArr.length;i++)
{

    int    c = Integer.parseInt(stringArr[2])+ Integer.parseInt(stringArr[3]);


        }
                        }

1 Comment

There are about 15 lines that all have stringArr[2] and [3], I need to put this into a loop to count up [2] and [3] on each line, should I just introduce this into a loop?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.