Java 突然缩短的绳子

Java 突然缩短的绳子,java,string,jgrasp,Java,String,Jgrasp,我从用户那里读取了一个字符串。然后,当我使用字符串时,我看到它被缩短了。我不知道怎么做或者为什么?我需要整个字符串,而不仅仅是其中的一部分 代码如下: String exp=null; System.out.println("Enter an expression without nested parenthesis"); try { exp=read.next(); System.out.println("exp 1 : " + exp); } 运行示例: input: 1

我从用户那里读取了一个字符串。然后,当我使用字符串时,我看到它被缩短了。我不知道怎么做或者为什么?我需要整个
字符串
,而不仅仅是其中的一部分

代码如下:

String exp=null;
System.out.println("Enter an expression without nested parenthesis");
try {
    exp=read.next();
    System.out.println("exp 1 : " + exp); 
}
运行示例:

input: 13 + 45
示例输出为:

exp 1 : 13

您需要阅读整行内容:

String exp = null;
System.out.println("Enter an expression without nested parenthesis");
try {
    exp = read.nextLine();
    System.out.println("exp 1 : "+exp);
}

读取。下一步
只读取到第一个空格。

您需要读取整行:

String exp = null;
System.out.println("Enter an expression without nested parenthesis");
try {
    exp = read.nextLine();
    System.out.println("exp 1 : "+exp);
}
read.next
只读取到第一个空格。

使用next()只返回空格前面的内容

     String exp=null;
     System.out.println("Enter an expression without nested parenthesis");
     try {
        exp=read.nextLine(); //read line
        System.out.println("exp 1 : "+exp); 
     }
使用next()将只返回空格前面的内容

     String exp=null;
     System.out.println("Enter an expression without nested parenthesis");
     try {
        exp=read.nextLine(); //read line
        System.out.println("exp 1 : "+exp); 
     }

你想要
nextLine()
…你想要
nextLine()
…这确实有效,但为什么会发生这个问题?!,我总是使用read.next();它总是有效的@Duha,因为
next()
只将文本返回到输入中的下一个空格。这确实有效,但为什么会出现此问题?!,我总是使用read.next();它总是有效的@Duha,因为
next()
只将文本返回到输入中的下一个空格。