what's wrong with my java program? [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
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;

Related

Java Scanner from second method [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 2 years ago.
Improve this question
I am having problem to invoke Scanner class from second method in main method. My code is this:
import java.util.Scanner;
public class Main{
static void checkAge(int age){
Scanner new_age = new Scanner(System.in);
age = new_age.nextInt();
System.out.println("Enter your age");
if(age < 18){
System.out.println("You are a minor");
} else {
System.out.println("You are of apropriate age");
}
}
public static void main(String[] args){
checkAge();
}
}
I get an error:
Main.java:18: error: '.class' expected
checkAge(Scanner.new_age(int));
^ 1 error
Call you function with arguments : checkAge(18); or checkAge(15);as you checkAge() requires parameters to check the conditions.

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.

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.

I have to write a code that uses classes for DNA, RNA, and Protein. Im stuck on the first part [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 have to take a users input int as length of the dna a sequence. Im trying to return the numberString but i get an issue with my array every time
Driver
import java.util.Scanner;
public class GenBank1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.println("Enter a desired DNA sequence length, between 1 and 10 please:");
int inputLength = in.nextInt();
System.out.println(inputLength);
System.out.println(DNA1.toNumString(inputLength));
int[] baseId = new int[inputLength];
for(int i=0;i<=baseId.length;i++){
baseId[i]=inputLength;
int rndmr = (int)(4.0*Math.random());
baseId[i]-=rndmr;
System.out.print(baseId[i]+1 + ",");
}
}
}
for(int i=0;i<=baseId.length;i++)
You should probably do
for(int i=0;i<baseId.length;i++)
since, in an array with (say) 10 entries, they will have indices 0 to 9. Trying to look up array[10] will throw an exception.
Without knowing what the DNA1 class is and what the function of the program is, I would not be able to comment futher. However yes, you will experience ArrayIndexOutOfBoundsException to correct this the following should work
import java.util.Scanner;
public class GenBank1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.println("Enter a desired DNA sequence length, between 1 and 10 please:");
int inputLength = in.nextInt();
System.out.println(inputLength);
System.out.println(DNA1.toNumString(inputLength));
int[] baseId = new int[inputLength];
for(int i=0;i<baseId.length;i++){
baseId[i]=inputLength;
int rndmr = (int)(4.0*Math.random());
baseId[i]-=rndmr;
System.out.print(baseId[i]+1 + ",");
}
}
}
Your ending clause for your FOR loop wanted to go from 0 -> length which would be wrong as you will go over the Array index.

Array Cannot Find Symbol-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 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";

Categories