Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Question about scanners?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, new to java and to JavaRanch, dont' hate my silly questions, I'll have pleanty!

So about scanners, I"m having issues at the moment scanning a string like: Tom 4.56232 Blanken 1.230.

How would one skip the double numbers and just print "Tom" & "Blanken"? Thanks!



 
William Nerem
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*Edit Note* Keep getting compile errors when trying to run it. I tried skipping it, with Scanner.next, but... no luck there.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Can you show the code you are running and the compiler errors you are getting.
 
William Nerem
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well...



What I have marked out is the part I'm stuck on, trying to get this loop to print everything except for the names. How to get around a double variable?

Error is:


Top10.txt looks like this:
 
William Nerem
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shouldn't... Bob 3.4000 Richardson

System.out.print(lineScanner.next() + " ");
lineScanner.next();
System.out.print(lineScanner.next() + " ");

Print Bob Richardson? Why doesn't lineScanner.next() work in the loop with doubles?
 
William Nerem
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose the best approach, to avoid this NoSuchElementException is to make a hasNextDouble()... so if I have that, lets try printing the doubles...

Ok weird, it printed the year, and not the double values in the top10.txt... why'd it do that?



Edit --- *AfterPostThoughts*:

So... 8.1456 isn't a double?

What does this mean?

// assign locale as US to recognize double numbers in a string
scanner.useLocale(Locale.US);



Anybody have any advice, I'm drowning here lol....
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

William Nerem wrote:Shouldn't... Bob 3.4000 Richardson

System.out.print(lineScanner.next() + " ");
lineScanner.next();
System.out.print(lineScanner.next() + " ");

Print Bob Richardson? Why doesn't lineScanner.next() work in the loop with doubles?


The following works for me:
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

William Nerem wrote:Anybody have any advice, I'm drowning here lol....


Yes (and I know I'm going to get a lot of flak for it, so I will add that it's ONLY my opinion ):
Don't use Scanner's "typed" methods.

IMO, the ONLY method you need to know about with Scanner - apart from close(), which almost everyone forgets - is: nextLine().

If you type something in AND press the 'ENTER' key, you will always get a String; and furthermore, your Scanner will always be in a state to accept another "line" of input (not always the case if you don't).

What you DO with that line, after you've got it, is then entirely up to you.

I should add that the above only works if you are simply using Scanner to accept keyboard input. If you want to double it up to also accept input from a file, then you have to box a bit more clever; but it sounds like that's not your situation.

Winston
 
William Nerem
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

There is where my debugging stops me, it told me source not found? So it printed the first line, 1880, and it stops at Bob...
 
Ranch Hand
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I run your first code and I didn't get any runtime exceptions.
And you don't have to use another Scanner and search line by line. The next() method of the Scanner will keep search in the next line if the current line has end.



What do think will print here..? 12.3456.. No, here you get that runtime error. But, if there weren't that abcd answer will be 12.3456

Scanner lineScanner = new Scanner(line);
while (lineScanner.hasNextDouble()){
System.out.print(lineScanner.next() + " " + "\n");



The text on the file do not contain double value in the first place, except the first line. Therefore hasNextDouble() works only for first line.

So you have to keep searching for a double value, if find one print it. And if it is not the end of the file, keep doing that thing.



Actually, your question was to print the names, not the doubles. I think this may help.
 
Winston Gutkowski
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

William Nerem wrote:There is where my debugging stops me, it told me source not found? So it printed the first line, 1880, and it stops at Bob...


My apologies. Didn't read the whole thread (cautionary tale to others ).

I'm still not clear where you're getting this error, because you've commented out some code and haven't included the line number (or a comment stating where you ARE getting it). nextInt() would throw something like it on line 2 of your file because it doesn't start with an integer; but you seem to be precluding that with hasNextInt() (Edit: Doh! But of course, you're not since, as Ramesh pointed out, that number is NOT an integer).

I think you need to give us ALL the details, and your code EXACTLY as you executed it.

Furthermore, your 'lineTwoScanner' field is redundant because it's based on the exact same String as 'lineScanner'.

Suggestion: Have you considered using String.split() instead? This strikes me as a VERY fiddly case for a Scanner.

Winston
 
William Nerem
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm actually working through this problem slowly. Thanks for the input / idea's. Here's my current code with no errors:


So what I'm trying to do is print the year and first two names in this document.


So I should print:
1880 John Mary
1881 John Mary

I'm not there yet because I have to find a way to break the while loop after I find the first 2 names while avoiding those stupid doubles.
 
Winston Gutkowski
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

William Nerem wrote:I'm not there yet because I have to find a way to break the while loop after I find the first 2 names while avoiding those stupid doubles.


Right, and this goes back to my first piece of advice: forget every method except nextLine() (and hasNextLine()).

Assuming you can rely on the format you've shown us, this would be my solution:Do you see the logic? Instead of trying to trawl through each field individually, I just split each line up. If it has 1 field, I assume its a "year" line, and if it has 4, I assume it's a "names" line. Furthermore, if I'm looking for a year line, I can ignore any lines that don't have exactly 1 field, and conversely if I'm looking for a "name" line, I can ignore any that don't have 4.

I should add that this is only ONE way of doing it; there are many others, including solutions that use Scanner throughout. You might also need to refine it slightly if a "year" line must be followed by a name line, or there are other types of line in the file.

HIH

Winston
 
bacon. tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!