• Post Reply Bookmark Topic Watch Topic
  • New Topic

While Loop Error  RSS feed

 
Saad Zahoor
Ranch Hand
Posts: 82
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question :  i want to make a program that will take the name of guest from user and when user input required information.
Error : After type specified command the loop should end  >> command "done"  .. how to declare this type of thing
Getting Error :  at while loop

 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are seeing an error because the local variable names has not been initialized.
 
Saad Zahoor
Ranch Hand
Posts: 82
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then How to SOLVE it ???
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about it. Hint: is a straight while loop the best for entering things from the keyboard?
Don't write \n unless somebody says they want the LF character. Use println or printf("...%n...") instead.
And please explain under which circumstances your loop will terminate.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Saad Zahoor
Ranch Hand
Posts: 82
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question :
how do i get the output in the form of list not in arrays ..
my output is like >>>>  {"name 1" , "name 2" , "name 3"}
but i want the output like
name 1
name 2
name 3


 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
change to

you should have added to your old topic and not created yet another one.

You have also been warned not to use "\n".
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I shall see if I can merge the two discussions.
Also (not quite sure why): don't write f:\\myFile. Use / like this: f:/myFile. Even on Windows boxes. The JVM can convert the / to \ automatically depending on the operating system.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your second version of the do loop will work nicely, but I suggest you add statements to prompt the user for the next input.
Don't add line end characters to the Strings to be stored in the List. Did you know you can print absolutely anything with System.out.println? Any type of primitive, null, any type of object. You can print everything. Even a List. I think the same applies to any PrintStream object. CB has explained how you can print the names on separate lines.

An alternative to do loops. You will have to read the name (not names) into the loop directly. If you are using a Scanner, it will never return null so you don't need to write "done" first. The only Scanner methods that return null have names starting find. There are two of them. In all other cases, if there is no input, the Scanner throws an Exception instead. What you have to do is read the input into the name and compare it with something else. Since the reading involves the = operator, which has a low precedence, it must be wrapped in ().Note that version will read Saad Zahoor as two names.
If you want five people, why not use a for loop, since you can use counter‑controlled repetition.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saad Zahoor wrote:. . .
What a dreadful name for a print stream. It is really dangerous to have an empty catch block because you won't know if anything goes wrong.
[edit: add the following]Use try with resources instead of explicitly closing the print stream.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It is really dangerous to have an empty catch block because you won't know if anything goes wrong.


That's generally true and worth pointing out. However...

The API docs for PrintWriter wrote:Methods in this class never throw I/O exceptions


and

The client may inquire as to whether any errors have occurred by invoking checkError().


So to me, that means you should avoid using PrintWriter for serious work unless you're willing to write the code which calls checkError() frequently to make sure you're still on the rails.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hadn't realised that. It sounds a bit like Formatter and Scanner which each have an ioException() method. Any such Exceptions are consumed by the Scanner/Formatter and you would have to call that method to find whether an Exception had been caught.If you simply write throw f.ioException(); without the if, it usually returns null because IOExceptions are unusual and you would suffer an NPE.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!