Click here to Skip to main content
15,885,842 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a file created with a PrintWriter class, containing only integers separated by a space, I'd recovered these integers for each line and put in tables.
For example, if I have 3 lines, I must have 3 tables.

[edit]
My problem is when I do:

Java
FileReader fr = new FileReader("path/file.txt");
int[] tab = new int [10];
int i = 0;
int c = fr.read();
while(c != -1){
   if(c != (int)(' '){
      tab[i] = c;
      i++;
   }
   c = fr.read();
}

i have an exception : java.lang.ArrayIndexOutOfBoundsException, and the file doesn't exceed 10 integers

NB : here it's only in case when I have only one single line
[/edit]
Posted
Updated 23-Nov-13 3:40am
v2
Comments
Richard MacCutchan 23-Nov-13 8:33am    
And what exactly is your problem in doing this?
SimpDev 23-Nov-13 8:55am    
Supplementary information moved to question.
Richard MacCutchan 23-Nov-13 9:46am    
Your code has a few problems. Firstly you are not checking for the end of the array bound. Secondly you are taking characters and storing them as integers so all the values will be wrong. You need to create a new array after every 10 characters.
SimpDev 23-Nov-13 10:36am    
thank you for your reply,

why checking the end of the table, I know it does not exceed 10 characters.
The problem of incorrect character can easily be solved, the other problem is the "read()" method, which reads character by character, so if I have a number with two digits, it will take two boxes in the table,it's for this reason that I have the exeption.
baliram bhande 23-Nov-13 10:38am    
ArrayList<integer> list = new ArrayList<integer>();
file = new Scanner(new File("test.txt"));
while(file.hasNext()){
if (file.hasNextInt()) list.add(file.nextInt());
else file.next();
}
Collections.sort(list);
for (Integer i: list) System.out.println(i);


you can try this code i hope u have useful

1 solution

There are a few problems with your code. As has been mentioned in the comments, you're not checking for array bounding and you're reading character by character. You may want to look into the BufferedReader class. It allows you to read an entire line in one go. You also may want to look into the parse() method of the various primitive types. The Number class has a parse method, and thus so do the Double, Integer, Long, Short, etc classes. Last, check out the java.util package. It has a lot of usefully collections such as lists, dictionaries (called Maps), and other things.

Here's some code that demonstrates all of the above to solve the problem you're having.

Java
String line = null;
ArrayList<integer> numbers = new ArrayList<>();
//Try-with-resource, so the stream is automatically closed when we're done
try(BufferedReader reader = new BufferedReader(new FileReader("someFilePath"))){
    //While the last line we read is not null, aka EOF
    while((line = reader.readLine()) != null){
        //Split along spaces
        String[] ints = line.split(" ");
        //For each integer in the line
        for(String number : ints){
            try{
                //Try to parse the string into an integer
                Integer i = Integer.parse(number);
                //Add it to the list
                numbers.add(i);
            }
            catch(NumberFormatException ex){
                //Parsing failed, keep going anyway
                continue;
            }
        }
    }
}
catch(IOException ex){
    //TODO Handle exception
}
 
Share this answer
 
v2
Comments
SimpDev 25-Nov-13 8:18am    
Thanks Glek, you helped me a lot
Glek 25-Nov-13 10:14am    
Not a problem. Glad to help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900