Reputation: 147
I am writing a program for my class, and I want to test it out. However, I cannot compile it since I am getting this error "error: cannot find symbol" at "menuChoice = kb.nextInt();"
import java.util.*;
public class Program3
{
public static void main (String [ ] args)
{
Scanner kb = new Scanner(System.in);
System.out.println("Please enter a non-negative integer.: ");
int num = kb.nextInt();
Random rand = new Random();
int sum = 0;
int factor = 1;
int menuChoice;
while (num > 0)
{
System.out.println("Number cannot be negative! Please enter a non-negative integer.: ");
num = kb.nextInt();
}
do
{
System.out.println("\nPlease choose an option:");
System.out.println("\t0\tPrint the number");
System.out.println("\t1\tDetermine if the number is odd or even");
System.out.println("\t2\tFind the reciprocal of the number");
System.out.println("\t3\tFind half of the number");
System.out.println("\t4\tRaise the number to the power of 5 (using a Java method)");
System.out.println("\t5\tRaise the number to the power of 5 (using a loop)");
System.out.println("\t6\tGenerate 20 random numbers between 0 and the number (inclusive)");
System.out.println("\t7\tFind the sum of 0 up to your number (using a loop)");
System.out.println("\t8\tFind the factorial of the number (using a loop)");
System.out.println("\t9\tFind the square root of the number (using a Java method)");
System.out.println("\t10\tFind the square root of the number (using a loop, Extra Credit)");
System.out.println("\t11\tDetermine whether the number is prime (using a loop, Extra Credit) ");
System.out.println("\t12\tExit the program");
menuChoice = kb.nextlnt(); //<<< error occurs right here!!!
switch (menuChoice)
{
case 0: System.out.print("Your number is" + num);
break;
case 1: if (num % 2 ==0)
System.out.print(num + " is even");
else
System.out.print(num + " is false");
break;
case 2: if (num == 0)
System.out.print("There are no reciprocal");
else
System.out.print("The reciprocal of " + num + " is 1/" + num);
break;
case 3: System.out.print("half of " + num + " is" + (num/2));
break;
case 4: System.out.print(num + " of the power of 5 is" + (Math.pow(num , 5)));
break;
case 5: for (int i = 1 ; i <= 6 ; i++)
for (int j = 1 ; j <= 6 ; j ++)
System.out.print(num + " of the power of 5 is" + ( i * j));
break;
case 6: for (int i = 1 ; i<=20; i++)
System.out.println(rand.nextInt(6));
break;
case 7: for (int i = 1; i <=num ; i++)
{
sum += 1;
System.out.println(sum);
}
break;
case 8: for (int i = 1 ; i <=num; i++)
factor = factor*i;
System.out.println(factor);
break;
case 9: double theSqrt = Math.sqrt(num);
System.out.println("The square root of " + num + " is " + theSqrt);
break;
case 10: System.out.println("Did not do this extra credit :(");
break;
case 11: boolean isPrime;
for (int i = 2; i<=num; i++)
{
isPrime = true;
for (int divisor = 2; divisor<Math.sqrt(num) && isPrime; divisor++)
{
if (num%divisor == 0)
isPrime = false;
}
if (isPrime)
System.out.println("The prime number of" + num + " is prime");
}
break;
case 12: System.out.println("Exiting the now.");
break;
default: System.out.println("Illegal choice, try again");
break;
}
}while (menuChoice !=12);
}
}
That is the only part that is preventing me from running the program and seeing if my code is correcly written.
Thank you for the help.
Upvotes: 0
Views: 1579
Reputation: 37083
You have a typo in your code. There is no such method like nextlnt
in Scanner.
menuChoice = kb.nextlnt();
^
You should change it to:
menuChoice = kb.nextInt();
^
Also look at this while condition:
while (num > 0)
Which means if num is greater than 0 then you are treating it as negative number. So instead you should have you while like:
while (num < 0)
Upvotes: 1
Reputation: 11973
you just change the
menuChoice = kb.nextlnt();
to
menuChoice = kb.nextInt();
then your code will work properly.
Upvotes: 2