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".
This is the reason for "while zero".press 0 if they need to reenter the code or 1 to advance
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.
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.
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:-
Carey Brown wrote:Two steps forward, one step back. You undid some of the other suggestions.
True, but what was the purpose of incrementing it in the first place?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.
Carey Brown wrote:Remove line 27. I said that that line should be moved but instead you copied it.
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.
You are still trying to get input before giving the user a prompt.Sean Stevens wrote:Is this the proper order?
Carey Brown wrote:
You are still trying to get input before giving the user a prompt.Sean Stevens wrote:Is this the proper order?
Sean Stevens wrote:
Carey Brown wrote:
You are still trying to get input before giving the user a prompt.Sean Stevens wrote:Is this the proper order?
But putting that line anywhere else results in the IDE saying the variable hasn't been initialized.
Carey Brown wrote:
Sean Stevens wrote:
Carey Brown wrote:
You are still trying to get input before giving the user a prompt.Sean Stevens wrote:Is this the proper order?
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.
Carey Brown wrote:Now line 8 is missing. This is where you're supposed to get user input.
Carey Brown wrote:Persistence wins!
This statement is syntactically correct but grammatically incorrect. Just tweak the wording.
System.out.println("value of id : " + x);
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.
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 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]
Sean Stevens wrote:It ran fine though.
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
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
Sean Stevens wrote:What makes you think it doesn't work properly?
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.
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.
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...
Is this the real life? Is this just fantasy? Is this a tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|