I can't get my userName variable right, it prints out user name the first time,
but the second time it prints out null...I've been at this same problem for nearly 24 hours.

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
    private String userName;
    /**
     * 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 userName()
    {
       
        String userName = reader.nextLine();
        return userName;
    }
    
    /**
     *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?");
      String userName = reader.nextLine();
This prints out Hello, Heather (or whatever userName is)
      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) {
   This line prints out Great Job, null     
            System.out.println("Great Job, " + userName + "! 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...

You declare (at least) three different variables called userName. This is almost certainly a mistake. Just keep the very first one, and in the other 2 places just use the first one, eg instead of

String userName = reader.nextLine(); // creates a new userName local to this method

just have

userName = reader.nextLine(); // uses the userName declared in the class

Thank you so much! It worked. I can't believe it messed me up for sooo long.

Heather

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.