Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 2nd, 2012  |  syntax: None  |  size: 1.94 KB  |  hits: 28  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Trouble with while loop and storing data
  2. String fileName = "Data.txt";
  3. int [] fill = new int [6];
  4. try{
  5.   Scanner fileScan = new Scanner(new File(fileName));
  6.   int i = 0;
  7.   while (fileScan.hasNextInt()){    
  8.     Scanner line = new Scanner(fileScan.nextLine());
  9.     i++;
  10.     line.next();
  11.     fill[i] = line.nextInt();
  12.     System.out.println(fileScan.nextInt());
  13.   }
  14. }catch (FileNotFoundException e){
  15.     System.out.println("File not found. Check file name and location.");
  16.     System.exit(1);
  17.   }
  18. }
  19.        
  20. > run FileApp
  21. 0
  22. 0
  23. 1
  24. java.lang.ArrayIndexOutOfBoundsException: 4
  25.     at FilePanel.<init>(FilePanel.java:35)
  26.     at FileApp.main(FileApp.java:14)
  27.     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  28.     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  29.     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  30.     at java.lang.reflect.Method.invoke(Unknown Source)
  31.     at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)
  32.  
  33. >
  34.        
  35. 1 1 20 30 40 40
  36. 0 2 80 80 50 50
  37. 0 3 150 200 10 80
  38. 1 1 100 100 10 10
  39.        
  40. String fileName = "Data.txt";
  41.         int[] fill = new int[12];
  42.         try
  43.             {
  44.                 Scanner fileScan = new Scanner(new File(fileName));
  45.                 int i = 0;
  46.                 while(fileScan.hasNextInt())
  47.                     {
  48.                         Scanner line = new Scanner(fileScan.nextLine());
  49.                         // System.out.println(fileScan);
  50.                         // System.out.println(line);
  51.  
  52.                         line.next();
  53.                         fill[i] = line.nextInt();
  54.                         System.out.println("fill" + fill[i]);
  55.                         // System.out.println(fileScan.nextInt());
  56.  
  57.                         i++;
  58.                     }
  59.             }
  60.         catch (FileNotFoundException e)
  61.             {
  62.                 System.out.println("File not found. Check file name and location.");
  63.                 System.exit(1);
  64.             }