1

I have to say I am a newbie in java programming so I would like some help with the following question:

How can I delay a java program so that it waits for a user to give an input? Here is my code:

import java.util.Scanner;

public class test {

    public static void main(String[] args) {
        System.out.println("1st value= ");
        Scanner aa1 = new Scanner(System.in);
        System.out.println("2nd value= ");
        Scanner bb1 = new Scanner(System.in);

What I get now from this code is "1st value= " and "2nd value= " displayed. After that the program requests the input. Any ideas on how can I make this simple programm to wait for the user's answer before printing "2nd value= " in the console?

akash
  • 22,664
  • 11
  • 59
  • 87
Bram Fran
  • 113
  • 5
  • 11

6 Answers6

3

Ask for input from the user:

int index = aa1.nextInt();
Ryan
  • 2,058
  • 1
  • 15
  • 29
0

You don't need to create two Scanner variables. You can use one of them and use the function input.nextInt(); to get both values, where input it's the name of the Scanner variable.

import java.util.Scanner;

public class test {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int number1, number2;

        System.out.println("1st value= ");
        number1 = input.nextInt();
        System.out.println("2nd value= ");
        number2 = input.nextInt();
   }
}
Francisco Romero
  • 12,787
  • 22
  • 92
  • 167
  • Thanks but why is Scanner method not suitable??? – Bram Fran Sep 11 '15 at 14:06
  • @BramFran When you use `Scanner bb1 = new Scanner(System.in);` you are creating a new `Scanner` variable named `bb1` but you are not reading the input. – Francisco Romero Sep 11 '15 at 14:07
  • what if I want the input to be double instead of int? because I make it number1 = input.nextDouble(); (NO ERRORS) and then when i type the number 1.5 for example I get this crash report: Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextDouble(Unknown Source) at test2.main(test.java:10) – Bram Fran Sep 11 '15 at 14:37
  • @BramFran Try using a coma. See this question: http://stackoverflow.com/questions/10584948/doubles-commas-and-dots. – Francisco Romero Sep 11 '15 at 14:41
0

You have to use only one Scanner variable. This is the solution :

import java.util.Scanner; 
import java.util.*;

public class test {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        String str = sc.nextLine();
        System.out.println("1st value= " + str);

        str = sc.nextLine();
        System.out.println("2nd value= " + str);
    }
}

You can see the different next***() on the doc on this link http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html.

I advise you to follow a Java formation if you want to code in this language.

romainb78
  • 51
  • 1
  • 8
0
  1. You must first import the Scanner before your Main class.

    import java.util.Scanner;

  2. You must then create a Scanner variable. I use "in" in my example:

    Scanner in = new Scanner(System.in);

    1. This will force the program to wait for user input.

` import java.util.Scanner;

public class Example{
    public static void main( String[] args )
    {
        String name;
        String address;
        String cityStateZip;
        Scanner in = new Scanner(System.in);

        //User input
        System.out.println("What is your name?");
        name = in.nextLine();

        System.out.println("Enter your address:");
        address = in.nextLine();

        System.out.println("Enter your City, State, and Zip code:");
        cityStateZip = in.nextLine();`
Jonathan Scialpi
  • 771
  • 2
  • 11
  • 32
0

You can use it as:

import java.util.Scanner;

public class test {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("1st value= " 
            + input.nextInt()
            + "2nd value= "
            + input.nextInt());
}

For further reference to can go through Scanner java doc http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

cptjack
  • 379
  • 4
  • 4
0

Add necessary imports by pressing control+shift+o after you have typed the main function.

 public static void main(String agrs[])
    {
    // Take Scanner object
     Scanner scanner =new Scanner(System.in);
    // Ask user for 1st input
    System.out.println("Enter 1st value:");
    String value=scanner.nextLine();
    System.out.println("1st value is ="+s);
    // Ask user for 2nd input
    System.out.println("Enter 2nd value:");
    value=scanner.nextLine();
    System.out.println("2nd value is ="+s);



    }

NOTE : scanner.nextLine() will read the user input as a String, If you need the input as int , use scanner.nextInt(); For float use scanner.nextFloat() and so on.

Now the console is halted for user input and then asks for second input.

Hope it helps.

App Work
  • 21,899
  • 5
  • 25
  • 38