I am making a code for a project. When I input for example
Harry + John
Harry * James
finish
The output should be
HarryJohn
HarryHarryHarryHarryHarry
End
However, my current output is
HarryJohn
HarryHarryHarryHarryHarry
End
I have to press enter in order for the program to print "End" and finish. Is there any way for me to not press enter in order to finish running my code?
The following is the code I have written so far.
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String str1 = in.next();
while (!str1.equals("finish")) {
String op = in.next();
String str2 = in.next();
if (op.equals("+")){
System.out.println(str1 + str2);
}
else if (op.equals("*")){
for (int i = 0; i < str2.length(); i++){
System.out.print(str1);
}
System.out.println();
}
else {
System.out.println("wrong");
}
String blankStr = in.nextLine();
str1 = in.next();
}
System.out.println("End");
}