Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

input output from file  RSS feed

 
mike Vigor
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a beginner so bear with me ...

I have a class Row...


I have data like this on file text.txt


now I awant to fill an arrayList of type Row with the data in text.txt where each row object will be having it five members as digits of a line in the text.txt file.

this is the very poor code I came up with which is not working "I'm bowing my head in shame" lol...


so someone help me do better..thanks
 
Dave Tolls
Rancher
Posts: 2913
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "doesn't work"?

Also, what is the value of wheelName?
Scanner doesn't work with file names, it works with Streams/Files/Paths/Readers/Strings.
If you pass a String into its constructor it will treat the String as the data to scan over.
 
mike Vigor
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:What do you mean by "doesn't work"?

Also, what is the value of wheelName?
Scanner doesn't work with file names, it works with Streams/Files/Paths/Readers/Strings.
If you pass a String into its constructor it will treat the String as the data to scan over.


it doesnt work means it doesn't do what I wrote it to do..wheelname is text.txt...
 
Campbell Ritchie
Marshal
Posts: 55691
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mike Vigor wrote:. . . it doesnt work means it doesn't do what I wrote it to do. . . .
Please read this. Your program is doing exactly what you wrote it to do. Maybe not what you want, but we can only help you if you tell us the full details. You have already been told why you sho‍uld not pass Strings to your Scanner constructor. And wheelName is a dreadful name for that parameter.
I can see some serious problems with your Row class, too:-
  • 1: Why have you get a no‑arguments constructor? If you have fields, then a no‑arguments constructor usually constitutes a design mistake.
  • 2: Why have you not marked all the fields with private access?
  • 3: Why have you not written any methods? You sho‍uld at least provide getXXX methods and toString.
  •  
    mike Vigor
    Ranch Hand
    Posts: 49
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    mike Vigor wrote:. . . it doesnt work means it doesn't do what I wrote it to do. . . .
    Please read this. Your program is doing exactly what you wrote it to do. Maybe not what you want, but we can only help you if you tell us the full details. You have already been told why you should not pass Strings to your Scanner constructor. And wheelName is a dreadful name for that parameter.
    I can see some serious problems with your Row class, too:-
  • 1: Why have you get a no‑arguments constructor? If you have fields, then a no‑arguments constructor usually constitutes a design mistake.
  • 2: Why have you not marked all the fields with private access?
  • 3: Why have you not written any methods? You should at least provide getXXX methods and toString.


  • so instead of asking me why why, why don't you propose a better solution? I'm really new to java although I'm still learning, can you give a better snippet to copy line by line the contents of text.txt which resides in the same folder as the program into an arraylist<row> , like I asked in the question? first lets get it to do what I want, then we can talk about conventions and optimizations please...
    I thought I was very specific in the question ...

    I need to copy the contents of text.txt into an arraylist<Row>...and possibly display it on the screen, the code I wrote is not doing exactly that...thats why I came to this forum please...now I'm trying some suggestions but in the mean time if someone can give me a better solutions..thanks
     
    Carey Brown
    Bartender
    Posts: 2992
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    For starters, change
    to
    Edit: BTW, this example is shown in the Javadocs for Scanner.
     
    Dave Tolls
    Rancher
    Posts: 2913
    35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    mike Vigor wrote:
    so instead of asking me why why, why don't you propose a better solution? 


    It helps us to know what issue you are having so that we can focus on that area.
    Otherwise we have to spend a lot more time trying to figure out what it is this is supposed to do and exactly which bit of it isn't working.
    If you tell us "I go this result ... when I expected this result..." that really helps a lot, and it helps you to make our life a little easier.
     
    Liutauras Vilda
    Marshal
    Posts: 4636
    316
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi mike,

    mike Vigor wrote:so instead of asking me why why, why don't you propose a better solution?
    Campbell Ritchie gave you some questions as a hints for you to re-think your solution. This site doesn't provide complete solutions, exceptions are very rare. Read once again CR post and try to extract the given advises.

    Do you have some pseudo code written for a start, so you'd know what goes after what?

    As far as I understand you want to read the file row by row and create Row objects out of each Row.
    So, in order to do that, you need first to come up with Row class. You already started, but CR gave you advises how to improve it, don't you want to do it right away? That is part of your task it seems anyway. Once you get that part correct, you can move on reading the file part. One task at a time. Currently it seems you have everything done partially rather than fully, moreover - neither of those are correct, so to assemble those parts wouldn't make much sense, as the output would be wrong too (you found that out already).

     
    mike Vigor
    Ranch Hand
    Posts: 49
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    So thanks for the stern suggestions, the code has been corrected and things are running smoothly now, but there is this new problem.
    although the text file resides in the same folder as the code, at run time I get the message "the system cannot find the file specified"
    ...I changed name of the file..same.
    this is quite strange, anybody knows why?
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 7806
    142
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Show us your corrections and the file name that you pass to your method.
     
    mike Vigor
    Ranch Hand
    Posts: 49
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Stephan van Hulst wrote:Show us your corrections and the file name that you pass to your method.


    solved by inserting the absolute path...
    thanks to everybody
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!