Array Cannot Find Symbol-Java [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am still new to coding but I get a cannot find symbol
I don't know much about arrays but I have been looking on the internet but I cannot find why
import java.util.Scanner;
public class Alphabet {
public static void main(String[] args) {
String[] alpha = new String[26];
alpha[0] = a;
System.out.println("PLease enter a number from 0-25:");
Scanner input = new Scanner(System.in);
int userInput = input.nextInt();
}
}

alpha[0] = a;
Variable a is not declared, that's why you are getting the error.

Generally Cannot find symbol error occurs when compiler not able to resolve the symbol.
Here a can not be resolved by the compiler in your code at line
alpha[0]= a;
So you are getting error.
Declare it like this
String a = "hello";
alpha[0] = a;
or
alpha[0] = "a";

Related

Why isn't this recursion working in Java? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to calculate the factorial of the integer user inputs, but it does not return anything. Why?
Thanks.
import java.util.Scanner;
`class App {
public static int factorial(int n) {
if (n == 0) {
return 1;
} else {
int recurse = factorial(n - 1);
int result = n * recurse;
return result;
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer to calculate its factorial: ");
int users = input.nextInt();
factorial(users);
}
}
The problem in your code is that you are have not giving a print statement for displaying factorial of the number entered. Just returning a value will not print it. If you are working in BlueJ environment, you can only use the code by directly executing the method factorial. Thank You.
The problem in your code is that , you are have not given a print statement for >displaying factorial of the number entered.

what's wrong with my java program? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
import java.util.Scanner;
public class NumOne {
public static void main(String[] args) {
Scanner ley = new Scanner(System.in);
boolean num2;
int num;
System.out.print("Enter number: ");
num = ley.nextInt();
}
public boolean isPositive(boolean num) {
if (a > 0) {
System.out.print("positive");
} else {
System.out.print("negatinve");
}
return num;
}
C:\Users\nimzkie\Desktop\NumOne.java:15: cannot find symbol
symbol : variable a
location: class NumOne
if(a>0){
^
1 error
Process completed.
The error message is very specific: You are using a in your if statement, but Java has no idea what a is. You probably meant to say if(num > 0). And you don't mean boolean num, you mean int num.
Here are the reasons for that error: http://java.about.com/od/cerrmsg/g/Definition-Cannot-Find-Symbol.htm
You did not declare variable a.
int a = 0;

Do while with try catch JAVA validation [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm having a trouble figuring out how to solve this issue when it just stops when I call this method when I input letters and other types. What is the problem with my code? Can anyone help? Thanks in advance!
Here is my code:
public int selectOption(int maxRange, String sourceType) throws IOException
{
do
{
try
{
userInput = input.nextInt();
}//end try
catch(Exception e)
{
validInput=false;
input.next();
}//end catch
if (userInput<1 || userInput>maxRange)
{
MovieHouse.clearScreen();
System.out.println("Select from the options only!");
loadHeader();
if(sourceType.equals("main"))
MovieHouse.homeMenu();
else if(sourceType.equals("movie menu"))
loadMovieMenu();
}//end if
}while(userInput<1 || userInput>maxRange || validInput==false);
return userInput;
}//end selectOption
"input letters" - Does it mean normally the program take digits and you are providing char as input.
Not clear what is your issue.

simple java user input, where did i go wrong?? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hi guys im working on a basic java program to get somebodys name, it comes up with a lot of errors could you correct me where i went wrong. Sorry and thanks. I also need to basicly set the user input to a string variable which i can then later store into a text file.
import java.io.Console;
import java.util.Scanner;
public class HelloJello {
public static void main(String[] args) {
System.out.print("Hi using this program i will find out"
+ "what your name is and store it in document");
Scanner scanner = new Scanner (System.in);
System.out.print("Enter your name");
name = scanner.next(); // Get what the user types.
}
}
what is name ?
You need to declare the variable reference properly
You need to declare data type of name. It should be a String.
public static void main(String[] args) {
String name=""; // always good to initialize
System.out.print("Hi using this program i will find out"
+ "what your name is and store it in document");
Scanner scanner = new Scanner (System.in);
System.out.println("Enter your name");
name = scanner.nextLine(); // Get what the user types. Reads entire line..
}

Make Scanner read a String in java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am converting command line java application to swing GUI. I have a Parser class that reads input from Scanner
Scanner in = new Scanner(System.in);
command = in.nextInt();
Is it possible to pass String instead of (System.in) ? Or, is there a better way to deal?
You can use nextLine() of Scanner class to read string value entered in console.
Below is an example:
public class Parser {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Please enter command:");
String command = in.nextLine();
JOptionPane.showMessageDialog(null, command);
}
}
Eneter string value in console:
Please enter command:
Java Command
If you want pass Strings in Scanner just use Java's next() method
Scanner in = new Scanner(System.in);
String command = in.next();
I hope this is what you were looking for.

Categories