web-dev-qa-db-ja.com

同じスキャナー変数を使用して文字列のint、double、およびセンテンスを読み取る方法

import Java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            String s=new String();
            int x=sc.nextInt();
            double y=sc.nextDouble();
            s = sc.next();

            System.out.println("String:"+s);
            System.out.println("Double: "+y); 
            System.out.println("Int: "+x);     
     }       
}

1つのWordだけをスキャンして、提案を与えてください...

5
Gokul Raj

次のようにスキップしてみませんか

import Java.util.Scanner;

public class Solution {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int i = scan.nextInt();
    scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
    double d = scan.nextDouble();
    scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

    scan.nextLine();
    String s = scan.nextLine();

    System.out.println("String: " + s);
    System.out.println("Double: " + d);
    System.out.println("Int: " + i);
}
2
jprism
import Java.util.Scanner;

public class Solution 
{
    public static void main(String[] args) 
    {
     Scanner scan = new Scanner(System.in);
     int i = Integer.parseInt(scan.nextLine());
     double d = Double.parseDouble(scan.nextLine());
     String s = scan.nextLine();

     System.out.println("String: " + s);
     System.out.println("Double: " + d);
     System.out.println("Int: "+ i);
     }
}
0
Sneha
import Java.util.Scanner;

public class Solution 
{
    public static void main(String[] args) 
    {
     Scanner scan = new Scanner(System.in);
     int i = Integer.parseInt(scan.nextLine());
     double d = Double.parseDouble(scan.nextLine());
     scan.nextLine();
     String s = scan.nextLine();
     scan.close();

     System.out.println("String: " + s);
     System.out.println("Double: " + d);
     System.out.println("Int: "+ i);
     }
}

入力のトークンの読み取りと入力の全行の読み取りを切り替える場合、スキャナーオブジェクトは前の読み取りが中断した行の残りの部分を読み取るため、nextLine()をもう一度呼び出す必要があります。

行に何もない場合は、単に改行を消費して次の行の先頭に移動します。

二重宣言の後、次のように記述する必要があります。scan.nextLine();

0
Arun

このコードブロックを試すことをお勧めします

Scanner scan = new Scanner();
int i = Integer.parseInt(scan.nextLine());
double d = Double.parseDouble(scan.nextLine());
String s = scan.nextLine();

ダブル入力または整数入力の後に、これが文字列入力の読み取りをスキップしない(または、行の残りをスキップする単一のWordを読み取る)ことを望まない。

fb/ayur.sharmaで私に従ってください

0
Bittu Sharma

NextInt()メソッドの直後にnextLine()メソッドを使用する場合、nextInt()が整数トークンを読み取ることを思い出してください。このため、整数入力のその行の最後の改行文字は引き続き入力バッファーにキューイングされ、次のnextLine()は整数行の残り(空)を読み取ります。

0

これがあなたの答えです

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

    int i = scan.nextInt();         
    double d = scan.nextDouble(); 

    scan.nextLine();
    String s = scan.nextLine();  

    //if you want to skip the unnecessary input
    //scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

    System.out.println("String: " + s);       
    System.out.println("Double: " + d);      
    System.out.println("Int: " + i);

   scan.close();
}
0
Mukta