Cannot find symbol variable number?

K

KyoGaSuki

I was just trying out a simplified version of a reply from a previous
post, attempting to call a method...but it keeps coming up with the
error message "Cannot find symbol variable number". Can anyone help?

/**
* @(#)try2.java
*
* try2 application
*
* @author Kasumi
* @version 1.00 2008/4/3
* Attempting to finally and sucessfully CALL a method...and...I still
prove unsucessful...
*/
import java.util.*;

public class try2 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(getNumber(num)); // All I want it to do is to
print the number that I
// input from the method.
}

public static int getNumber(){
Scanner input = new Scanner(System.in); // It told me it couldn't
find the scanner, so
// I put it here and the error message went away.
System.out.print("Enter an integer: ");
int number = input.nextInt(); // Could this be the problem? I
don't know any other way to
// make the user input a variable, though.
return number; // ...please return?
}
}

------------ Build Report ------------
cannot find symbol variable num
 
K

KyoGaSuki

By the way...sorry for how messy it looks. If you have trouble reading
it, I can clean it up and repost?
 
J

Jeff Higgins

KyoGaSuki said:
By the way...sorry for how messy it looks. If you have trouble reading
it, I can clean it up and repost?

Oh, no problem buddy. Shoot, I can do that for ya.

/**
* @(#)try2.java
*
* try2 application
*
* @author Kasumi
* @version 1.00 2008/4/3
* Attempting to finally and sucessfully
* CALL a method...and...I still prove unsucessful...
*/
import java.util.*;

public class try2 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// All I want it to do is to
// print the number that I
// input from the method.
System.out.println(getNumber(num));
}

public static int getNumber(){
// It told me it couldn't find the scanner, so
// I put it here and the error message went away.
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
// Could this be the problem? I don't know any other
// way to make the user input a variable, though.
int number = input.nextInt();
return number; // ...please return?
}
}
 
A

Andrea Francia

KyoGaSuki said:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(getNumber(num)); // All I want it to do is to
^^^^
The variable 'num' is not defined in main().
And it is not required because your getNumber() method takes no arguments.
public static int getNumber(){
Other tip: if the getNumber() is not used outside of its class it is
better define it private.
 
A

Andrea Francia

KyoGaSuki said:
By the way...sorry for how messy it looks. If you have trouble reading
it, I can clean it up and repost?

I suggest you to use 2 spaces for indentation when posting the code on
usenet. This will make your code easier to read.

Also is a good idea post the complete compiler error

C:\...\JavaApplication14\src\try2.java:15: cannot find symbol
symbol : variable num
location: class try2
System.out.println(getNumber(num));
^
1 error
BUILD FAILED (total time: 0 seconds)
 
K

KyoGaSuki

aa...I'm sorry. Thank you all for your help -- maybe methods and/or
java all together is just not for me. I think I was better off just
using the computer instead of understanding it.
 
A

Andrea Francia

KyoGaSuki said:
aa...I'm sorry. Thank you all for your help -- maybe methods and/or
java all together is just not for me. I think I was better off just
using the computer instead of understanding it.
It's your choice ...
 
M

Mark Space

KyoGaSuki said:
aa...I'm sorry. Thank you all for your help -- maybe methods and/or
java all together is just not for me. I think I was better off just
using the computer instead of understanding it.

Well that's always an option.

Java is a "real" programming language, one that does not try to hold
your hand or make it easy on beginners.

If you are not in a Computer Science program at a university, I can see
why you are having problems. And don't feel bad, programming can be
hard for people who do have degrees. That's why we are all here,
sharing our problems.
 
K

KyoGaSuki

Well that's always an option.

Java is a "real" programming language, one that does not try to hold
your hand or make it easy on beginners.

If you are not in a Computer Science program at a university, I can see
why you are having problems.  And don't feel bad, programming can be
hard for people who do have degrees.  That's why we are all here,
sharing our problems.

...Sad part is, I am. I am taking Introduction to programming with
JAVA at a university. x.x
 
K

KyoGaSuki

Well, I changed it a little bit...or tried, at least. Please tell me
if I am getting any closer or further away...please?:

/**
* @(#)try2.java
*
* try2 application
*
* @author Kasumi
* @version 1.00 2008/4/3
* Attempting to finally and sucessfully CALL a method...and...I still
prove unsucessful...
*/
import java.util.*;

public class try2 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
System.out.println(getNumber(number));
}

private static int getNumber(int number){
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
return number;
}
}

-------------------- Error Report --------------------

C:\Users\Kasumi\Desktop\try2\src\try2.java:17: variable number might
not have been initialized
System.out.println(getNumber(number));
^
1 error

Process completed.
 
A

Alex.From.Ohio.Java

Well, I changed it a little bit...or tried, at least. Please tell me
if I am getting any closer or further away...please?:

/**
* @(#)try2.java
*
* try2 application
*
* @author Kasumi
* @version 1.00 2008/4/3
* Attempting to finally and sucessfully CALL a method...and...I still
prove unsucessful...
*/
import java.util.*;

public class try2 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
System.out.println(getNumber(number));
}

private static int getNumber(int number){
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
return number;
}

}

-------------------- Error Report --------------------

C:\Users\Kasumi\Desktop\try2\src\try2.java:17: variable number might
not have been initialized
System.out.println(getNumber(number));
^
1 error

Process completed.

Say:
int number=0;
that'll eliminate compiler error.

Alex.
http://www.myjavaserver.com/~alexfromohio/
 
A

Alex.From.Ohio.Java

Well, I changed it a little bit...or tried, at least. Please tell me
if I am getting any closer or further away...please?:
/**
* @(#)try2.java
*
* try2 application
*
* @author Kasumi
* @version 1.00 2008/4/3
* Attempting to finally and sucessfully CALL a method...and...I still
prove unsucessful...
*/
import java.util.*;
public class try2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
System.out.println(getNumber(number));
}
private static int getNumber(int number){
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
return number;
}

-------------------- Error Report --------------------
C:\Users\Kasumi\Desktop\try2\src\try2.java:17: variable number might
not have been initialized
System.out.println(getNumber(number));
^
1 error
Process completed.

Say:
int number=0;
that'll eliminate compiler error.

Alex.http://www.myjavaserver.com/~alexfromohio/

Actually you don't need number at all. Here is what you need.
I can't say it's perfect but it's close to what you wrote:

import java.util.*;

public class try2 {

static Scanner input = new Scanner(System.in);

public static void main(String[] args) {

System.out.println(getNumber());
}

private static int getNumber() {
System.out.print("Enter an integer: ");
return input.nextInt();
}
}

http://www.myjavaserver.com/~alexfromohio/
 
M

Mark Space

KyoGaSuki said:
...Sad part is, I am. I am taking Introduction to programming with
JAVA at a university. x.x

In that case, don't give up yet. These sorts of problems are normal
when you are just starting out.

I used to work at the computer lab at my university, and I'd see the
same sort of problems all the time.

Query: are you a Com Sci major, or just taking an intro course as part
of your GE?

You should be getting help from your instructors, the assistants at the
computer lab, fellow students, etc. They can help you debug your
program much faster than we can one little bit at a time over the 'net.
 
C

Chase Preuninger

you never declared the varible num befor you passed it to the
GetNumber method in your main method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top