5

This is probably a really stupid question, but I'm having problems calling methods in java. For my computer science class I am instructed to write a single program with multiple methods. In one method I am to prompt the user to enter an integer, return that integer and store it in a variable. The next method is to be passed the integer from the previous method and return true if the integer is odd and false if it is not.

My problem is this, when I try to call the second method from the main method, I get an error message saying "cannot find symbol. symbol number" I'm sure it has something to do with the scope of the variable only existing within the getInput method, but I don't know how to get the program to print the value from my second method if it won't recognize my variable from the first method.

Here's what I have come up with so far. (You can disregard the method named printBanner, that one works, I'm just having trouble with the next two, getInput and isOdd)

import java.util.Scanner;
public class MethodlabPractice {
 public static void main(String[] args) {
 printBanner();
 getInput();
 isOdd(number);
 } // end main
 public static void printBanner () {
 for (int count = 0; count != 10; count++)
 System.out.println("Beth Tanner");
 } // end printBanner
 public static int getInput() {
 Scanner input = new Scanner(System.in);
 System.out.println("Please enter an integer:");
 int number = input.nextInt();
 System.out.println(number);
 return number;
 } // end getInput
 public static boolean isOdd(int number) {
 boolean odd = number % 2 != 0;
 return odd;
 } // end isOdd
}// end class 
asked Nov 7, 2013 at 20:04
4
  • Its always easier to answer a question about an exception if you post the stacktrace. Commented Nov 7, 2013 at 20:06
  • @Deadron This should actually be a compiler error, hence no stack trace. It still will give an error message, though, which OP should post in full. Commented Nov 7, 2013 at 20:07
  • 5
    Downvote was mean. We were all beginners once. Commented Nov 7, 2013 at 20:08
  • 3
    Agree with NickJ. While no errors were posted, the explanation was good and the code is pretty clear -- especially for a beginner. Commented Nov 7, 2013 at 20:11

3 Answers 3

7

You haven't defined the variable number within the scope of the main method.

int number = getInput();
isOdd(number);
Eran
395k57 gold badges726 silver badges793 bronze badges
answered Nov 7, 2013 at 20:05
Sign up to request clarification or add additional context in comments.

5 Comments

Well I feel dumb, that's easy enough. Thanks!
You should also accept his answer. Another alternative is isOdd(getInput()).
This solved the problem I was having, but now instead of using the value stored in number for the isOdd method, it asks for another integer before printing true or false. How can I make sure it just used the value I entered when prompted the first time rather than being prompted again?
There's something funny going on there - you're not calling getInput twice by any chance?
@Reimeus not intentionally, but I may be.
4

getInput returns int, save it and then pass it

int number = getInput();
isOdd(number);

instead what you are trying to do is

getInput();
isOdd(number); // passing number, but number is not defined

Or you can do:

isOdd(getInput());
Baklap4
4,2222 gold badges33 silver badges58 bronze badges
answered Nov 7, 2013 at 20:06

Comments

1

You're passing the argument number to isOdd() method inside your main method. But number must be declared before it can be passed.

answered Nov 7, 2013 at 20:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.