-2

How can I make this stop exceeding the actual amount of values preventing the crash?

    String[][] firstLine = new String[8][10];
    Scanner input = new Scanner(new File("lab1.dat"));
    String temp2 = " ";


    for(int i = 0; i < firstLine.length; i++){
        for(int j = 0; j < firstLine[i].length; j++){
            firstLine[i][j]  = input.nextLine();
            System.out.println(firstLine[i][j] );

1000 1011 1006 1211 1854 1799 10 34 18 96 45
10 R W X O A N O N N N X
34 N A W X X R N O N R N
18 W N N A X N N N O N N
96 W W W R R X N R N O W
45 R W N O N N N N X R O
10 1006 
34 1000
34 34
96 1211
96 1854
45 18
34 1211
-1 -1
Martin G
  • 17,357
  • 9
  • 82
  • 98
  • 2
    what did cause the crash(exception)? please provide a minimal running example of your problem. – SomeJavaGuy Sep 10 '15 at 06:04
  • do you want read 8 lines form file! or in which way you want to read. provide information? – Yash Sep 10 '15 at 06:06
  • Make sure your file has more than 8 lines – M. Reif Sep 10 '15 at 06:07
  • @KevinMoore simply edit the question – SomeJavaGuy Sep 10 '15 at 06:12
  • @KevinMoore You can add the data as part of the question itself, so that you can format your data part with code snippet option. – bprasanna Sep 10 '15 at 06:12
  • This is the data set @Yash 1000 1011 1006 1211 1854 1799 10 34 18 96 45 10 R W X O A N O N N N X 34 N A W X X R N O N R N 18 W N N A X N N N O N N 96 W W W R R X N R N O W 45 R W N O N N N N X R O 10 1006 34 1000 34 34 96 1211 96 1854 45 18 34 1211 -1 -1 There is 0-10 for row & 0-13 for column. I used values appropriate for those 11 & 12 but it wouldn't work.. I think due to empty places – Kevin Moore Sep 10 '15 at 06:14
  • firstLine is an `Array`, your loop will ask for `8 x 10 = 80` rows, which you don't have. Are you trying to store all the values from the file or an exact amount? Is the bi-dimensional array required for some reason? What is the expected output? – Alexandru Severin Sep 10 '15 at 06:22
  • Please add the full stack trace and use a better title. See http://stackoverflow.com/a/3132349/5072526 how to get detailed information instead of "Unknown Source". – hinneLinks Sep 10 '15 at 06:37

3 Answers3

1

Try ArrayList

Scanner sc = new Scanner(new FileInputStream(new File("Input.txt")));
        ArrayList<List<String>> filedata = new ArrayList<List<String>>();

        while (sc.hasNextLine()) {
            ArrayList<String> row = new ArrayList<String>();
            String[] col = sc.nextLine().split("d*[\\s+]");
            for (int i = 0; i < col.length; i++) {
                row.add(col[i]);
            }
            filedata.add(row);
        }
        System.out.println("File Data : "+filedata);
Yash
  • 9,250
  • 2
  • 69
  • 74
0

look at Scanner method hasNextLine() and try to use it in your code. Maybe like this:

  String[][] firstLine = new String[8][10];
   Scanner input = new Scanner(new File("lab1.dat"));
    String temp2 = " ";

for(int i = 0; i < firstLine.length; i++){
    for(int j = 0; j < firstLine[i].length; j++){
       if(input.hasNextLine()){
        firstLine[i][j]  = input.nextLine();
        System.out.println(firstLine[i][j] );
       }

Or you can try-catch that nextLine() method and handle NoSuchElementException in catch block.

edasssus
  • 331
  • 4
  • 15
  • tip: add an `else` clause to that `if` and run `break;` on it, to make the loops stop. However, I don't think this is the output expected by the OP. – Alexandru Severin Sep 10 '15 at 06:25
0

I think you have to use input.hasNextLine() while calling nextLine you should check if scanner has one.

       try {
            String[][] firstLine = new String[8][10];
            Scanner input = new Scanner(new File("lab1.dat"));
            int count  = 0 ;
            //80 is the number of lines you want to read
             while(input.hasNextLine() && count < 80){ 
                 firstLine[count%8][count%10] = input.nextLine();
                 System.out.println( firstLine[count%8][count%10]);
                 count++;

             }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
karim mohsen
  • 2,164
  • 1
  • 15
  • 19