Hi,
I made a program in java here is the code:
import java.util.Scanner;
class apples{
public static void main(String args[]) {
Scanner x = new Scanner(System.in);
int bucky = 6;
System.out.println("Try to Guess What Number I am.");
if (bucky == x){
System.out.println("Congradulations, you guessed it.");
}else{
if (bucky >= x){
System.out.println("Your Wrong Try Again. The Number is > the number you guessed.");
}else{
if (bucky <= x){
System.out.println("Your Wrong Try Again. The Number is < the number you guessed.");
}
}
}
}
}
I am working with eclipse and it gives me this error:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Incompatible operand types int and Scanner
The operator >= is undefined for the argument type(s) int, Scanner
The operator <= is undefined for the argument type(s) int, Scanner
at apples.main(apples.java:10)
I get that my code is wrong at line 10, but I don't know how to fix it. I am new to this. Please Help!
Offline
Hello Again,
I edited my program and it still doesn't work help.
import java.util.Scanner;
class apples{
public static void main(String args[]) {
Scanner x = new Scanner(System.in);
int num = 6;
System.out.println("Try to Guess What Number I am.");
switch (x){
case (x == num):
System.out.println("Congradulations, you guessed it.")
brake;
case (x <= num):
System.out.println("Your Wrong Try Again. The Number is < the number you guessed.");
case (x >= num):
System.out.println("Your Wrong Try Again. The Number is > the number you guessed.");
}
}
}
I finally understand that I can't do this with the scanner, but is there another way to do this? Please Help. Thanks!
Offline
x itself isn't an integer value - it's a reference to the object of the Scanner class that you created. So naturally, you can't check its value against that of an integer, because it doesn't have a numerical value in that sense.
Instead, what you want is a method from the Scanner class that takes the next parseable integer - that is, the next integer entered by the user that it can find. What you want to do is to create a variable of type int and make it equal to "x.nextInt()", then check that against the num variable.
I hope that makes sense. I'm not terribly good at explaining things. :S
Offline
Try heading over to the official topic. However, since I am the owner of the official topic, I'll try to help you here. Actually, Harakou is right, and he already answered it. I went through a whole long thing about this about how to get a string, then realizing that it was completely unnecessary XD
Offline
16Skittles wrote:
Try heading over to the official topic. However, since I am the owner of the official topic, I'll try to help you here. Actually, Harakou is right, and he already answered it. I went through a whole long thing about this about how to get a string, then realizing that it was completely unnecessary XD
Offline