在Java中,可以使用同一个对象来接受整数输入和字符串输入。

huangapple go评论146阅读模式
英文:

Using same object to take integer input and string input in java

问题

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 = obj2.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!

英文:

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!

答案1

得分: 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);

    }

}
英文:
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);

    }

}

答案2

得分: -3

尝试这个:String s = obj2.next();

英文:

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

huangapple
  • 本文由 发表于 2020年7月28日 08:08:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63125296.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定