John
Posts: 1
Nickname: jdkbeginer
Registered: Jul, 2003
reading files
Posted: Jul 12, 2003 7:19 AM
Advertisement
Can anyone please analyze my code and explain what is wrong with it cause the out of bounds error keeps showing and I cant understand what is wrong with my code? Pls. help asap thanks. The code is (I'm using JCreator Pro and Jdk 2.4): import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; public class files { public int[][] arrjobs = new int[100][100]; public int jobtotal; public char cCheck; public int nChar; public String sChar; public files() { loadfile(); } public void loadfile() { int i,j,bCheck; String sTmp; i=0; j=0; JFileChooser fileChooser = new JFileChooser(); int result = fileChooser.showOpenDialog( null ); fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY ); if ( result == JFileChooser.CANCEL_OPTION ) return; File fileName = fileChooser.getSelectedFile(); if ( fileName == null || fileName.getName().equals("") ) JOptionPane.showMessageDialog( null, "Invalid File Name", "File Not Found", JOptionPane.ERROR_MESSAGE ); else { try { RandomAccessFile file = new RandomAccessFile(fileName , "r"); sTmp = ""; sChar = file.readLine(); nChar = Integer.parseInt(sChar); jobtotal = nChar; for(i=0;i<jobtotal;i++) { System.out.println("job num:"+(i+1)); sChar = file.readLine(); System.out.println("length: "+sChar.length()); j=0; bCheck=0; while(j<sChar.length() && bCheck!=1) { while(sChar.charAt(j)!=' ' && sChar.charAt(j)!='\n' && sChar.charAt(j)!='\r' && j<sChar.length()) { sTmp = sTmp + String.valueOf(sChar.charAt(j)); System.out.println("Counter: "+j); j++; } if(!sTmp.equals("")) { arrjobs[j]= Integer.parseInt(sTmp); System.out.println("Array val: "+arrjobs[j]); sTmp = ""; } System.out.println("Counter2: "+j); j++; System.out.println("Counter3: "+j); if(j>=21) bCheck=1; } } System.out.println("Total No. of Jobs: "+jobtotal); JOptionPane.showMessageDialog( null, "Jobs Loaded.", "Load Jobs", JOptionPane.PLAIN_MESSAGE); } catch ( IOException ex ) { JOptionPane.showMessageDialog( null, "File does not exist"+ex , "Invalid File Name", JOptionPane.ERROR_MESSAGE ); } } } public static void main(String args[]) { int i=0,j=0; System.out.println("opening filechooser"); files loader = new files(); } } The sample data I used is: 4 2 3 10 3 8 23 14 35 9 7 1 20 4 16 3 13 6 10 2 3 7 0 15 1 13 12 1 8 2 12 28 8