File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes incorrect data and hasNext() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "incorrect data and hasNext()" Watch "incorrect data and hasNext()" New topic
Author

incorrect data and hasNext()

Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 123
I am reading different lines from a file and for some reason the one named amount balance is outputing incorrect data. Even if I edit the txt file and change the number, the program will still output the same incorrect number.

I am also trying to get the program to read many "accounts" in the same file but when I try to use



I get "wedge2: exit code for process is 1."

Any help on this would be great. Thanks.

I posted a snippet of my code below.



Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Why are you catching new line character again at line 20, when you previously invoked nextLine()?


The quieter you are, the more you are able to hear.
Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 123
Kemal Sokolovic wrote:Why are you catching new line character again at line 20, when you previously invoked nextLine()?


I thought that may solve the problem but it did not and I forgot to remove it.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38844
    
  23
Welcome to the Ranch

I think you have misunderstood Scanner#nextLine(). Don’t worry, since a lot of books don’t get it right, even!
The default implementation of Scanner (if you haven’t altered its delimiter) uses something like \s+ as its regular expression which means one or more whitespace characters. And line end characters count as whitespace, so the nextAnything methods don’t so much catch new line or anything, as skip over it and ignore it. Most people who have problems with nextLine sort it out by invoking it once more (example here).

You appear to have taken the opposite approach, calling nextLine once too often.
For your code to work, I would expect the file to look like thisYour line 20 reads the blank line after Ordinary Retail. If the 234.56 was supposed to be there, you will gradually get out of phase with the file, until you suffer an InputMismatchException.

Lots of people tell you to use doubles, but that is severely mistaken for money. Anybody would think they don’t know about BigDecimal or other accurate forms of arithmetic.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

And what is the format of the data you are trying to read from the file? I just tested code (without line 20) for the given content of txt file:

and everything works fine.
Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 123
Kemal Sokolovic wrote:And what is the format of the data you are trying to read from the file? I just tested code (without line 20) for the given content of txt file:

and everything works file.


The format has account number on first line, account type on second, customer name on third, customer type on forth, and balance on fifth.

12345
checking
Marty McFly
business
23456.78

after this there is a blank line and then another account, and there are quite a few of them.

OK. This is working now. So I should just always use float if it is money?

Any insight on the while loop?
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

I suppose you are reading data in a while loop.
Then you could add inputFile.nextLine(); after reading that double value, to skip that empty line, and in next iteration you will read the correct data. Just note that if you use this approach, you will need to add new line after the last line containing data in your txt file. So basically:

with the file looking like this:
Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 123
That is what I thought it was but for some reason when I ran it, it was only printing the first set of account details. Now, it is only leaving the last one off. I have seven and it is only printing six.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38844
    
  23
That sounds like an off‑by‑one error. The first thing to check with one of those things is your loop. I would have thought Kemal Sokolovic’s while loop would have given you the correct count.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

And it does work with me, as long as data in the file is organized as stated previously.
Can you post the exact code (entire while loop) and contents of txt you are working with? Maybe you overlook something.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38844
    
  23
That might be because you require a blank line at the end. If the blank line is missing, as KS has already said, it won’t work. You always get this problem when reading such files: the format of the file must match the format expected by the program and vice versa.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Perhaps putting all account details in one line in a file, and separating them by some delimiter would be better in this case. It will involve some parsing (but still simple) of each line, but at least the while condition will tell you exactly what it should - when no more lines are in a file, stop reading it. This way there will be no need for skipping some lines as in previous solution, which very likely lead to an error.
Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 123
Here is what I have. It looks like the same thing unless I overlooked something. I will look closer. I did add a blank print line so it would space each one.

here is the exact format of the txt file. There are seven accounts listed and there is a blank line between each one.

12345
savings
William Wallace
business
34567.89

98765
checking
Tommy Gavin
student
145.26




By the way, thanks for all the help!



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38844
    
  23
Glad to be able to help.

But where is your Account class? You ought to read all those data from the file, create an Account object, and use its toString() method to display it. In the toString() method you can get everything formatted nicely; I would probably use String#format and the % tags, remembering to use %n for new line.
Don’t use println(" ") unless you specifically want spaces before the next text. If you simply want a new line, use println();
Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 123
Campbell Ritchie wrote:Glad to be able to help.

But where is your Account class? You ought to read all those data from the file, create an Account object, and use its toString() method to display it. In the toString() method you can get everything formatted nicely; I would probably use String#format and the % tags, remembering to use %n for new line.
Don’t use println(" ") unless you specifically want spaces before the next text. If you simply want a new line, use println();



That is something we have not learned yet so I can't use it in the program.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

You have learned file I/O, but you haven't learned about classes, toString() and other things Mr Campbell mentioned?
Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 123
Kemal Sokolovic wrote:You have learned file I/O, but you haven't learned about classes, toString() and other things Mr Campbell mentioned?


We are in Chapter 4 in our text book and the toString() method is first introduced in Chapter 9. Is Mr. Campbell talking about creating a class or object? We have not covered that either.

Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

All right then. It's a little different learning path than the one I followed couple of years ago.
Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 123
Kemal Sokolovic wrote:All right then. It's a little different learning path than the one I followed couple of years ago.


Of course I am not an expert but IO file does seem a little out of place in the book. We went from if and if else statements to while and for loops to reading and writing from a file. It was only a few pages and then random numbers.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38844
    
  23
Raymond Gillespie wrote: . . . Is Mr. Campbell talking about creating a class or object? . . .
Both.
Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 123
Campbell Ritchie wrote:
Raymond Gillespie wrote: . . . Is Mr. Campbell talking about creating a class or object? . . .
Both.


I do not know how to do that just yet but after reading about it, it does not seem difficult, but as I said, we are not to use anything we have not "learned" in class yet.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Then do it yourself, on the side, and for the class do whatever the requirement is. Don't wait for others to teach you how to write programs.
Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 123
Kemal Sokolovic wrote:Then do it yourself, on the side, and for the class do whatever the requirement is. Don't wait for others to teach you how to write programs.


That sound like a good idea. I try to work ahead as much as possible that way I have at least somewhat of an understanding of what is going on before we cover it in class.

Right now, I am taking Python, C++, and Java and when I get an assignment in one, I write the program in all three languages.

Anyway, even if I do use what he is talking about, that will still not solve the problem I have right now, so I need to figure that out first.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

I thought you've already resolved that issue with reading a file? What, it's still not reading the last one?
Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 123
Kemal Sokolovic wrote:I thought you've already resolved that issue with reading a file? What, it's still not reading the last one?


It was but I have been messing with it and I removed the last inputFile.nextLine(); I had and it is now working properly. Well, almost. I also have to add a monthly fee to each account so I included some if else statements to determine the fee and everything is returning 0 so I still have to figure that out. I thought this should be in the same while loop but now I am not so sure because if I do not initialize the monthly fee variable before the while loop, it will not compile and says the variable has not been initialized on the line where I am printing it out so it looks like the if statements are been ignored, overlooked, or skipped for some reason or other.

I should be able to figure it out.

I want to say thanks again for the help. So far, this is the first time I have had trouble figuring something out and you have been very nice and very helpful. I am sure I will hang out here more because of all the useful info as well as the great insight from you guys. Hopefully, I will be able to help a few others as well one day.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

If you removed that last inputFile.nextLine() then you need to remove those blank lines from your txt file, too. There is one suggestion I made couple of posts above, that you format your txt so for each account information is stored in one line of your txt file. E.g.:

Unless the requirement is made for the format of the file, this solution would probably save you a lot of trouble. You would have to do some parsing, as I mentioned, and since you need to follow your class for the implementation I don't know if you've done some of it. But for your own practice you can check StringTokenizer class and see how it works. Your code would then be rather simple:


As of that monthly fee, if that's the part of data you need to read then you should really do it in the same loop (whatever approach you use). Otherwise, you'll have to add another loop that will once more read each line of the file, just in order to get one value per each account, and that doesn't sound like the best solution. If you think you did it wrong somewhere, feel free to post it here.

And, finally, you're very welcome! As long as you want to learn new stuff, and expand your existing knowledge/skill, this is the best place to do that.
Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 123
I had noticed that you suggested to modify the txt file but I am not allowed to do that either.

The monthly fee is not read data. For example, if the account type is checking, the account type is business, and the balance > 40,000 the fee would be zero. If the fee is < 40,000 the fee would be $100.

I have a table to go by for which account have fees and the stipulations of each.

I at first put the determining statements in the hasNext while loop, but as I said, they are being skipped or ignored. My second thought was to put it in it's own loop, but I have not decided, or figured out, what the condition would be for that loop.

I am all about learning. I like to try to come up with my own solutions without any help but sometimes, one just has to ask for a little help.
Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 123
I got everything working today. I was not comparing the strings properly in the if statements. I was using == instead of .equals

Taking three languages at one time makes me do dumb things like that sometimes. Two days ago when writing a Python program, it took my 15 minutes to realize I needed to use elif instead of else if.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38844
    
  23
Beware if you have used C# because (I think) == is overloaded for Strings and works like String#equals() in C#
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: incorrect data and hasNext()