-2

I've looped an array so firstly it grabs from inputted data full name and splits it to lname and fname, then with a loop inside that loop it sums scores you input.

When you end the second loop the first loop then asks for String.split (in my case fullname) again, but it doesn't allow me to input a new data to split instead gives the error "java.lang.ArrayIndexOutOfBoundsException: 1" and highlights "String fname = parts[1];

Any ideas, anything to help me get on the right track would be appreciated!

I've also tried

string = "";

string = null;

Here is my Code so far:

import java.util.Scanner;


public class TestScores
{
   public static void main(String[] args) {
       Scanner kb = new Scanner (System.in);

       String fullname;
       int sum = 0;
       int count = 0;
       int score;


       System.out.println("Please enter name here e.g lastname, firstname");
       fullname = kb.nextLine( );
       while (fullname.equals ("Done") !=true){
       String[] parts = fullname.split(",");
       String lname = parts[0];
       String fname = parts[1];

       System.out.println("Enter scores (Type -1 to finish scoring.");
       score = kb.nextInt();

       while(score != -1)
       {
           sum += score;
           score = kb.nextInt();
           count++;
        }
       System.out.println(fname + " " + lname + "'s total Score is: " + sum);
       System.out.println();



       System.out.println("Please enter another name here e.g lastname, firstname to start scoring"); 
       fullname = kb.nextLine( );

    }

    }
}
tshepang
  • 10,772
  • 21
  • 84
  • 127
SamsinOzo
  • 35
  • 1
  • 1
  • 7

3 Answers3

1

Solution:

System.out.println("Please enter another name here e.g lastname, firstname to start scoring"); 
kb.nextLine();  //add this line to fix your problem.
fullname = kb.nextLine( );

Explanation:

You are doing Scanner.nextInt() and this does not read the last newline character. The line fullname = kb.nextLine( ); consume this newline character and then you only got a newline in fullname. Then the split returns an array of size one and so you get an ArrayIndexOutOfBoundsException in this line String fname = parts[1];. You only have to add one nextLine() to consume the the newline character first, before waiting for input.

Take a look at this answer: Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods

Community
  • 1
  • 1
kai
  • 6,219
  • 16
  • 37
  • I'd like to thank everyone who has replied it all helped, adding the kb.nextLine(); worked like a charm! – SamsinOzo Mar 19 '14 at 11:12
  • You can accept any answer to show that the problem is solved. – kai Mar 19 '14 at 11:15
  • Done! Thanks again, also thanks for adding the "Skipping nextLine() after use nextInt()" post, it cleared my understanding up even more :3 – SamsinOzo Mar 19 '14 at 11:19
0

First of all test weather your split result are there and try to initialize fullname variable

String fullname = "";

String[] parts = fullname.split(",");
if(parts.length==2){
       String lname = parts[0];
       String fname = parts[1];
}
RMachnik
  • 3,444
  • 1
  • 29
  • 47
0

The reason for the error is that parts is not long enough to have a parts[1].

The reason parts is so short is that fullName.split(",") returned a one-element array.

The reason split() returned a one-element array is that fullname did not contain a comma.

The reason fullname did not contain a comma is that a line in the file did not contain a comma.

What you do to fix this is up to you. You could specify that your program is liable to fail on invalid input. You could detect short arrays and skip that line. You could detect short arrays and insert a blank/null surname or forename. It all depends on the requirements you've been given (or have invented).

slim
  • 36,139
  • 10
  • 83
  • 117