Results 1 to 2 of 2
  1. #1
    sgar0807 is offline Member
    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    Question Can someone help me with a beginner's problem with java? (bluej)

    Hi, I am a beginner to java programming, and I am using bluej to code my programs for my class.
    I am trying to construct a calculator that works by having the user enter a equation in the form of a string with the scanner class, and then parse the Equation with stringtokenizer to answer their equation. However I have run into a slight problem on the last bit of coding. Does anyone know how to set an int variable equal to empty? I'm trying to set it sort of like String x = ""; so that the int is empty and therefore can be added to from the inside of a while loop. This is my code so far, my problem is in the parseEqn method.


    /**
    * Write a description of class M3L2T1 here.
    *
    * @author (your name)
    * @version (a version number or a date)
    */
    import java.util.Scanner;
    import java.util.StringTokenizer;
    class M3L2T1 {
    public static void main (String[] args) {
    Scanner sc = new Scanner (System.in);
    StringTokenizer x;
    String q = "";
    System.out.println ("This program was created by -.");
    System.out.println ();
    System.out.println ("Simple Calculator program");
    System.out.println ("This program will only accept a math expression with");
    System.out.println ("2 operands and 1 operator. Acceptable operators");
    System.out.println ("are +, -, *, /, and %. For example, 2*3 or 6%4.");
    System.out.print ("Please enter an expression ending with the character (;) or put q to quit, then press enter: ");
    String math = sc.nextLine();
    System.out.println ();
    while (!math.equals("q")) {
    System.out.printf("%s = %d%n", math, parseEqn(math));
    System.out.println ("Please enter an expression ending with the character (;) or put q to quit, then press enter: ");
    math = sc.nextLine();
    }
    }
    public static int parseEqn (String input) {
    int equation = // on this line is my problem
    StringTokenizer x = new StringTokenizer(input, "+-*/;");
    while (x.hasMoreTokens()) {
    String a = x.nextToken();
    int aInt = Integer.parseInt(a);
    String b = x.nextToken();
    int bInt = Integer.parseInt(b);
    if (a == ("+")) {
    int plus = ((aInt)+(bInt));
    return equation = plus;
    } else if (a==("-")) {
    int minus = ((aInt) - (bInt));
    return equation = minus;
    } else if (a == ("*")) {
    int multiply = ((aInt) * (bInt));
    return equation = multiply;
    } else if (a == ("/")) {
    int divide = ((aInt) / (bInt));
    return equation = divide;
    }
    }
    return equation;
    }
    }

  2. #2
    foulkelore is offline Member
    Join Date
    Dec 2012
    Location
    Des Moines, IA
    Posts
    35
    Rep Power
    0

    Default Re: Can someone help me with a beginner's problem with java? (bluej)

    First off welcome to the boards! Also its hard to read your code since you didn't use the code tags. String and ints are not the same thing. You can set an int to equal 0 like this.

    Java Code:
    int x = 0;
    Java Code:
    for (int i = 0; i < querySplit.length; i++) {
          String queryText = querySplit[i];
    }

Similar Threads

  1. java update problem(beginner:/)
    By vthimis in forum New To Java
    Replies: 3
    Last Post: 12-25-2013, 06:29 AM
  2. Replies: 2
    Last Post: 03-13-2012, 01:02 PM
  3. Beginner Java Problem
    By nighteagle2 in forum New To Java
    Replies: 2
    Last Post: 02-16-2012, 05:35 AM
  4. Beginner Java Problem (Running Objects)
    By marcwilliamson_ in forum New To Java
    Replies: 4
    Last Post: 04-24-2011, 06:45 AM
  5. Beginner Java Modding Problem...
    By walkineagle in forum New To Java
    Replies: 3
    Last Post: 04-13-2011, 05:20 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
  •