2017-06-27 8 views
0

JavaでStdInputおよびStdOutputを取得する構文は何でしょうか。ユーザーからの任意のデータ型(int、float、string)を任意の順序で受け入れます。

私はユーザーからの入力を受けなければなりません。これは、任意の順序と任意のデータ型(int、float、string)で入力できます。私のコードはそうですが、データ型をランダムな順序で受け入れる柔軟性は許されません。

Scanner sc = new Scanner(System.in); 
int x = sc.nextInt(); 
double y = sc.nextDouble(); 
sc.nextLine(); 
String s = sc.nextLine(); 

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

データタイプに関係なく、入力をどのようにして取得できますか?

+0

サンプル入力を行うことができます: 38.3 こんにちは -8 – Shabbir

+0

「文字列」タイプfまたはあなたのすべての入力 –

+0

@IvanPronin私は確信しています、いくつのエントリをユーザーが行います。どのようにそのことについて? – Shabbir

答えて

0

データの種類を任意の種類から判断する場合は、スキャナ(スキャナを使用する場合)の唯一のオプションはnextLine()(またはnext())です。これらのメソッドを使用すると、たとえば、あなたがしたいデータ型に解析できる文字列を返す:

String s = sc.nextLine(); 
// For an integer 
int i = Integer.parseInt(s); 
// For a double 
double i = Double.parseDouble(s); 
+0

これは問題があるかもしれません。入力が文字列かスマイリーな顔ならどうでしょうか?私は '試しとキャッチ'ブロックでそれを包みたいかもしれません。 –

0

あなたはスキャナののhasNext()メソッドを使用することができます。ただ、APIをご確認ください: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

import java.util.Scanner; 

public class Solution { 

    public static void main(String[] args) { 
      Scanner sc=new Scanner(System.in); 
      int x; 
      double y; 
      String s; 
      while(sc.hasNext()) { 
        if (sc.hasNextInt()) { 
         x = sc.nextInt(); 
        } else if (sc.hasNextDouble()) { 
         y = sc.nextDouble(); 
        } else if (sc.hasNextLine()) { 
         y = sc.nextNextLine(); 
        } 
        // and so on... 
      } 
    } 
} 
2

それはあなたが入力して欲しいものに依存します。

あなたができることの1つは、入力を文字列として受け取り、文字列の内容を検査することです。たとえば、parseInt()およびparseDouble()のメソッドを使用して、データ型を追跡してエラーを起こすことができます。このような何か:

try { 
    // Try to parse it as an integer 
    Integer.parseInt(input); 
} 
catch (NumberFormatException exc) { 
    try { 
     // Try to parse it as a double 
     Double.parseDouble(input); 
    } 
    catch (NumberformatException exc) { 
     // Else, it's a string 
    } 
} 

しかし、よりエレガントな方法以下:小数点セパレータは、ロケールに依存していることを

Scanner sc = new Scanner(System.in); 
while (true) { // Some condition 
    if (sc.hasNextInt()) { 
     int i = sc.nextInt(); 
     System.out.println("int: " + i); 
    } 
    else if (sc.hasNextDouble()) { 
     double d = sc.nextDouble(); 
     System.out.println("double: " + d); 
    } 
    else { 
     String s = sc.next(); 
     System.out.println("string: " + s); 
    } 
} 

注意。

0

Scannerあなたはそれに基づいて分岐することができますので、次のトークンがあるタイプかを決定するためにhasNextInthasNextDoubleのようなメソッドがあります。

Scanner sc = new Scanner(System.in); 
while (true) { 
    if (sc.hasNextInt()) { 
     int i = sc.nextInt(); 
     System.out.println("Int: " + i); 
    } else if (sc.hasNextDouble()) { 
     double d = sc.nextDouble(); 
     System.out.println("Double: " + d); 
    } else { 
     String s = sc.next(); 
     if (s.equals("END")) break; // stop condition 
     System.out.println("String: " + s); 
    } 
} 
0

あなたが何かのように、

Scanner in = new Scanner(System.in); 
in.useDelimiter(" "); 
while(in.hasNext()) { 
    String s = in.next(); 
    try { 
     Double d = Double.valueOf(s); 
     d += 1; 
     System.out.print(d); 
     System.out.print(" "); 
    } catch(NumberFormatException e) { 
     StringBuffer sb = new StringBuffer(s); 
     sb.reverse(); 
     System.out.print(sb.toString() + " "); 
    } 
} 
+0

情報をありがとう、どうすればループから抜け出すことができますか? 私は次のようなものを使うべきですか?Scanner.close(); – Shabbir

+0

入力が終了するとループから抜けるだけです。 .close()メソッドを使用する必要はありません。プログラムの終了前に使用することができます。 while条件のために.hasNext() – abstractnature

関連する問題