0

I am new to Java programming. I was reading about Scanner class and I decided to compile a simple program:

package com.company;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner obj= new Scanner(System.in);
        Scanner obj2=new Scanner(System.in);
        int n=obj.nextInt();
        String s = obj.nextLine();
        System.out.println(n*2);
        System.out.println(s);
    }
}

By using the object obj, I can only read the integer input and doesn't let me read the String input. Why can't we use the same object of Scanner class for taking the input since the only use of object is to call a method like nextLine().

Whereas if I use this code,

package com.company;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner obj= new Scanner(System.in);
        Scanner obj2=new Scanner(System.in);
        int n=obj.nextInt();
        String s = obj2.nextLine();
        System.out.println(n*2);
        System.out.println(s);
    }
}

Using a different object obj2 for string and obj for integer,I get my desired output. Any help would be appreciated!

  • Why do you think you have to pull ints out of that scanner? Instead, just read in strings, and _then_ see "if that input was a number or not" by trying to parse it as Integer. If that fails: the input was not a number, use the codepath for string data. If it passes: that was a number, call whatever handles numbers. And if you want to get even more creative, write a string parser that can split up a line of text into parts that might be numbers, text, complex information, etc. Java isn't making you do anything here, you get to invent the wheel. – Mike 'Pomax' Kamermans Jul 28 '20 at 00:12
  • Why can't I use object obj, to read the Integer input as well as String input. – Riten Bhagra Jul 28 '20 at 00:13
  • You can? Just use [Scanner.next()](https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#next--), not [Scanner.nextLine()](https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#nextLine--), which does something _completely different_. Remember that it's always important to verify that a function you're calling actually does what you think it does. In this case, that is very much not the case right now. – Mike 'Pomax' Kamermans Jul 28 '20 at 00:18
  • You ain't getting my point, please run the 2 programs I have given in the question and understand what I am trying to say! – Riten Bhagra Jul 28 '20 at 00:24
  • 1
    @Idle_Mind you're a genius mate, thank you so much for the help! – Riten Bhagra Jul 28 '20 at 01:03

2 Answers2

1
package com.company;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner obj= new Scanner(System.in);
        int n=obj.nextInt();
        String s = obj.next();
        System.out.println(n*2);
        System.out.println(s);

    }

}
  • This also does not fully answer the question, as there is no indication as to *why* OP's code does not work. –  Jul 28 '20 at 00:21
  • @Taschi Can you please tell me why my 1st code does not work? – Riten Bhagra Jul 28 '20 at 00:32
  • @Mike'Pomax'Kamermans Can you please tell me why my 1st code does not work? – Riten Bhagra Jul 28 '20 at 00:32
  • I already did in a comment: you didn't read up on what `nextLine()` does, but if you did (and I linked you to the documentation in my comment) you'd realise that's _not_ the function you're supposed to use to get a string. To get a string, you use `next()` – Mike 'Pomax' Kamermans Jul 28 '20 at 14:54
-3

try this: String s = obj2.next();