• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception in Reading the file  RSS feed

 
Rakesh Keerthi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have been trying to solve problem from a programming site and the data is as below. content in my text file as below.


here the first number gives the number of test cases, here it is 2, and the number after it is the sum and the next line is the size of array followed by the array elements(numbers). here i need to sum the array numbers and match with the sum that is given. and print the position of the numbers that match the sum.

In the above case the results should be.


i managed to get this manually but unable to figure out how to do this from a text file.

the code i used to get the values is as below.


and the code i used to get the values from text file is as below.


when i ran the above code, i got the below output.


here i'm unable to figure out how the data can be read from file(the sum, the number of inputs with arraysize etc..). please let me know how i can do it.

I've tried the below code to get my solution, but there is an exception thrown.



The error i get is


please let me know how can i fix it.

Also in my input text file, the values are comma separated, just want to know if this leads to exception by any chance.

Thanks
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should read the file line by line (using a BufferedReader wrapped around a FileReader), and then use a Scanner for each line (instead of one Scanner for the entire file, as you have it now).
 
Rakesh Keerthi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the Reply Dittmer
You should read the file line by line


when i ran a code(as mentioned in my question to fetch values into arraylist only), the output i got was



and in my text file i have it as



Here the data in my text file is comma seperated, but not in different lines.


(using a BufferedReader wrapped around a FileReader), and then use a Scanner for each line (instead of one Scanner for the entire file)


Just want to know what difference does it make and also wanted to know why the exception triggered.

Thanks



 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the Reply Dittmer

My first name is Ulf. If you want to address me by my last name, then that's fine, but the polite way to do so is by prefixing it by "Mr". First name is fine, though.

Here the data in my text file is comma seperated, but not in different lines.

The data you posted is organized by lines. So the data in the actual file has some other format? If so, post the actual format you're working with.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!