2016-07-04 4 views
0

文字列の入力は「こんにちは」ですが、出力は「hi」だけです。エラーがどこにあるか教えてください。私はnextLine()で試してみました。スキャナを使用して文字列を印刷することができません

import java.util.Scanner; 


public class Stringintalter { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     Scanner sc=new Scanner(System.in); 
     System.out.println("enter the num"); 
     int a = sc.nextInt(); 
     System.out.println("Enter the Double"); 
     double b = sc.nextDouble(); 
     System.out.println("Enter the string"); 
     String c = sc.next(); //tried out with nextLine() also. 

     System.out.println(c); 
     System.out.println(b); 
     System.out.println(a); 
    } 

} 

これは基本的なエラーです。私はまだ多くを閲覧しましたが、エラーを分類することはできません。たぶん、このコードがお手伝いします

 System.out.println("enter the num"); 
    int a = sc.nextInt(); 
    sc.next(); 
    System.out.println("Enter the Double"); 
    double b = sc.nextDouble(); 
    sc.next(); 
    System.out.println("Enter the string"); 
    String c = sc.next(); //tried out with nextLine() also. 
+0

トライsc.next()+ sc.nextLine()、これは –

+2

は 'int型、A = ...'の後に ''とダブルB)( 'sc.nextLineを追加していきます。 .. .. – Jens

+0

@SeekAddoうん、それはうまくいきます。どうもありがとう。 – saimanoj

答えて

1

毎回あなたがあなたのスキャナは、改行トークンを

int a = sc.nextInt(); 

    double b = sc.nextDouble(); 

読んでいませんか。

Scanner u = new Scanner(System.in); 

    Scanner sc=new Scanner(System.in); 
    System.out.println("enter the num"); 
    int a = sc.nextInt(); 
    System.out.println("Enter the Double"); 
    double b = sc.nextDouble(); 

    System.out.println(b); 
    System.out.println(a); 


    String c; 
    c = u.nextLine(); 
    System.out.println(c); 
0

:あなたのような何かをする必要がありますので

1

このコードは私のために機能しました。

import java.util.Scanner; 


public class Stringintalter { 

    public static void main(String[] args) { 

     Scanner sc=new Scanner(System.in); 
     System.out.println("enter the num"); 
     int a = sc.nextInt(); 
     System.out.println("Enter the Double"); 
     double b = sc.nextDouble(); 
     System.out.println("Enter the string"); 
     sc.nextLine(); 
     String c = sc.nextLine(); 
     System.out.println(c); 
     System.out.println(b); 
     System.out.println(a); 
    } 

}

関連する問題