• 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

looping to repeat a step

 
Ranch Hand
Posts: 47
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having trouble figuring out how to make the ID number step of this program provide an option to press 0 if they need to reenter the code or 1 to advance. I know I need to use a nested loop somehow but I'm not sure how to implement it.




   
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need a nested loop, just a  single loop will do. Best to use a "do-while" loop and loop while the user inputs a zero.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To print what the user has entered you keep using the same structure:
Two things about this: 1) use "println" not "print", and 2) don't append additional spaces after the Name.

Also, by Java convention, all variable names must begin with a lower case letter, so change "Name" to "name".
 
Sean Stevens
Ranch Hand
Posts: 47
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:To print what the user has entered you keep using the same structure:
Two things about this: 1) use "println" not "print", and 2) don't append additional spaces after the Name.

Also, by Java convention, all variable names must begin with a lower case letter, so change "Name" to "name".



I fixed some of the things you mentioned. Can you tell me whats wrong with the loop I've created here?





 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aren't you going to tell s what the compiler errors are? I can guess some. You are confusing String input and numbers; a String (id) isn';t a number, so you can't use the ++ operator on it. Nor can you use = 0 on it. Actually, you can't use = 0 on a number either in that context; you probably want == 0 instead. But I find that hard to understand. Why do you want to keep going while the id is equal to 0? Maybe you want to keep repeating while the id is not equal to 0.
I think you are getting confused between numbers and text. You probably want the id to be a number, in which case you would read it from your Scanner with its nextInt() method, or similar. Beware: a nextXXX() call followed by nextLine() needs nextLine() twice. Explanation here.
This is the sort of thing I would write:-
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "do" should be before the prompt on line 1. After you print id online 4 prompt again but this time ask for a '1' or '0'. Get input again but put it into a new variable, not "id". In the while check to see if the new variable is "0".

Note that your current while expression uses "=", this is the assignment operator, not the "==" which is the equality operator. And in your case you still won't be able to use "==" because that only compares references for equality and you want to compare the contents of the string. For that you need to use the "equals()" method.
And regarding your other changes you are still appending an empty String. Don't append any String.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

press 0 if they need to reenter the code or 1 to advance

This is the reason for "while zero".
 
Sean Stevens
Ranch Hand
Posts: 47
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:The "do" should be before the prompt on line 1. After you print id online 4 prompt again but this time ask for a '1' or '0'. Get input again but put it into a new variable, not "id". In the while check to see if the new variable is "0".

Note that your current while expression uses "=", this is the assignment operator, not the "==" which is the equality operator. And in your case you still won't be able to use "==" because that only compares references for equality and you want to compare the contents of the string. For that you need to use the "equals()" method.
And regarding your other changes you are still appending an empty String. Don't append any String.





I think I did what you said to do properly but it tells me "bad operand type String for unary operator" for line 33.


   
   

 
Sean Stevens
Ranch Hand
Posts: 47
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sean Stevens wrote:

Carey Brown wrote:The "do" should be before the prompt on line 1. After you print id online 4 prompt again but this time ask for a '1' or '0'. Get input again but put it into a new variable, not "id". In the while check to see if the new variable is "0".

Note that your current while expression uses "=", this is the assignment operator, not the "==" which is the equality operator. And in your case you still won't be able to use "==" because that only compares references for equality and you want to compare the contents of the string. For that you need to use the "equals()" method.
And regarding your other changes you are still appending an empty String. Don't append any String.





I think I did what you said to do properly but it tells me "bad operand type String for unary operator" for line 33.


   
   



Line 31 is where the error is.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Sean Stevens
Ranch Hand
Posts: 47
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Aren't you going to tell s what the compiler errors are? I can guess some. You are confusing String input and numbers; a String (id) isn';t a number, so you can't use the ++ operator on it. Nor can you use = 0 on it. Actually, you can't use = 0 on a number either in that context; you probably want == 0 instead. But I find that hard to understand. Why do you want to keep going while the id is equal to 0? Maybe you want to keep repeating while the id is not equal to 0.
I think you are getting confused between numbers and text. You probably want the id to be a number, in which case you would read it from your Scanner with its nextInt() method, or similar. Beware: a nextXXX() call followed by nextLine() needs nextLine() twice. Explanation here.
This is the sort of thing I would write:-



I made a new int variable and used nextint as you can see here. I think what I did wrong here was the while part of the statement. There are no error messages but the program doesn't advance beyond "you entered xxxx".


   
   
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two steps forward, one step back. You undid some of the other suggestions.

Make 'x' a String and use nextLine().
 
Sean Stevens
Ranch Hand
Posts: 47
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Two steps forward, one step back. You undid some of the other suggestions.



You said I couldn't increment the string so I tried switching it to an int.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sean Stevens wrote:

Carey Brown wrote:Two steps forward, one step back. You undid some of the other suggestions.



You said I couldn't increment the string so I tried switching it to an int.

True, but what was the purpose of incrementing it in the first place?
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remove line 27. I said that that line should be moved but instead you copied it.
 
Sean Stevens
Ranch Hand
Posts: 47
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Remove line 27. I said that that line should be moved but instead you copied it.




   


   

 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On line 21 you are attempting to get keyboard input without letting the user know that it's expecting anything or what it would be expecting. The order of statements is extremely important.
 
Sean Stevens
Ranch Hand
Posts: 47
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:On line 21 you are attempting to get keyboard input without letting the user know that it's expecting anything or what it would be expecting. The order of statements is extremely important.



Is this the proper order?

 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sean Stevens wrote:Is this the proper order?

You are still trying to get input before giving the user a prompt.
 
Sean Stevens
Ranch Hand
Posts: 47
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Sean Stevens wrote:Is this the proper order?

You are still trying to get input before giving the user a prompt.



But putting that line anywhere else results in the IDE saying the variable hasn't been initialized.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sean Stevens wrote:

Carey Brown wrote:

Sean Stevens wrote:Is this the proper order?

You are still trying to get input before giving the user a prompt.



But putting that line anywhere else results in the IDE saying the variable hasn't been initialized.


True, but the line it's complaining about is line 7 where you attempt to print 'x' before it has been initialized. Printing 'x' at this point serves no purpose, so, leave the println() statement but remove 'x' from it.
 
Sean Stevens
Ranch Hand
Posts: 47
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Sean Stevens wrote:

Carey Brown wrote:

Sean Stevens wrote:Is this the proper order?

You are still trying to get input before giving the user a prompt.



But putting that line anywhere else results in the IDE saying the variable hasn't been initialized.


True, but the line it's complaining about is line 7 where you attempt to print 'x' before it has been initialized. Printing 'x' at this point serves no purpose, so, leave the println() statement but remove 'x' from it.




Removing the "x" from line 7 caused the same problem to go down a line and say x hasn't been initialized.

 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now line 8 is missing. This is where you're supposed to get user input.
 
Sean Stevens
Ranch Hand
Posts: 47
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Now line 8 is missing. This is where you're supposed to get user input.



Okay I finally get it. Thanks


 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Persistence wins!

This statement is syntactically correct but grammatically incorrect. Just tweak the wording.
System.out.println("value of id : " + x);
 
Sean Stevens
Ranch Hand
Posts: 47
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Persistence wins!

This statement is syntactically correct but grammatically incorrect. Just tweak the wording.
System.out.println("value of id : " + x);



Yes I will just have to tweak it as you say. Another question for you, I need to use this information to compute amounts and display them in a table ( all in the same program ), is this possible from how I've set it up so far?
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are part way there. You'd have to change the variables for amounts from a String to an int or a double. Then you'd have to use nextInt() or nextDouble() to get the keyboard input.

There is an issue with getting keyboard input using one of the nextXXX() methods other than nextLine(). "nextInt()", for example, will leave a new-line character pending in the buffer at the point it returns the int. This is one of the sucky parts of dealing with a Scanner.

 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You only need to call newLine() and discard the result if you are going to use newLine() next. If you follow newXXX() by newYYY(), there is no need for a newLine() call.
For more details, look through my posts for “Beatles”, and you will find this, with more explanation in.

[edit]I said newLine() and newXXX(); that should read nextLine() and nextXXX() etc.
 
Sean Stevens
Ranch Hand
Posts: 47
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:You are part way there. You'd have to change the variables for amounts from a String to an int or a double. Then you'd have to use nextInt() or nextDouble() to get the keyboard input.

There is an issue with getting keyboard input using one of the nextXXX() methods other than nextLine(). "nextInt()", for example, will leave a new-line character pending in the buffer at the point it returns the int. This is one of the sucky parts of dealing with a Scanner.



public static void main(String[] args) {
       int name;
       int id;
       int fm;
       int income;
       int x;

       //Create scanner to obtain user input
       Scanner scanner1 = new Scanner(System.in);

       //obtain user input
       System.out.println("Enter your first and last name: ");
       name = scanner1.nextInt();
       //output information
       System.out.println("You entered " + name + "");

       do {
           System.out.println("Enter your 4 digit ID number: ");
           id = scanner1.nextInt();

           System.out.println("You entered " + id + "");

           System.out.println("Please enter 1 if correct, Enter 0 to try again:  ");
           x = scanner1.nextInt();

           System.out.println("\n");

       } while (x==0);

       System.out.println("Please enter number of family members including you: ");
       fm = scanner1.nextInt();
       System.out.println("You entered " + fm + "");

       System.out.println("Please enter your total income for the year: ");
       income = scanner1.nextInt();

       System.out.println("You entered " + income + "");
   }}  

I converted all of the Strings to ints, for the next step I need to know what kind of table I should be using for this data.I'm going to need to use the income and family members to compute amounts and display them in a table along with the name and ID number.
 
Sean Stevens
Ranch Hand
Posts: 47
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sean Stevens wrote:

Carey Brown wrote:You are part way there. You'd have to change the variables for amounts from a String to an int or a double. Then you'd have to use nextInt() or nextDouble() to get the keyboard input.

There is an issue with getting keyboard input using one of the nextXXX() methods other than nextLine(). "nextInt()", for example, will leave a new-line character pending in the buffer at the point it returns the int. This is one of the sucky parts of dealing with a Scanner.




I converted all of the Strings to ints, for the next step I need to know what kind of table I should be using for this data.I'm going to need to use the income and family members to compute amounts and display them in a table along with the name and ID number.



 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried running it? Probably not because it wouldn't work as written.

When writing code use these steps to keep your project from getting out of control:
  • Write just a few lines (5-10) of code.
  • Debug it till it compiles.
  • Run it and fix any runtime problems: exceptions, invalid output.
  • [repeat]
  •  
    Sean Stevens
    Ranch Hand
    Posts: 47
    Netbeans IDE Chrome Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Carey Brown wrote:Have you tried running it? Probably not because it wouldn't work as written.

    When writing code use these steps to keep your project from getting out of control:

  • Write just a few lines (5-10) of code.
  • Debug it till it compiles.
  • Run it and fix any runtime problems: exceptions, invalid output.
  • [repeat]


  • It ran fine though. Exactly the output I needed besides the table portion.
     
    Carey Brown
    Saloon Keeper
    Posts: 10687
    85
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Sean Stevens wrote:It ran fine though.


    If you ran the code you just posted then it didn't run fine.

    Run the code and enter the following:
    joe smith
    1234
    1
    5
    100
     
    Sean Stevens
    Ranch Hand
    Posts: 47
    Netbeans IDE Chrome Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Carey Brown wrote:

    Sean Stevens wrote:It ran fine though.


    If you ran the code you just posted then it didn't run fine.

    Run the code and enter the following:
    joe smith
    1234
    1
    5
    100



    run:
    Enter your first and last name:
    joe smith
    You entered joe smith
    Enter your 4 digit ID number:
    1234
    You entered 1234
    Please enter 1 if correct, Enter 0 to try again:  
    1
    Please enter number of family members including you:
    5
    You entered 5
    Please enter your total income for the year:
    100
    You entered 100

    Here's the full code

     
     
    Sean Stevens
    Ranch Hand
    Posts: 47
    Netbeans IDE Chrome Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Carey Brown wrote:

    Sean Stevens wrote:It ran fine though.


    If you ran the code you just posted then it didn't run fine.

    Run the code and enter the following:
    joe smith
    1234
    1
    5
    100



    What makes you think it doesn't work properly?
     
    Carey Brown
    Saloon Keeper
    Posts: 10687
    85
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Sean Stevens wrote:What makes you think it doesn't work properly?


    You are not running the code you posted. Look at line 6.
     
    Sean Stevens
    Ranch Hand
    Posts: 47
    Netbeans IDE Chrome Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Carey Brown wrote:

    Sean Stevens wrote:What makes you think it doesn't work properly?


    You are not running the code you posted. Look at line 6.



    Maybe I posted the wrong code earlier. This is the current source code in my IDE and it runs correctly.

     
    Carey Brown
    Saloon Keeper
    Posts: 10687
    85
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Sean Stevens wrote:

    Carey Brown wrote:

    Sean Stevens wrote:What makes you think it doesn't work properly?


    You are not running the code you posted. Look at line 6.


    Maybe I posted the wrong code earlier. This is the current source code in my IDE and it runs correctly.


    Impossible. You are not running the code you posted. Did you even look at line 6?
    You could put the IDE away for a moment and try running the compiler from the command line and then try executing it from the command line.

    When I run your code...
     
    Sean Stevens
    Ranch Hand
    Posts: 47
    Netbeans IDE Chrome Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Carey Brown wrote:

    Sean Stevens wrote:

    Carey Brown wrote:

    Sean Stevens wrote:What makes you think it doesn't work properly?


    You are not running the code you posted. Look at line 6.


    Maybe I posted the wrong code earlier. This is the current source code in my IDE and it runs correctly.


    Impossible. You are not running the code you posted. Did you even look at line 6?
    You could put the IDE away for a moment and try running the compiler from the command line and then try executing it from the command line.

    When I run your code...



    What's wrong with line 6?
     
    Marshal
    Posts: 28177
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Line 6:



    The prompt before you set that value says "Enter your first and last name: " -- is your first and last name acceptable to assign to that variable? Mine isn't.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic