• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Input in a do while loop

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I run this it start out okay. It asks for the name and then the salary. When I type y for yes and press enter it prints both the name and salary prompt on one line. It goes right by the input for name. What am I doing wrong?


 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is with the line


Scanner's nextLine() here is not the correct method to use. Can you figure out which method to use?

Also for lines 8 and 9, does the compiler let you compile without specific the type for the ArrayList??
 
Ranch Hand
Posts: 172
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Marxia,
Welcome to CodeRanch
Perhaps you need to check line 10, i think the variable answer requires initialisation because the variable answer would be initialised by default only if it was placed outside the main method and within the class scope
Also, though you have terminated the while loop with a semi-colon i dont understand the need of the code opening brace on line 22
Off course java allows you to enclose statements within braces but here its only amounting to redundancy
I would also advise you that once you are able to figure out the mistakes try to re-write this code in an object-oriented fashion
Hope this helps
 
Marixa Garcia
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

K. Tsang wrote:The problem is with the line


Scanner's nextLine() here is not the correct method to use. Can you figure out which method to use?

Also for lines 8 and 9, does the compiler let you compile without specific the type for the ArrayList??



I've read till my eyes were blurry, but I can't find another method that will read in a string with a first AND last name. In other words, including a space. Adding another nextLine() at the end seems to solve my problem (I found that solution on this site). But I would love to know what I should have done. I want to do it the correct way. I"m anxious to learn. This works:



And yes, NetBeans let me forget to add the type to the array list. It compiled and ran. I fixed it though.
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code is the real issue:


The issue is, when you read an input using Scanner#nextDouble() or any Scanner class method reading numeric types, they do not read the linefeed character at the end. And also, it does not advances the Scanner past the current line. So, the next time you read the input using Scanner#next() method, it reads the linefeed. And the input you passed as "y/n", is not read into the "answer" variable. And thus your condition is failed, and "while" loop exits.

And option is to read a blank line after the "input.nextDouble()" like this:

 
R. Jain
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

K. Tsang wrote:
Also for lines 8 and 9, does the compiler let you compile without specific the type for the ArrayList??


The syntax is fine for Java 7. It's called diamond operator. In Java 7, you don't need to give the type parameter on the RHS. It is inferred from the one used on the LHS.
 
R. Jain
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

R. Jain wrote:The issue is, when you read an input using Scanner#nextDouble() or any Scanner class method reading numeric types, they do not read the linefeed character at the end. And also, it does not advances the Scanner past the current line. So, the next time you read the input using Scanner#next() method, it reads the linefeed. And the input you passed as "y/n", is not read into the "answer" variable. And thus your condition is failed, and "while" loop exits.

And option is to read a blank line after the "input.nextDouble()" like this:



Ah! I'm sorry. I did a mistake there. You should put the "input.nextLine();" after the "input.next()". Actually, the Scanner#next() is fine. It will read the next token. But it also does not read the linefeed at the end. So, if the while condition is true, and you go onto another iteration, the linefeed left over by "input.next()" is read by "input.nextLine()", and your prompt directly goes to "input.nextDouble();".

So, change your code to:



Please ignore my last to last post.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marixa Garcia wrote:It goes right by the input for name. What am I doing wrong?


Hi Marixa, and welcome to JavaRanch.

Just to add to the good advice you've been given:
1. User input is tricky. It's fiddly, and takes quite a lot of code to get right.
2. The Scanner class is not as simple to use as it might seem - as I suspect you're already discovering - although it's simpler than most alternatives.
3. Once you've solved your current problem, you'll probably run into another one: ensuring that each requested value is correct. Try entering "X" when your program asks you to enter the salary, and you'll see what I mean.

The basic problem is that the keyboard is a character-based device, so what it returns are either single characters or Strings. Methods like nextDouble() simply convert the string of characters you type in to the correct type under the hood, and it will throw an Exception if the characters you type in are not what it expects.

Personally, I always use nextLine(), and convert the String it returns myself. For example, instead of:
input.nextDouble()
you can use:
Double.valueOf( input.nextLine() )
or:
Double.parseDouble( input.nextLine() )
which basically do the same thing.

It's a bit more typing, but it has a couple of advantages:
1. It forces the user to press the ENTER key before your program processes anything.
2. You don't have to worry about "unread" newlines, so the process is always the same.

The only disadvantage is that you can't change to a different form of input (like a file) as easily; but I doubt that you need to worry about that for a while yet.

However, it's just an alternative; and if you're happier with nextDouble() and next(), feel free to ignore it.

HIH

Winston
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

R. Jain wrote:

K. Tsang wrote:
Also for lines 8 and 9, does the compiler let you compile without specific the type for the ArrayList??


The syntax is fine for Java 7. It's called diamond operator. In Java 7, you don't need to give the type parameter on the RHS. It is inferred from the one used on the LHS.



Thank R. Jain. Now I know what's the diamond operator Not really into Java 7 yet.
 
Marixa Garcia
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. That's all good advice.
 
Bartender
Posts: 5465
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

R. Jain said:
The syntax is fine for Java 7. It's called diamond operator. In Java 7, you don't need to give the type parameter on the RHS. It is inferred from the one used on the LHS.


Just found out (NetBeans pointed it out to me, I didn't know it): it also works with return values in methods, like this:


Greetz,
Piet
 
What's gotten into you? Could it be this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic