• Post Reply Bookmark Topic Watch Topic
  • New Topic

Prints more than one Value for String.  RSS feed

 
Arthur Brown
Greenhorn
Posts: 2
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK Hello All. I think I have a simple question.
Where I have System.out.println(pw);
If you put a wrong password in 3 times it seems like it will hold more than one value in pw.
Can someone explain what I am doing wrong here please.
I am fairly new to Java and this is just confusing me. Thanks in Advance.
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arthur Brown wrote:If you put a wrong password in 3 times it seems like it will hold more than one value in pw.


Can you explain what makes you think that? It would help if you told us what you input, and then what the resulting output was. And what you expected instead, which I suppose would be different.
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch!
 
Arthur Brown
Greenhorn
Posts: 2
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first time i typed in 111.
The second time i typed in 111.
The third time i typed in 111.
Then i typed in 1234Acb.
at the line with System.out.println(pw) it gave me
111
111
111
1234Acb.
I am just wondering why would it hold these inputs and print them all out at once.
I always thought that value would be overwritten by the newest or wither it will point to some where different.
If there is more information that I need to add I will try.
Again thanks for the replies guys. You all are wonderful.
I was hoping that it would only print out the last value 1234Acb.
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not as complicated as that. It's much simpler: every time you enter something, it's printed out on the console. So you typed in 111, and then 111 got written out by System.out.println. Then you typed in 111 again, and again 111 got written out. And so on.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
== true or == false?? Never write that sort of thing. It’s if (b) or if (!b). Using the == operator is poor style and very error-prone if you mistakenly write = instead. Also give real names to your local variables, not x y z.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest your big block of if-elses should be simplified greatly, to three simple ifs. And use a StringBuilder to put together Strings in parts.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!