• Post Reply Bookmark Topic Watch Topic
  • New Topic

File reading  RSS feed

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get a little confused with the file reading. I want to read a file like this :
180
16 0.23
14 0.21
156 0.22
23 0.8
5 1.1
19 0.33
64 0.19
108 0.45
40 0.95
....

So i need to save the first number in an int N, the first column of the other numbers in an array and the second in an other array.
Which way would you recommend me ?
 
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the number from the first line; create the two arrays with that size; set a counter to zero.

For each line in the file: read two numbers; put them into the arrays at the index which the counter points to; increment the counter.
 
Christina Tzogka
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I did this but after the compile occur error at line 9 and 10. Please tell me what is wrong in my code.
 
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to use 'w' as a count down counter, something like

Instead of your while().
 
Sheriff
Posts: 4930
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also don't use File along with the Scanner. You may run into problems at some point. Instead, use Path object. i.e.:
 
Ranch Hand
Posts: 35
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Liutauras,

Could you please elaborate why not to use the File object? I tried looking for it on Google but didn't find anything related to this.

Thanks.
 
Liutauras Vilda
Sheriff
Posts: 4930
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vaibhav Sagar wrote:Hi Liutauras,

Could you please elaborate why not to use the File object? I tried looking for it on Google but didn't find anything related to this.

Thanks.
Please go through this tutorial https://docs.oracle.com/javase/tutorial/essential/io/legacy.html

it is a bit unfortunate that Oracle only in tutorial page mentions that, but not adding a reference into API documentation. And quite recently we had a chance to see those actual drawbacks in practice when File along with Scanner was failing to read a textual file. Of course there were not a *.txt, but rather some kind of other format + some specific encoding used, but that is good indication, that better is to stick to Path object as suggested in the link I gave you.

Check what options gives you Paths and Files from java.nio.file package (not from java.io) gives you. There are some useful methods which File does not have.
 
Christina Tzogka
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys i tried all the ways you recommended but it's error again and again ( it shows error in for too ) ! I don't know what else i could do... Could anyone give me exactly the right code ?
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We don't give out answers like that here. We will help you, but we feel that handing you the solution doesn't teach you anything. Further, if this is a school assignment, handing you a solution could lead to ethics issues.

The best way for us to help you is for you to provide all the information. Compiler errors MEAN something. Knowing what it says tells us a LOT. So, post your current code, and the full text of just the first one or two compiler errors. Having all that will let us focus in on the issue, and is much easier than guessing.
 
Christina Tzogka
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Off course i don't except you to solve my project ! It's a whole semester project and i only asked your help for this part that confused me, you should not worry about ethic issues. I don't want to give you the image of a programmer who looks for ready and easy work !! Simply i thought that someone could give me these 5-6 lines of code. I tried everything you told me before but for an incomprehensible reason doesn't work although it seems ok. It is my fault that i didn't refer exactly the error messages. Also, i tried to simplify the file reading so i wrote this in order to print the numbers that have been read.



Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at javaapplication99.Main.main(Main.java:3)
Java Result: 1
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It appears that the exception is thrown by this line here, line 3:



It's thrown by the nextInt() method, too, the stack trace tells you that. (Can you see from the stack trace why I made those two statements?)

So that would mean that the first thing the code does, calling nextInt() on the scanner, finds something unexpected. I don't know what that might be but I'd suggest a smaller piece of code to find out if I'm on the right track.



I'm suggesting that because a Scanner is a weird and complicated object which can easily be thrown off track, at least to me it is. So let's see if maybe you're looking at an empty file, or the wrong file, or something else.
 
Christina Tzogka
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the file that i am expecting to read :
16 0.23
14 0.21
156 0.22
23 0.8
5 1.1
19 0.33
64 0.19
108 0.45
40 0.95

The first number is int and the second is float. I tried your code and it works properly, it prints 16 ! But when i try to add a float x in order to read the next number it is error again (the float) by exception.
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your original post:

Christina Tzogka wrote:I get a little confused with the file reading. I want to read a file like this :
180
16 0.23
14 0.21
156 0.22
....

So i need to save the first number in an int N, the first column of the other numbers in an array and the second in an other array.
Which way would you recommend me ?


So now your requirements have changed: you don't have a line at the beginning which says how many lines follow it, and that's what your code is expecting.
 
Christina Tzogka
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed my file on purpose in order to be sure that it will read the int number and the float next to it (before i said that i simplified the code, i meant this part too). When i accomplish that i will add the number that i mentioned at the first line.
 
Liutauras Vilda
Sheriff
Posts: 4930
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, it is clear I think that requirements are not clear.

Christina Tzogka wrote:So i need to save the first number in an int N, the first column of the other numbers in an array and the second in an other array.
Read this sentence multiple times, but still don't understand what it means. Especially underlined parts.
I see 2 columns of numbers in total, but the way you describe it, it sounds like you take 3 numbers from the one line:
  • 1. first number in an int N
  • 2. other numbers in an array
  • 3. and the second in an other array

  • Try to rephrase it, or colour numbers and tell us, which colour needs to go where.
    Exercises are not getting solved that way - what needs to be done needs to be SUPER clear.
     
    Liutauras Vilda
    Sheriff
    Posts: 4930
    334
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    16 0.23
    14 0.21
    156 0.22
    23 0.8
    5 1.1
    19 0.33
    64 0.19
    108 0.45
    40 0.95

    So:
  • RED goes to an int N
  • GREEN goes to an array A
  • BLUE goes to an array B


  • Is that correct?

    [edit] I'm sorry in advance the ones, who might are colour blind
     
    Ranch Foreman
    Posts: 3068
    37
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The "proper" format is in the original post, so the number going into 'n' would be 180, since the first row has only a single number in it.
    After that, each row has two numbers, the first goes into one array (presumably ints) and the second into another (presumable doubles).

    Personally I would create a class to hold the two numbers and have a single array, since I assume that the two numbers relate to each other ins ome way.
     
    Liutauras Vilda
    Sheriff
    Posts: 4930
    334
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Oh.. Thanks Dave for clearing this up.

    Then Carey's Brown given idea would work.
    @Christina, you'd just need to add another 2 reading statements inside the loop, one for nextInt and another for nextDouble.

    with the condition - only if the data file content looks like:
    Christina Tzogka wrote:180
    16 0.23
    14 0.21
    156 0.22
    23 0.8
    5 1.1
    19 0.33
    64 0.19
    108 0.45
    40 0.95
    ....
     
    Christina Tzogka
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you Dave this is exactly what i am thinking ! Thank you all guys, i am going to try Dave's idea using the code Carey and Liutauras recommended !
     
    Sheriff
    Posts: 4289
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Post your code back here when you're finished so we can all bask in its beauty. ;-)
     
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Christina Tzogka wrote:Thank you Dave this is exactly what i am thinking ! Thank you all guys, i am going to try Dave's idea using the code Carey and Liutauras recommended !

    You might also want to think about what these two values represent, and encapsulate them in a single class rather than dealing with each one separately.

    And BTW: you may be frustrated (we've all been there ), but it doesn't pay to take it out on people who are trying to help.

    Almost always, frustration comes from coding before you're ready, so my advice for the future:
    Stop; chill out; have a pint (if you're old enough); play a video game; go for a run; or bash a ball or a workout bag until you've cleared your mind enough to look at your problem afresh.

    HIH

    Winston
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!