[英]Not able to print a string using scanner
Here the input for string is " hi how are you" but I am getting output as "hi" alone. 在这里,字符串的输入是“嗨,你好吗”,但我的输出却是单独的“嗨”。 Do let me know where is the error.
让我知道错误在哪里。 I have tried out with nextLine() also.
我也尝试过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);
}
}
This is basic error only. 这仅是基本错误。 I browsed a lot still I am not able to sort out the error.
我仍然浏览了很多东西,但仍无法解决错误。
everytime you do 每次你做
int a = sc.nextInt();
double b = sc.nextDouble();
your scanner is not reading the newLine token, so you should do something like: 您的扫描仪没有读取newLine令牌,因此您应该执行以下操作:
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.
This code worked out for me. 这段代码对我有用。
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);
}
} }
Maybe this code will help you. 也许这段代码会为您提供帮助。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.