Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about restart

 
Karl Collins
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Karl - Please post the code and tell how the code behaves and what you expected.
 
Karl Collins
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48943
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48943
60
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai, why are you creating Scanner objects inside that do loop?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48943
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let’s change your code to incorporate that amendment:-
 
Karl Collins
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48943
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We were told to use the EasyIn program. Does that mean I cant use Scanner also?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48943
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic