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

Need help reading text from files  RSS feed

 
Aron Silvester
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using java to write this code

I have a text file that has the following strings:
John
chris
Zonado
Buggy

The output when I run this is:
run:
null null null null BUILD SUCCESSFUL (total time: 0 seconds)

What is causing these null outputs? My array has the right amount of size to store the 4 strings.

CROSS POST LINK HERE: Need help reading text from files


 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Runs fine for me, Aron. The code you posted should print five lines, one for each name in your file, and a fifth line with all four names on it. What you describe as your output doesn't show the first four lines, which should appear even if the Strings are all null references. Have you posted exactly what your source code is?

By the way, cross-posting the same question to more than one forum is frowned upon. Some people think it is a pretty big deal, while others don't, but it can lead to people spending time and effort in one forum, trying to help you, after people in the other forum have already solved your problem for you. Best practice is to pick one and stick with it for a given question. I'd recommend coderanch.com, but I'm a bit biased about that.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aron Silvester wrote:The output when I run this is:
run:
null null null null BUILD SUCCESSFUL (total time: 0 seconds)

That's odd, because even if it's wrong (and nothing leaps out at me) I'd expect it to be
null
null
null
null null null null null BUILD SUCCESSFUL...


Are you sure you're running the exact code you showed us?

One point though. In theory, the check should be
  inputFile.hasNextLine()
although I don't think it should cause an error in this case.
It might also cause the last "line" to be missed if it isn't specifically terminated with a newline character - TBH, I'm not absolutely sure about this.

It's worth remembering though: Whenever you use 'nextXXX()', the check you use should be 'hasNextXXX()'.

BTW, you can make that first loop a bit more self-contained by using a for loop, viz:
HIH

Winston
 
Aron Silvester
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Aron Silvester wrote:The output when I run this is:
run:
null null null null BUILD SUCCESSFUL (total time: 0 seconds)

That's odd, because even if it's wrong (and nothing leaps out at me) I'd expect it to be
null
null
null
null null null null null BUILD SUCCESSFUL...

Are you sure you're running the exact code you showed us?

Winston



Yes I am running the exact same code.

Actually I put a print statement in the while loop to see if it's executing. The print statement did not execute.

Also, I'm pretty sure that the program has access to my text file, if it didn't it would be giving me a file not found exception. So it's no that.
 
Aron Silvester
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:Runs fine for me, Aron. The code you posted should print five lines, one for each name in your file, and a fifth line with all four names on it. What you describe as your output doesn't show the first four lines, which should appear even if the Strings are all null references. Have you posted exactly what your source code is?

By the way, cross-posting the same question to more than one forum is frowned upon. Some people think it is a pretty big deal, while others don't, but it can lead to people spending time and effort in one forum, trying to help you, after people in the other forum have already solved your problem for you. Best practice is to pick one and stick with it for a given question. I'd recommend coderanch.com, but I'm a bit biased about that.


Okay sorry bout that, I'll keep that in mind next time. My while loop is not executing, I put print statement in the loop and it did not print, so therefore it's not entering the while loop, which I don't know why.
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is pretty good about handling the inconsistent ways that Linux/Unix and Windows handle the "/" and "\" characters, but I wonder if you are having some problem related to that. What operating system are you using? I'm using Windows. Cutting and pasting your posted code, with the only change being from "/Users/regies/NetBeansProjects/Sanchez_Project#2/ListOfNames.txt" to "/Users/smiller/ListOfNames.txt", it runs fine for me.

Use a debugger and step through your code line by line. If the while loop isn't even being entered, it means that the call to inputFile.hasNext() is returning false, which can only mean your Scanner thinks it is looking at an empty File. So, I'm guessing your problem is at Line 22. Use a debugger and let us know what you find there.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aron Silvester wrote:Okay sorry bout that, I'll keep that in mind next time. My while loop is not executing, I put print statement in the loop and it did not print, so therefore it's not entering the while loop, which I don't know why.

Actually, I suspect it means you're not running that code. Did you compile it?

Rest assured: if you successfully created a .class file from that source, and you're running it correctly, it will print out the stuff at line 30.

The Java Gods may be many things, but they're not capricious.

Winston
 
Aron Silvester
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:Java is pretty good about handling the inconsistent ways that Linux/Unix and Windows handle the "/" and "\" characters, but I wonder if you are having some problem related to that. What operating system are you using? I'm using Windows. Cutting and pasting your posted code, with the only change being from "/Users/regies/NetBeansProjects/Sanchez_Project#2/ListOfNames.txt" to "/Users/smiller/ListOfNames.txt", it runs fine for me.

Use a debugger and step through your code line by line. If the while loop isn't even being entered, it means that the call to inputFile.hasNext() is returning false, which can only mean your Scanner thinks it is looking at an empty File. So, I'm guessing your problem is at Line 22. Use a debugger and let us know what you find there.


Okay I will do that but quick question. For my text file I used microsoft word and typed in the strings. I saved it normally. Once saved on my desktop I proceeded to add .txt at the end of the name. Is that how you save the text files for these? Maybe that's causing the problem?
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aron Silvester wrote:For my text file I used microsoft word and typed in the strings. I saved it normally. Once saved on my desktop I proceeded to add .txt at the end of the name. Is that how you save the text files for these? Maybe that's causing the problem?

Possibly. A word file definitely isn't the same as text. Use Notepad, or alternatively download a text editor - there are gazillions out there, including several that "understand" Java code and display in nice colours for you.

Unfortunately, I'm a Linux user now, so I've forgotten all things Microsoft.

Winston
 
Aron Silvester
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Aron Silvester wrote:For my text file I used microsoft word and typed in the strings. I saved it normally. Once saved on my desktop I proceeded to add .txt at the end of the name. Is that how you save the text files for these? Maybe that's causing the problem?

Possibly. A Word file definitely isn't the same as text. Use Notepad, or alternatively download a text editor - there are gazillions out there, including several that "understand" Java code and display in nice colours for you.

Unfortunately, I'm a Linux user now, so I've forgotten all things Microsoft.

Winston


I'm suspecting that the way I created the text file is the one that's cousin the problem.

Okay I've created another file (not using microsoft word). I used my mac's built in text editor. When I ran the program again, now the print statement ("Hello") in the while loop executed 4 times. Before when I used the microsoft file the print statement in the while loop did not execute.

But now there's a bunch of weird words. Here is the output. Still did not print the names in the text file though.

run:
Printing out the length of the file
351
Hello World
Hello World
Hello World
Hello World
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf13 0 {\fonttbl\f0\fswiss\fcharset0 Helvetica;} {\colortbl;\red255\green255\blue255;} \margl1440\margr1440\vieww10800\viewh8400\viewkind 0 BUILD SUCCESSFUL (total time: 0 seconds)
 
Aron Silvester
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It worked. Thank you guys so much!!!
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aron Silvester wrote:It worked. Thank you guys so much!!!


Glad we could help. Winston got it for you: MS Word does not create text files. In general, it is a bad idea to change the extension of a filename. Most likely, Windows gave you a warning when you did that. Warnings should be respected, if not universally abided by.

The lesson here is to check your assumptions. You (and I) assumed your input file contained certain data that it did not contain. Your program wasn't the problem. The problem was that you didn't check your assumption. Understandable, if you are not familiar with MS Word. But, when something seems like it has to work, and it's not, you may be looking in the wrong place.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!