newbdeveloper
newbdeveloper

Reputation: 107

First user input attempt not working (Java)

I am trying to get the user input and when I put a value for the first attempt it will give me the question again "Enter the Expression (% to break):" although if the first attempt is blank it will give the correct error message "Expression cannot be blank".
So what I need is how to get the first attempt to work.

THANKS THIS HAS BEEN SOLVED!! I REALLY APPRECIATE IT!

package PrefixCalc;

import java.util.Scanner;

public class PrefixCalc {

    public static void main(String[] args) {
        Expressions calc;
        Scanner console = new Scanner(System.in);

        System.out.print("Enter the Expression(% to break): ");
        String line = console.nextLine();
        while (line.isEmpty()) {
            System.out.println("Expression cannot be empty!");
            System.out.print("Enter the Expression(% to break): ");
            line = console.nextLine();
        }
        while (!line.isEmpty()) {
            if (line.equals("%")) {
                break;
            } else {

                if (args.length > 0) {
                    System.out.println("Processing string " + args[0]);
                    calc = new Expressions(new Scanner(args[0]));
                } else {
                    System.out.print("Enter the Expression(% to break): ");
                    calc = new Expressions(new Scanner(console.nextLine()));   //System.in
                }

                System.out.println("\nInput as prefix expression:");
                calc.showPreFix();
                System.out.println("Result: " + calc.evaluate());
                System.out.println("infix expression:");
                calc.showInFix();
                System.out.println("...................................................");
            }


        }
    }
}

Upvotes: 0

Views: 259

Answers (2)

mdewitt
mdewitt

Reputation: 2534

In your while loop, you are not using the input you received from the console in your if statements, you are using args, the input into the main method, which is not the value the user inputted after being prompted with "Enter the Expression(% to break): ".

The problem is right here:

            if (args.length > 0) {  <--- args instead of line
                System.out.println("Processing string " + args[0]);
                calc = new Expressions(new Scanner(args[0]));
            } else {
                System.out.print("Enter the Expression(% to break): ");
                calc = new Expressions(new Scanner(console.nextLine()));   //System.in
            }

args.length is 0 because, presumably, you did not run this method with any commands. Because args is empty, you are going into the else statement and printing out the "Enter the Expression(% to break): " prompt a second time.

Just use the line variable in the if-statment and it should work fine. Like this:

            if (line.length > 0) {
                System.out.println("Processing string " + args[0]);
                calc = new Expressions(new Scanner(args[0]));
            } else {
                System.out.print("Enter the Expression(% to break): ");
                calc = new Expressions(new Scanner(console.nextLine()));   //System.in
            }

Also, you should probably reassign line to the new value you are getting after the second prompt. You need to do something with it to avoid an infinite loop. See @Kevin Bowersox's answer for an elegant solution to that problem.

Upvotes: 1

Kevin Bowersox
Kevin Bowersox

Reputation: 94489

The code is creating an infinite loop. The following while loop executes while line is not empty, however it will never be empty since the String line is never modified. At some point line needs to be assigned the value entered by the user, so that it can break appropriately if a % is entered. Also I'm not sure why the code uses the arguments passed into the main method, instead it should be using line.

  //if line contains anything other than `%` it causes an infinite loop
  while (!line.isEmpty()) {
            if (line.equals("%")) {
                break;
            } else {

                if (args.length > 0) {
                    System.out.println("Processing string " + args[0]);
                    calc = new Expressions(new Scanner(args[0]));
                } else {
                    System.out.print("Enter the Expression(% to break): ");
                    calc = new Expressions(new Scanner(console.nextLine()));   //System.in
                }

                System.out.println("\nInput as prefix expression:");
                calc.showPreFix();
                System.out.println("Result: " + calc.evaluate());
                System.out.println("infix expression:");
                calc.showInFix();
                System.out.println("...................................................");
            }
        }

FIX

  while (!line.isEmpty()) {
            if (line.equals("%")) {
                break;
            } 
                calc = new Expressions(new Scanner(line));
                System.out.println("\nInput as prefix expression:");
                calc.showPreFix();
                System.out.println("Result: " + calc.evaluate());
                System.out.println("infix expression:");
                calc.showInFix();
                System.out.println("...................................................");
                System.out.print("Enter the Expression(% to break): ");
                line = console.nextLine();

        }

Upvotes: 1

Related Questions