Results 1 to 2 of 2
  1. #1
    Propinquity is offline Member
    Join Date
    Oct 2013
    Rep Power

    Default How do you parse an inputted expression?

    Java Code:
    import java.util.Scanner;
    public class fractioncalcs {
    	public static void main(String[] args) {
    		//initialization of variables begins here
    		double fraction1;	
    		boolean repeat = true; 
    		//scanner object for keyboard input
    		Scanner keyboard = new Scanner(;
    		Scanner scanner = new Scanner(;
    		scanner.useDelimiter(" ");
    while (repeat) {
    	String input = null;
    	if ("quit".equals(input))
    		repeat = true; //continues the loops
    		repeat = false;//this breaks the infinite while loop
    	System.out.println("Enter an expression that you wish to evaluate.");
    	while (scanner.hasNext()){
    I'm having a lot of problems figuring out how to parse the expressions that are inputted into the program.

    What I'm trying to do is have a program with an input loop that quits correctly and echos the parts of the input in parsed form.

    I have the input loop, but I have absolutely no idea how to parse it. As you can see from my code, I've attempted using two scanner objects to accomplish this but I don't think I fully understand it since it's not working and my textbook isn't providing any insight into this.

    When I enter an expression into the program, I've tried to use whitespace as a delimiter, but for some reason, it only echos back the first two parts of the string, unless I type an additional white space after the string.

    For example (commas represent whitespaces):

    I enter: 3,+,4
    I receive: 3
    I enter: 3,+,4,
    I receive: 3

    This completely fails to work with fractions.
    For example:
    I enter: 2/3,+,4/5,
    I receive the error:
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at fractioncalcs.main(

    Could you please provide some clarification as to why this is happening? I have been trying to determine how to do this for some time and I have absolutely no idea how to continue.

    Ideally, I would like to accomplish a program that can do something along the lines of this:
    Use input is bolded below.
    Enter an expression. 1/4 + 1_1/2
    Fraction 1: 1/4
    Operation: +
    Fraction 2: 1_1/2
    Enter an expression. quit
    Last edited by Propinquity; 11-19-2013 at 02:41 PM.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    The Netherlands
    Rep Power

    Default Re: How do you parse an inputted expression?

    You probably should not parse the string as it comes in, but let the user enter the entire input in one go and then parse it as a whole using your favourite method. You could start with String.split(), maybe a RegExp could be useful here and there ("Now you have two problems" ™), writing a lexer/parser might be a bit too much at this point... :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Counting Occurences from Inputted Integers HELP!
    By ProgrammingNoob in forum New To Java
    Replies: 10
    Last Post: 09-27-2013, 02:59 PM
  2. How to check duplicate Strings inputted
    By javazers in forum New To Java
    Replies: 9
    Last Post: 09-02-2012, 02:30 PM
  3. Ordering Inputted Java Values
    By MichaelSpame in forum New To Java
    Replies: 2
    Last Post: 07-16-2012, 05:05 AM
  4. How to parse an expression with given format
    By mariaK in forum New To Java
    Replies: 2
    Last Post: 11-23-2011, 11:17 AM
  5. Replies: 2
    Last Post: 07-18-2011, 07:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts