Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

DrJava Error message

Options
  • 23-10-2013 12:37pm
    #1
    Registered Users Posts: 9


    Hi there,

    I am a studying java on a beginners SDLC course and currently working on an assignment.

    I need to create a user name and password but I keep coming up with a "The local variable user_name is never read" error message.

    What's wrong? here's what I've got so far.


    // Begin Program
    import java.util.Scanner;
    // Define class
    public class groceryOnline

    {
    public static void main(String[] args)
    {
    Scanner user_input = new Scanner(System.in);

    String user_name;
    System.out.println("Enter; ");
    first_name = user_input.next();
    }
    }


Comments

  • Registered Users Posts: 474 ✭✭Umekichi


    Hi there,
    you have declared a String for user_name, yet you used first_name to store the input.
    Did you mean to do:

    String user_name;
    System.out.println("Enter; ");
    user_name = user_input.next();


  • Registered Users Posts: 9 KimboForte


    Just tried that and it didnt work,

    import java.util.Scanner;
    // Define class
    public class groceryOnline

    {
    public static void main(String[] args)
    {
    Scanner user_input = new Scanner(System.in);

    String user_name;
    System.out.println("Enter: ");
    user_name = user_input.next();
    }
    }


  • Registered Users Posts: 474 ✭✭Umekichi


    Just fiddled with it there the problem is you aren't using user_name. It just sits there waiting to be used.
    if you put in a System.out.println(user_name); statement after user_name = user_input.next(); you'll see what i mean.


  • Registered Users Posts: 3,078 ✭✭✭onemorechance


    A simple text scanner which can parse primitive types and strings using regular expressions.

    A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.

    For example, this code allows a user to read a number from System.in:

    Scanner sc = new Scanner(System.in);
    int i = sc.nextInt();

    As another example, this code allows long types to be assigned from entries in a file myNumbers:

    Scanner sc = new Scanner(new File("myNumbers"));
    while (sc.hasNextLong()) {
    long aLong = sc.nextLong();
    }
    The scanner can also use delimiters other than whitespace. This example reads several items in from a string:

    String input = "1 fish 2 fish red fish blue fish";
    Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
    System.out.println(s.nextInt());
    System.out.println(s.nextInt());
    System.out.println(s.next());
    System.out.println(s.next());
    s.close();
    prints the following output:

    1
    2
    red
    blue
    The same output can be generated with this code, which uses a regular expression to parse all four tokens at once:

    String input = "1 fish 2 fish red fish blue fish";
    Scanner s = new Scanner(input);
    s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
    MatchResult result = s.match();
    for (int i=1; i<=result.groupCount(); i++)
    System.out.println(result.group(i);
    s.close();
    The default whitespace delimiter used by a scanner is as recognized by Character.isWhitespace.

    A scanning operation may block waiting for input.

    Java docs


  • Registered Users Posts: 3,078 ✭✭✭onemorechance


    Don't forget to close your scanner.

    user_input.close();

    System.out.println("user name: " + user_name);

    Also in Java class names should start with upper case, always do this.


  • Advertisement
  • Registered Users Posts: 9 KimboForte


    Finally :- 0 it works! great thanks so much!


  • Registered Users Posts: 710 ✭✭✭mad turnip


    user_input.next();

    I think should be

    user_input.nextLine();

    although according to the API next() should work.


  • Registered Users Posts: 3,078 ✭✭✭onemorechance


    mad turnip wrote: »
    user_input.next();

    I think should be

    user_input.nextLine();

    although according to the API next() should work.

    It's because the scanner is not closed; it blocks still accepting input.


  • Registered Users Posts: 9 KimboForte


    What if I don't want to system to display the username i just entered? It should go like this:

    Enter Username:
    Enter Password:

    Welcome/invalid log in.

    Obviously I need to set a value to the username string.


  • Registered Users Posts: 474 ✭✭Umekichi


    KimboForte wrote: »
    What if I don't want to system to display the username i just entered? It should go like this:

    Enter Username:
    Enter Password:

    Welcome/invalid log in.

    Obviously I need to set a value to the username string.

    Maybe try using an if statement?
    if(username.equals(what you want username to be ) && password.equals(what you want pass to be)){
    System.out.println("Welcome!");
    }else{
    System.out.println("Invalid log-in");
    }


  • Advertisement
  • Registered Users Posts: 9 KimboForte


    Whatever I put in the brackets for username, the error message being returned is that it can't be resolved to a variable


  • Registered Users Posts: 474 ✭✭Umekichi


    This is what I did:
    import java.util.Scanner;
    public class groceryOnline {
    public static void main(String[] args) {
    Scanner user_input = new Scanner(System.in);

    String password;//password declared
    String user_name;//username declared
    System.out.println("Enter: ");
    user_name = user_input.nextLine();

    System.out.println("Password: ");
    password = user_input.nextLine();

    if(user_name.equals("hi") && password.equals("ok")){
    System.out.println("Welcome!");
    }else{
    System.out.println("Invalid log-in");
    }
    user_input.close();
    }
    }

    So what I did was, took info from user for Username and password. Then I ran an if statement that compared the user_name and password to ones I specified and if both were equal it printed "Welcome!" and if they both weren't equal then it prints, "Invalid log-in".


Advertisement