I'm having an issue that I've been working on most of the day. I want to get the user name, then return it twice, but can't seem to get the variable right. Here is my code.

import java.util.Random;
import java.util.ArrayList;
import java.util.Scanner;


public class mlab
{
    // instance variables - this instance will get reader's name
    private Scanner reader;//name from user
    private Random random;//construct a Responder, declare a field type of Random
    private ArrayList<String> name;//construct a responder, declare a field type ArrayList

    /**
     * Constructor for objects of class Calculator
     * Create a responder. 
     * Create the Random and ArrayList object.
     * Create the Scanner input object.
     */
    public mlab()
    {
       reader = new Scanner(System.in); 
       random = new Random();
       name = new ArrayList<String>();
       
    }
    private String getInput()
    {
        String user = reader.nextLine();
        return user;
    }
    
    /**
     *Begin the program. This will print a welcome message
     *and enter into a dialog with the user, until the user ends
     *the dialog.
     * 
     * @param  y   a sample parameter for a method
     * @return  A String typed by user.
     */
    public void start()
    {
        printWelcome();
        }
    
    /**
     * Print a welcome message to the screen
     */
    private void printWelcome()
    {
      
      System.out.println("Welcome to the 5th Grade Math lab");
      System.out.println("What is your name?"); 
The code below gets the user name and returns it fine, but see a few lines down
      System.out.println("Hello, " + reader.nextLine() + ". Today you can practice Addition, Subtraction, or Multiplication.");
      System.out.println("Type A for Addition, S for Subtraction, or M for Multiplication.");
      
      String inputLine = reader.nextLine();
      if(inputLine.equalsIgnoreCase("A")){
          printA();
        }
      if(inputLine.equalsIgnoreCase("S")){
          printS();
        }
      if(inputLine.equalsIgnoreCase("M")){
          printM();
        }
        
    }
    
    
    private void printA()
    {

        random = new Random();
        int num1;
        int num2;
        int Index = random.nextInt();
            System.out.println("You have chosen to work on Addition today. The computer will now");
            System.out.println("randomly select two numbers for you.");
            System.out.println("The first number is: ");
            System.out.println(num1 = random.nextInt(100) + 1);
            System.out.println("The second number is: ");
            System.out.println(num2 = random.nextInt(100) + 1);
            System.out.println("Enter your answer for " + num1 + " + " + num2);  
            int guess = reader.nextInt();
    
        if(guess == num1 + num2) {
        //Here after Great Job, I want to output the username again and can't figure out how.
            System.out.println("Great Job, ! Your answer is CORRECT! " + num1 + " + "+  num2 + " = " + (num1 + num2) + "!");
        }
        else {
            System.out.println("Sorry, your answer is INCORRECT! " + num1 + " + " + num2 + " = " + (num1 + num2) + ".");
        }
        
            //some code omitted here
        }

Can anyone help? Please!

Thank you.

Recommended Answers

All 14 Replies

What do you mean by return twice?

This line, save the reader.nextLine() in a string before display, so you can reuse it again?

System.out.println("Hello, " + reader.nextLine() + ". Today you can practice Addition, Subtraction, or Multiplication.");
Member Avatar for coil

You should store the input in a String, as Taywin suggested:

String name=getInput();

Then use the name variable whenever you need it.

I know I need to use a variable, but everytime I try it cause all sorts of problems. Where do I put the code?

They mean that you need to declare a new variable to put the username into it, and then when prompting the user for the username, just put it inside the variable, and then you will be able to use it later.

private void printWelcome()
    {
      
      System.out.println("Welcome to the 5th Grade Math lab");
      System.out.println("What is your name?"); 
      userName = reader.nextLine(); //userName will be declared as a global variable to be used later.


      System.out.println("Hello, " + userName + ". Today you can practice Addition, Subtraction, or Multiplication.");
     
    //rest of code...
   
    }

Hi, I've tried that a million times. The error is "cannot find variable userName". I know it's something dumb I'm doing, but I'm very new to Java and find this very difficult.

Please post your code so far.

Okay, here is my new code. Thank you

import java.util.Random;
import java.util.ArrayList;
import java.util.Scanner;


public class mlab
{
    // instance variables - this instance will get reader's name
    private Scanner reader;//name from user
    private Random random;//construct a Responder, declare a field type of Random
    private ArrayList<String> name;//construct a responder, declare a field type ArrayList

    /**
     * Constructor for objects of class Calculator
     * Create a responder. 
     * Create the Random and ArrayList object.
     * Create the Scanner input object.
     */
    public mlab()
    {
       reader = new Scanner(System.in); 
       random = new Random();
       name = new ArrayList<String>();
       
    }
    private String getInput()
    {
        String name = getInput();
        String user = reader.nextLine();
        userName = reader.nextLine();
        return user;
    }
    
    /**
     *Begin the program. This will print a welcome message
     *and enter into a dialog with the user, until the user ends
     *the dialog.
     * 
     * @param  y   a sample parameter for a method
     * @return  A String typed by user.
     */
    public void start()
    {
        printWelcome();
        }
    
    /**
     * Print a welcome message to the screen
     */
    private void printWelcome()
    {
      
      System.out.println("Welcome to the 5th Grade Math lab");
      System.out.println("What is your name?");
      userName = reader.nextLine();
      System.out.println("Hello, " + userName + ". Today you can practice Addition, Subtraction, or Multiplication.");
      System.out.println("Type A for Addition, S for Subtraction, or M for Multiplication.");
      
      String inputLine = reader.nextLine();
      if(inputLine.equalsIgnoreCase("A")){
          printA();
        }
      if(inputLine.equalsIgnoreCase("S")){
          printS();
        }
      if(inputLine.equalsIgnoreCase("M")){
          printM();
        }
        
    }
    
    
    private void printA()
    {

        random = new Random();
        int num1;
        int num2;
        int Index = random.nextInt();
            System.out.println("You have chosen to work on Addition today. The computer will now");
            System.out.println("randomly select two numbers for you.");
            System.out.println("The first number is: ");
            System.out.println(num1 = random.nextInt(100) + 1);
            System.out.println("The second number is: ");
            System.out.println(num2 = random.nextInt(100) + 1);
            System.out.println("Enter your answer for " + num1 + " + " + num2);  
            int guess = reader.nextInt();
    
        if(guess == num1 + num2) {
        
            System.out.println("Great Job, ! Your answer is CORRECT! " + num1 + " + "+  num2 + " = " + (num1 + num2) + "!");
        }
        else {
            System.out.println("Sorry, your answer is INCORRECT! " + num1 + " + " + num2 + " = " + (num1 + num2) + ".");
        }
        
         //some code omitted below...   
        }

You are never declaring userName - there is no line "String userName;" which define userName as a String variable. Also - on line 28 - you are calling in a never-ending recursion to the same method getInput();

I know, I don't know where to put it??

Ok, first of all I suggest that you will read about variable scope, to help you understand where to put variables in order to access them later. Now for our issue - if you put the variable as a global one, meaning with reader, random etc' - you will be able to access it from every place in the class.

Still don't get it

Dude, I just gave you the answer - if you put the variable as a global one, meaning with reader, random etc' - you will be able to access it from every place in the class.

Member Avatar for coil

At the top, you declared your Scanner because you need it throughout your class. Since you also need your userName variable throughout your class, declare it there as well.

ok, I want to help, but I an unsure of what you want.. are you trying to read the username from a pre existing textfile? are you trying to get the username of the current user at the workstation? what are you doing?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.