2
import java.util.Scanner;

public class Hello {

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

        System.out.println("Enter int : ");
        a = in.nextInt();
        System.out.println("Enter String : ");

        s = in.nextLine();
        System.out.println("int : "+a+"\nString : "+s);
    }
}

I'm a completely beginner of Java. I made the class Hello and want to enter one number and one string but I think s = in.nextLine(); this line is ignored. How can I enter two values line by line.

Tiny
  • 24,933
  • 92
  • 299
  • 571
beginner
  • 21
  • 3
  • 5
    @user1348753 OP's saying he's a beginner, so help him/her to learn or move away, remember that you also have faced these problems when learning Java. – Luiggi Mendoza Jul 13 '12 at 06:15
  • 1
    Try to enter '1 string'. This will output probably what you are expecting, won't it? Scanner on System.in works the other way you are considering. – Viktor Stolbin Jul 13 '12 at 06:17
  • Another similar one: http://stackoverflow.com/questions/1466418/java-scanner-class-reading-strings – vaisakh Jul 13 '12 at 06:18
  • @Luiggi Mendoza this is very basic question and can be found on google. The best way to learn is learn yourself. That is what I am teaching him. And as he is bignner that why I haven't rated him -1 for his question. :) – ManMohan Vyas Jul 13 '12 at 06:22
  • @user1348753 So when you have a question to your teacher about how to do X, if he/she answers you to google it, what will you think? At least provide a link or a guide to solve the problem. – Luiggi Mendoza Jul 13 '12 at 06:25
  • @user1348753 Yeah, make'em suffer ;) – Viktor Stolbin Jul 13 '12 at 06:26
  • people have posted already enough link, I would have liked him to come with a blog post and sayig "I am not able to understand it". It is not that I dont want to help. And My good teacher thought me to learn myself. He always used to say, "I have read this book and teahing u , u too can read and teach me, instead of asking." Sorry If look harsh but anyways ..their are enough post to help him .. great life to all people here ..enjooyyy – ManMohan Vyas Jul 13 '12 at 06:32
  • Thank everyone who doesn't save your time to find Duplicate of my question. This is a my first question here and spent much time to just post my question. I leaned how to post and how to search similer subject of my worder before posting. once again Thank you. – beginner Jul 13 '12 at 06:58

4 Answers4

4

When you use Scanner.nextInt(), it does not consume the new line (or other delimiter) itself so the next token returned will typically be an empty string. Thus, you need to follow it with a Scanner.nextLine(). You can discard the result instead of assigning it to a:

int a = in.nextInt(); 
in.nextLine();

It's for this reason that I suggest always using nextLine (or BufferedReader.readLine()) and doing the parsing after using Integer.parseInt().

Lion
  • 17,515
  • 21
  • 76
  • 104
  • In simple words, the problem is that the `nextInt` call reads from input until the end of the int, but does not read the *newline* character after the int. – Lion Jul 13 '12 at 06:37
1

Instead of

s = in.nextLine();

try

in.nextLine();
s = in.nextLine();

The call to nextInt() still leaves a trailing newline.

Calling in.nextLine() actually goes to the next line. Then in.nextLine will get your actual result.

Brad
  • 8,805
  • 10
  • 39
  • 67
0

Try it like this :D

import java.util.Scanner;

public class Hello {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        int a;
        String s = null;
        System.out.print("Enter int: ");
        a = in.nextInt();

        while ((s = in.nextLine()).trim().isEmpty()) {
            System.out.print("Enter String: ");
        }

        System.out.println("int : " + a + "\nString : " + s);
    }
}
ioreskovic
  • 5,122
  • 5
  • 34
  • 62
0
import java.util.Scanner;
public class Hello{
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        int a;
        String s;
        System.out.println("Enter int : ");
        a = in.nextInt();
        System.out.println("Enter String : ");
        s = in.next();
        System.out.println("int : "+a+"\nString : "+s);
    }
}
Fathah Rehman P
  • 7,353
  • 3
  • 36
  • 41