-3

So i have this slice of code that reads text from a csv file where nums[] goes throughout the file and stores the number of a said student, and grades[][] goes through and stores each of their grades:

 Scanner sc = new Scanner(new FileReader("location.csv"));
    String [][] stuff = new String [10][];
    for(int i = 0; i<10; i++){
    String line = sc.nextLine();
    stuff[i] = line.split.(",");
 }
  int [][] grades = new int [10][10];
  int [] nums = new int [10];

  for(int x = 0; x<10; x++){
     nums[x] = Integer.parseInt(stuff[x][0]);
     System.out.println(nums[x]);
      for(int y = 0; y<11; y++){
      grades[x][y] = Integer.parseInt(stuff[x][y]);
    }
}

The problem is that numbs works wonderfully, but grades cant store any value that is past the first column of data. If I set grades [x][y] = stuff[any number] [0] it will run, but if I try to go past 0 in the rows, I error terminate.

Part of data file:

1, 66, 82, 85, 87, 65, 80, 97, 75, 68, 72

2, 70, 63, 75, 62, 84, 65, 67, 95, 81, 96

3, 100, 98, 73, 78, 69, 75, 97, 66, 61, 90

4, 75, 62, 79, 78, 87, 73, 74, 76, 63, 84

5, 81, 90, 80, 66, 75, 96, 73, 77, 66, 87

Stack Trace: java.lang.NumberFormatException: For input string: " 66"

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)

at java.lang.Integer.parseInt(Integer.java:481)

at java.lang.Integer.parseInt(Integer.java:527)

Fixed Code ( I apologize, I did not copy the code verbatim and included a few extra errors in it)

public static void main(String[] args)throws FileNotFoundException

{

    Scanner sc = new Scanner(new FileReader("location.csv"));
    String [][] values = new String [10][];
    for(int i = 0; i<10; i++){
    String line = sc.nextLine();
    values[i] = line.split(",");
 }
  int [][] grades = new int [10][10];
  int [] nums = new int [10];

   for(int x = 0; x<10; x++){
     nums[x] = Integer.parseInt(values[x][0]);
     System.out.println(nums[x]);
      for(int y = 0; y<10; y++){
      grades[x][y] = Integer.parseInt(values[x][y+1].trim());

    }

}
desidia
  • 3
  • 2

2 Answers2

5

You aren't getting rid of the whitespace between tokens.

Make sure to clear it, preferably with String#trim()

Integer.parseInt(stuff[x][0].trim());

For the record, I'd also look to use better names than stuff.

Obicere
  • 2,999
  • 3
  • 20
  • 31
  • I totally agree with your answer yet the OP has other issues as well – Kick Buttowski Sep 09 '14 at 03:17
  • 1
    That worked brilliantly. I have never learned about trim before. And yeah, stuff is a pretty shotty name I know, this is more of just a rough draft that I will polish as I finally get it working. Thank ya kindly! – desidia Sep 09 '14 at 03:19
  • @BenjaminBanister you got to be kidding ya? this is one issue that your code has – Kick Buttowski Sep 09 '14 at 03:21
  • @KickButtowski aye, my sincerest apologies for that. I did a poor job putting the code on here verbatim. fixed code is in the original question – desidia Sep 09 '14 at 03:29
0

Also, you're going to get an index out of bounds when you fix those other errors:

     for( int y = 0; y < 11; y++ )
        grades[x][y] = Integer.parseInt( stuff[x][y] );

All your array indexes go to 10, which in Java means 0 through 9. y here will go to 10, so boom out of bounds.

Considering the syntax errors pointed out in the comments, please in the future copy and paste the EXACT code you are using. It'll be no good if we are debugging different code than what you are actually using.

markspace
  • 10,621
  • 3
  • 25
  • 39
  • this is one issue. the for loop does not read the file for me either – Kick Buttowski Sep 09 '14 at 03:18
  • 1
    Yeah I switched to a StringReader, which was easier for test code. There might be a problem with his file handling. Hmm, maybe he has to call `Scanner.hasNextLine()`? I did. – markspace Sep 09 '14 at 03:19
  • I do not get it. these people thinks this website is the place for them to have shortcut and someone solve their homework. wowwwwwwww – Kick Buttowski Sep 09 '14 at 03:22