0

I read a string from the user. Then, when I use the string, I see it's being shortened. I don't know how or why? I need the whole String, not just a part of it!

This is the code:

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

An example run:

input: 13 + 45

The example output is:

exp 1 : 13
Nathaniel Ford
  • 16,853
  • 18
  • 74
  • 88
Duha
  • 82
  • 9

2 Answers2

2

You need to read the whole line:

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

read.next reads only until the first whitespace.

DevShark
  • 7,370
  • 7
  • 28
  • 52
0

Using next() will only return what comes before a space

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