0

I have some methods that should incorporate user Input. I put the scanner in the main method, but the values entered by the user are not getting returned, nor getting used in the code. How can I turn the user input into a var that can be accessed by the methods?

for example:

import java.util.Scanner;
public class example {

int variableOne;
int variableTwo;
int variableResultFromUserInput;

public int variableResultFromUserInput(int variableOne, int variableTwo) {
    this.variableResultFromUserInput = variableOne * variableTwo;
    return this.variableResultFromUserInput;

}
public static void main(String[]args) {
    example myexample = new example();
    Scanner keyboard = new Scanner(System.in);
    System.out.println("What's your variable one? ");
    variableOne = keyboard.nextInt(); //this is the variable that causes trouble

    System.out.println("Whats your variable two?");
    variableTwo = keyboard.hasNextInt(); //this is the second variable that causes trouble

    System.out.println(myexample.variableResultFromUserInput); //this is also wrong
}

}

4 Answers 4

1

So there are a lot of things going on in your code that are incorrect, so I will list out the problems:

  1. You are attempting to use both the instance myexample and set the instance variables of variableOne and variableTwo, and pass them as a parameter to an instance method. This is completely redundant and serves no purpose, you already have access to them from within variableResultFromUserInput(). Access them with this.variableOne (though the this is optional, but clarifies what is going on).
  2. You need to set variableOne and variableTwo from the static context of main using the instance, AKA use myexample.variableOne = keyboard.nextInt() not variableOne = keyboard.nextInt().
  3. You named a variable the same as your instance method, and were attempting to read that instead of calling the method. You should be using myexample.variableResultFromUserInput() with parenthesis and the correct number of parameters accordingly. This variable can be completely removed as it is redundant currently.
  4. You do not follow Java naming conventions for the class name, or for variable names. myexample should be myExample, and the class should be Example.
  5. variableTwo = keyboard.hasNextInt(); should be keyboard.nextInt() not hasNextInt(). hasNextInt only checks to see if there is an integer available, but it does not actually read the integer, it returns a boolean instead that is true or false.

Here is code with all the changes made below:

public class Example {

    int variableOne;
    int variableTwo;

    public int variableResultFromUserInput() {
        return this.variableOne * this.variableTwo;
    }

    public static void main(String[]args) {
        Example myExample = new Example();
        Scanner keyboard = new Scanner(System.in);
        System.out.println("What's your variable one? ");
        myExample.variableOne = keyboard.nextInt(); //this is the variable that causes trouble

        System.out.println("Whats your variable two?");
        myExample.variableTwo = keyboard.nextInt(); //this is the second variable that causes trouble

        System.out.println(myExample.variableResultFromUserInput()); //this is also wrong
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

In the last line of your code,

System.out.println(myexample.variableResultFromUserInput);

you are not actually calling the method. Your code should differ thus:

System.out.println(myexample.variableResultFromUserInput(variableOne,variableTwo);

In the the method "variableResultFromUserInput," you are recursively calling the method. While there is a way to accomplish what you are doing, I recommend to instead return the mathematical operation. This would look like the following:

public int variableResultFromUserInput(int variableOne, int variableTwo) {
return variableOne * variableTwo;}

Let me know how this works out.

Comments

0

You have done several mistakes in your code.

  • You didn't call your method.Instead of that, you printed a variable.
  • HasNextInt is used to check whether there is int in the line that you are reading. It only returns a boolean.
  • You have used misleading method names and variable names.

    import java.util.Scanner;
    
    public class Main {
    private static int variableOne;
    private static int variableTwo;
    int variableResultFromUserInput;
    
    public int variableResultFromUserInput(int variableOne, int variableTwo) {
        this.variableResultFromUserInput = variableOne * variableTwo;
        return this.variableResultFromUserInput;
    
    }
    
    public static void main(String[] args) {
        Main myexample = new Main();
        Scanner keyboard = new Scanner(System.in);
        System.out.println("What's your variable one? ");
        variableOne = keyboard.nextInt();
    
        System.out.println("Whats your variable two?");
        variableTwo = keyboard.nextInt();
    
        System.out.println(myexample.variableResultFromUserInput(variableOne,variableTwo));
    
    }
    

    }

Comments

0

Your code have several problems, try out this version:

import java.util.Scanner;
public class Example {

  public int variableResultFromUserInput(int variableOne, int variableTwo) {
     return variableOne * variableTwo;
   }

   public static void main(String[]args) {
    Example myexample = new Example();
    Scanner keyboard = new Scanner(System.in);
    System.out.println("What's your variable one? ");
    int variableOne = keyboard.nextInt();

    System.out.println("Whats your variable two?");
    int variableTwo = keyboard.nextInt();

    System.out.println(myexample.variableResultFromUserInput(variableOne, variableTwo));   }
}

You have to put this content in file named Example.java, compile with

javac Example.java 

and run with

java Example

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.