This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Question about restart Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about restart" Watch "Question about restart" New topic
Author

Question about restart

Karl Collins
Greenhorn

Joined: Nov 22, 2011
Posts: 21
I'm very new to programming, I have made a program that successfully loops and I would like the program at the end to say to the user, would you like to restart the program? If "y" type yes if no type "n". I've been trying for several hours now but all the tutorials I look up have something I don't understand. Nor does it work in my program. Any help would be greatly appreciated.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Hi Karl - Please post the code and tell how the code behaves and what you expected.
Karl Collins
Greenhorn

Joined: Nov 22, 2011
Posts: 21
John Jai wrote:Hi Karl - Please post the code and tell how the code behaves and what you expected.


Hi John! Thank you for the reply, I know this is annoying but I fear I may get into trouble if I post the code as this is for a school assignment.

If you cant help me without the code I understand. The best I can explain is that when my program ends instead of saying :Press any key to continue . . ." I want the program to say "Type "y" to restart the program or type "n" to end the program."

Thank you
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
No issues - You might look at using either of the while or do while loops - http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

Easy way is to write your code within a do while loop and at the near end of the loop, ask the user intention to continue or not. Read what the user inputs and check it in the while condition.



I hope so you can post at least this part of the code if you have issues after trying.
Karl Collins
Greenhorn

Joined: Nov 22, 2011
Posts: 21
I did try using what you said an looked at the examples although I'm not sure if i'm doing it correctly. I receive this error:

C:\Users\xxxx\Desktop\xxxMock.java:50: cannot find symbol
symbol : class Scanner
location: class Mock
Scanner sc = new Scanner(System.in);
^
C:\Users\xxxx\Desktop\Mock\Mock.java:50: cannot find symbol
symbol : class Scanner
location: class Mock
Scanner sc = new Scanner(System.in);
^
2 errors

Tool completed with exit code 1

I could pm the code to you if you don't mind looking at it from there. Thanks for the help.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
You are probably missing the import statement that tells the compiler to use the Scanner class before your class declaration. Please read on importing members -> http://docs.oracle.com/javase/tutorial/java/package/usepkgs.html

Note that the first line in the below code imports the Scanner class.



And please always UseTheForumNotEmail.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38414
    
  23
Karl Collins wrote: . . . I could pm the code to you if you don't mind looking at it from there. Thanks for the help.
Please don’t send code by PM, but post it where everybody can see it and help.
The error message you have looks as if you had forgotten to import the Scanner class.

And welcome to the Ranch
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38414
    
  23
John Jai, why are you creating Scanner objects inside that do loop?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Campbell Ritchie wrote:John Jai, why are you creating Scanner objects inside that do loop?

Ah.. I shouldn't do that... thanks for correcting
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38414
    
  23
Let’s change your code to incorporate that amendment:-
Karl Collins
Greenhorn

Joined: Nov 22, 2011
Posts: 21
Ok!

Sorry, I don't mean to be ignorant but I don't understand

I made a mock up program to show roughly what i'm doing

Can't I just use a Loop without the Scanner code?



After my loop goes 10 times I want the program to say Go again? Yes or No.
And if they they say yes I want it to start again. But if no I want it to system exit.



EDIT: I was using all 'int's to make the mock simple
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38414
    
  23
Don’t use System.exit() if possible; it is a bit vicious. Your application will exit when it reaches the end of the main thread, which in your case means when you go beyond the last line in the main method.
Have a look at the while test, and what values of loop you have. Also look through the loop and see how that value changes.
If you are going back to the start, you cannot say “go to” or similar; you would have to be inside another loop. Similar to what John Jai showed you earlier today.
Did you write the EasyIn class yourself? Have you been told to use it? Unless the answer to one of those questions is “yes”, I suggest you go back to Scanner.
Karl Collins
Greenhorn

Joined: Nov 22, 2011
Posts: 21
We were told to use the EasyIn program. Does that mean I cant use Scanner also?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Karl Collins wrote:We were told to use the EasyIn program. Does that mean I cant use Scanner also?


It looks like it's an alternative to Scanner. If you're using one, you probably don't need the other. If your teacher told you to use EasyIn, then use it. If you'd rather use Scanner, as your teacher if that's acceptable.

Also, just FYI, it's a library, not a program.
Karl Collins
Greenhorn

Joined: Nov 22, 2011
Posts: 21
Oh ok I get what you mean, Thanks.

I have changed the code to a do while loop like here,


Heres is what I added to attempt to loop it. I know this is way off though but I'll post it anyway because its all I can come up with.



This was what I added to the end.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38414
    
  23
I think you may have to be introduced to some awkward syntax.There is similar code for reading a file with a BufferedReaderAnd you need the round brackets () around the = to raise its precedence above the !=. I have omitted the necessary exception handling, and closing the Reader, for simplicity’s sake.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about restart