restart void main?

class Randoms{

publicstaticvoid main ( String args[] )throws java.io.IOException{

System.out.print("Choose a button and click it...");

char choice = (char) System.in.read();

if (choice =='a')

System.out.println("LYKE TEH OMFG U WIN!");

if (choice !='a')

System.out.println("You lose! Who in the hell would pick " + choice +"?");

}

}

alright at the end of the main void i want it so if you pick the wrong answer it restarts from the beginning...how would i go about doing that

[1107 byte] By [ByronTheOmnipotenta] at [2007-11-27 8:40:48]
# 1
Put the whole thing in a loop.
floundera at 2007-7-12 20:39:24 > top of Java-index,Java Essentials,New To Java...
# 2

Put it in a loop.

char choice

do {

choice = whatever...

processChoice(choice);

} while (choice != 'a');

jverda at 2007-7-12 20:39:25 > top of Java-index,Java Essentials,New To Java...
# 3

this is what i have now

class Randoms {

public static void main ( String args[] ) throws java.io.IOException {

char choice;

do {

System.out.print("Choose a button and click it...");

choice = (char) System.in.read();

if (choice == 'a')

System.out.println("LYKE TEH OMFG U WIN!");

if (choice != 'a')

System.out.println("Loser! Who in the hell would pick " + choice + "?");

} while (choice != 'a');

}

}

but the loser message shows up 3 times for some reason

ByronTheOmnipotenta at 2007-7-12 20:39:25 > top of Java-index,Java Essentials,New To Java...
# 4
Don't use System.in.read, for one thing. Use Scanner or BufferedReader (probably the former is better here). http://java.sun.com/docs/books/tutorial/essential/io/index.html
jverda at 2007-7-12 20:39:25 > top of Java-index,Java Essentials,New To Java...
# 5
no i know; the loop works fine it just displays the losing message 3 timesMessage was edited by: ByronTheOmnipotent
ByronTheOmnipotenta at 2007-7-12 20:39:25 > top of Java-index,Java Essentials,New To Java...
# 6
You know what? Better than jverd?Try following his advice and most likely that behaviour will disappear. If not, post your new code.
floundera at 2007-7-12 20:39:25 > top of Java-index,Java Essentials,New To Java...
# 7

> no i know; the loop works fine

No, it doesn't. It's being called three times for three different characters every time you type a character and then hit enter. If you did it on Linux, it would only be called twice.

Add this line immediately after you read choice from System.in

System.out.println((int)choice);

jverda at 2007-7-12 20:39:25 > top of Java-index,Java Essentials,New To Java...
# 8
> no i knowNo, you don't.
Navy_Codera at 2007-7-12 20:39:25 > top of Java-index,Java Essentials,New To Java...
# 9

what about using scanner like this?

class Randoms

{

private static Scanner scan = new Scanner(System.in);

public static void main(String args[]) throws java.io.IOException

{

char choice;

do

{

System.out.print("Choose a button and click it...");

choice = scan.nextLine().charAt(0);

.............

}

while (choice != 'a');

}

}

Or is this not a smart way to do this?

Message was edited by:

petes1234

petes1234a at 2007-7-12 20:39:25 > top of Java-index,Java Essentials,New To Java...
# 10

> what about using scanner like this?

> > class Randoms

> {

> private static Scanner scan = new

> Scanner(System.in);

>

> public static void main(String args[]) throws

> java.io.IOException

>{

>char choice;

>do

>{

> System.out.print("Choose a button and click

> it...");

>choice = scan.nextLine().charAt(0);

> .............

>}

> while (choice != 'a');

>}

>

>

>

> Or is this not a smart way to do this?

>

> Message was edited by:

> petes1234

That would be what jverd was suggesting in reply #4.

Navy_Codera at 2007-7-12 20:39:25 > top of Java-index,Java Essentials,New To Java...
# 11
> That would be what jverd was suggesting in reply #4.Indeed it is. My apologies to jverd.
petes1234a at 2007-7-12 20:39:25 > top of Java-index,Java Essentials,New To Java...