0

I have the following code:

String username="administrator", iusername="", password="password", ipassword="";
System.out.println("Please login to the system");
System.out.print("\nusername:\t");

iusername = scan.nextLine();
System.out.print("\npassword:\t");
ipassword = scan.nextLine();

i want user enter data for two variable (iusername and ipassword) but i only can enter data for ipassword, because it skip

iusername = scan.nextLine();

below is the sample of output from IDE

username:   username:   

password:   BUILD STOPPED (total time: 12 seconds)

it skip my username and go to password

8
  • 5
    What are scan and scan2? It sounds like you've created two separate Scanner instances... why? (And why are you initializing your variables and then immediately assigning new values?) Commented Nov 19, 2013 at 13:01
  • You're using scan and scan2. What's the difference? Commented Nov 19, 2013 at 13:01
  • using scan and scan2 is because i cannot enter the username variable data and it jump to the password variable @JonSkeet and @jeroenVannevel Commented Nov 19, 2013 at 13:04
  • Looks like Scanner class instances. You can resuse the same instance. Commented Nov 19, 2013 at 13:05
  • See my answer below, I think you just want it to show the username entered. Commented Nov 19, 2013 at 13:11

2 Answers 2

1

This can be used to put new output on new lines automatically.

System.out.println("");

Without more code, or a clearer question this is hard to answer.

username = scan.nextLine();
System.out.print("\npassword:\t");
password = scan2.nextLine();

Should be, as you can reuse the Scanner class Instance.

username = scan.nextLine();
System.out.print("\npassword:\t");
password = scan.nextLine();

But maybe you want to do

username = scan.nextLine(); // Get user input
System.out.println("Username:\t" + username); // Show typed username
password = scan2.nextLine(); // Get user input again
System.out.println("Password:\t" + password); // Show typed password
Sign up to request clarification or add additional context in comments.

Comments

1

I'm sorry, I don't catch your point... I tried this and I'm able to insert both values (iusername and ipassword):

import java.util.Scanner;

public class MultipleStringInput {

   public static void main(String[] args) {
      String username = "administrator", iusername = "", password = "password", ipassword = "";
      System.out.println("Please login to the system");
      System.out.print("\nusername:\t");

      Scanner scan = new Scanner(System.in);

      iusername = scan.nextLine();
      System.out.print("\npassword:\t");
      ipassword = scan.nextLine();
      System.out.println(iusername + " " + ipassword);
   }

}

I think this code works as expected ("i want user enter data for two variable"). It looks like your IDE is redirecting System.out to your scanner... strange... I suggest you test it from command line.

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.