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
  • 20,545
  • 20
  • 91
  • 102
Duha
  • 92
  • 1
  • 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
  • 8,558
  • 9
  • 32
  • 56
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
  • 404
  • 2
  • 15