PDA

View Full Version : Two Syntax errors




fishcrackers
Jan 22, 2008, 06:08 PM
I am having a problem with my program and I need some help. Anything would be greatly appreciated. I am running a guessing game.

Syntax error, insert "while ( Expression ) ;" to complete DoStatement
Syntax error, insert "}" to complete MethodBody

Here is my code:

import java.util.Scanner;
import java.util.Random;

public class Driver
{
public static void main(String[] args)
{
int z;
Double answer;
Double y;
Double n;
Scanner stdIn;
Random rand = new Random();
int x = rand.nextInt(100)+1;
do
{
System.out.println("Please guess a random number;");
z = stdIn.nextInt();
while(z != x)
{
if(z>x)
{
System.out.println("Too High. Guess Again:");
z = stdIn.nextInt();
}
else if(z<x)
{
System.out.println("Too Low. Guess Again:");
z = stdIn.nextInt();
{
System.out.println("Thats Correct!! Play again y or n?");
answer = stdIn.nextDouble();

}

}while(answer == y);

System.out.println("Thanks for playing!");
}

}



Aranince
Jan 22, 2008, 06:14 PM
Yay for indenting. Use [ code ] [ /code ] tags next time.

Your else if...the end bracket is { instead of }


public class Driver
{
public static void main(String[] args)
{
int z;
Double answer;
Double y;
Double n;
Scanner stdIn;
Random rand = new Random();
int x = rand.nextInt(100)+1;

do
{
System.out.println("Please guess a random number;");
z = stdIn.nextInt();
while(z != x)
{
if(z>x)
{
System.out.println("Too High. Guess Again:");
z = stdIn.nextInt();
}
else if(z<x)
{
System.out.println("Too Low. Guess Again:");
z = stdIn.nextInt();
{
System.out.println("Thats Correct!! Play again y or n?");
answer = stdIn.nextDouble();

}

}while(answer == y);

System.out.println("Thanks for playing!");
}

}

fishcrackers
Jan 22, 2008, 06:21 PM
Thanks for the help with that now I have another problem.

The constructor Scanner() is undefined

[ code ]
import java.util.Scanner;
import java.util.Random;

public class Driver
{
public static void main(String[] args)
{
int z;
Double answer;
Double y;
Double n;
Scanner stdIn = new Scanner();
Random rand = new Random();
int x = rand.nextInt(100)+1;
do
{
System.out.println("Please guess a random number;");
z = stdIn.nextInt();
while(z != x)
{
if(z>x)
{
System.out.println("Too High. Guess Again:");
z = stdIn.nextInt();
}
else if(z<x)
{
System.out.println("Too Low. Guess Again:");
z = stdIn.nextInt();
}
System.out.println("Thats Correct!! Play again y or n?");
answer = stdIn.nextDouble();

}

}while(answer == y);

System.out.println("Thanks for playing!");
}

}
[ /code ]

gnasher729
Jan 22, 2008, 06:33 PM
Thanks for the help with that now I have another problem.

The constructor Scanner() is undefined


Well, yes. If you look at the documentation of java.util.Scanner, can you find any constructor with no arguments?

fishcrackers
Jan 22, 2008, 06:41 PM
Got that one now for another problem. At the end I want them to either enter an "y" or an "n". Depending on if they want to run the program again. I need help doing that.

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The local variable y may not have been initialized
The local variable n may not have been initialized
The local variable a may not have been initialized

at Driver.main(Driver.java:10)

[code]import java.util.Scanner;
import java.util.Random;

public class Driver
{
public static void main(String[] args)
{
int z;
Double a;
Double y = y;
Double n = n;
Scanner stdIn = new Scanner(System.in);
Random rand = new Random();
int x = rand.nextInt(100)+1;
do
{

System.out.println("Please guess a random number;");
z = stdIn.nextInt();
while(z != x)
{
if(z>x)
{
System.out.println("Too High. Guess Again:");
z = stdIn.nextInt();
}
else if(z<x)
{
System.out.println("Too Low. Guess Again:");
z = stdIn.nextInt();
}
System.out.println("Thats Correct!! To play again press y or n to exit.");
a = stdIn.nextDouble();

}

}while(a == y);

System.out.println("Thanks for playing!");
}

}[/code}

fishcrackers
Jan 22, 2008, 06:46 PM
Got that one now for another problem. At the end I want them to either enter an "y" or an "n". Depending on if they want to run the program again. I need help doing that.

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The local variable y may not have been initialized
The local variable n may not have been initialized
The local variable a may not have been initialized
at Driver.main(Driver.java:10)

import java.util.Scanner;
import java.util.Random;

public class Driver
{
public static void main(String[] args)
{
int z;
Double a;
Double y = y;
Double n = n;
Scanner stdIn = new Scanner(System.in);
Random rand = new Random();
int x = rand.nextInt(100)+1;
do
{

System.out.println("Please guess a random number;");
z = stdIn.nextInt();
while(z != x)
{
if(z>x)
{
System.out.println("Too High. Guess Again:");
z = stdIn.nextInt();
}
else if(z<x)
{
System.out.println("Too Low. Guess Again:");
z = stdIn.nextInt();
}
System.out.println("Thats Correct!! To play again press y or n to exit.");
a = stdIn.nextDouble();

}

}while(a == y);

System.out.println("Thanks for playing!");
}

}

markild
Jan 22, 2008, 06:51 PM
You should definitly use String or char to store the y/n values (char would probably be easier, seeing it's simple to make comparisons.).

Also, there's no need to store the y and n values, seeing as you only compare them one time in your code, so:a == 'y' should probably do.

I might be a good idea to take a look at some documentation about types, btw.

fishcrackers
Jan 22, 2008, 06:56 PM
Thanks, that helped make it run but when I got to run the program again or end it i get this error message.

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at Driver.main(Driver.java:34)

markild
Jan 22, 2008, 07:00 PM
Try this instead of the stdIn.nextDouble line
a = stdIn.next().charAt(0);

fishcrackers
Jan 22, 2008, 07:06 PM
I then got another error. It must be something else. Thank you though. Anyone please...

fishcrackers
Jan 22, 2008, 07:31 PM
I need help making my program repeat and or end. I give the user the option to run the program again or to end it. When I in a y for yes or n for n. I get this error message.

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at Driver.main(Driver.java:34)

import java.util.Scanner;
import java.util.Random;

public class Driver
{
public static void main(String[] args)
{
int z;
Double a;
Double y;
Double n;
Scanner stdIn = new Scanner(System.in);
Random rand = new Random();
int x = rand.nextInt(100)+1;
do
{

System.out.println("Please guess a random number:");
z = stdIn.nextInt();
while(z != x)
{
if(z>x)
{
System.out.println("Too High. Guess Again:");
z = stdIn.nextInt();
}
else if(z<x)
{
System.out.println("Too Low. Guess Again:");
z = stdIn.nextInt();
}
}
System.out.println("Thats Correct!! To play again press y or n to exit.");
a = stdIn.nextDouble();

}while('a' == 'y');


System.out.println("Thanks for playing!");
}

}

HiRez
Jan 22, 2008, 07:38 PM
It's been a long time since I've used Java, but what is this code supposed to be doing?

Double y = y;
Double n = n;It looks like you're assigning something to itself (when it doesn't exist yet). Do you want to assign the integer value of the characters 'y' and 'n'? Why are you using a Double type? Maybe I don't understand it but that assignment code seems fishy to me.

eddietr
Jan 22, 2008, 07:44 PM
Is this your third thread on the same little program? :confused:

I don't see any reason for the following:

Double y
Double n

You don't need those two references, they don't seem to used anywhere.

Then the next issue is why are you reading the response to your question as a double?

and lastly you have a problem in the conditional ('a' == 'y'). That is comparing the literal 'a' against a literal 'y', which will always be false.

What you probably meant was (a == 'y'). But you really probably want (a.equals("y")) with 'a' as a String.

Or use an InputStreamReader instead to read a single char.

fishcrackers
Jan 22, 2008, 07:51 PM
I can enter 1 or 2 to end the program but it always ends the program and goes with the last "thanks for playing" I cannot get it to repeat the program

import java.util.Scanner;
import java.util.Random;

public class Driver
{
public static void main(String[] args)
{

int a;
int y = 1;
int z = 0;
Scanner stdIn = new Scanner(System.in);
Random rand = new Random();
int x = rand.nextInt(100)+1;
do
{

System.out.println("Please guess a random number:");
z = stdIn.nextInt();
while(z != x)
{
if(z>x)
{
System.out.println("Too High. Guess Again:");
z = stdIn.nextInt();
}
else if(z<x)
{
System.out.println("Too Low. Guess Again:");
z = stdIn.nextInt();
}
}
System.out.println("Thats Correct!! To play again press 1 or 2 to exit.");
a = stdIn.nextInt();

}while(a == 'y');


System.out.println("Thanks for playing!");
}

}

gnasher729
Jan 22, 2008, 07:56 PM
I think at some point you should really try to figure out a few things yourself. But could you please answer the following questions:

1. What is the difference between

a

and

'a'

and

"A"

2. What is the meaning of double and what is the meaning of Double?

If you can answer these questions, then I think you will get a lot lot further, and you don't need to do "programming by guessing" anymore.

eddietr
Jan 22, 2008, 07:56 PM
Dude, no one is going spend the time to chase down every thread you're creating to help you.

Stick to one thread, please. So people can get the context of what you're trying to do. Plus at this rate you'll have 20 threads created tonight.

The reason your program always ends is because the conditional is still (a='y') but now you've switched to ints (1 or 2).

You need (a=1) or (a=2). It's not clear whether you want the user to type 1 or 2 to continue.

jsw
Jan 22, 2008, 08:21 PM
<threads merged>