Announcement

Collapse
No announcement yet.

Java Help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Java Help

    Help!
    My boyfriend has been trying to figure out this program for his java class for over a week..to no avail. The teacher won't help him and he's at his wits end. I think it's supposed to be a calculator program. He says he's having trouble getting the operator character to read into the program. Can any of you figure this out? Here's what he has for his program so far:
    import java.util.StringTokenizer;

    import java.text.DecimalFormat;

    import javax.swing.JOptionPane;

    import java.io.*;


    public class Calculator

    {

    public static void main(String [] arg) throws IOException


    {

    BufferedReader keyboard = new
    BufferedReader(new InputStreamReader(System.in));


    StringTokenizer tokenizer;

    int results, results2, results3, results4, num1, num2/*, operator*/, test;

    String operator;//, num1, num2;

    String pane1, pane2;


    pane1 = JOptionPane.showInputDialog("The purpose of this program is to mimic a calculator."
    + "\n\n\nPlease enter the data in this format | 1 + 2 | .");



    tokenizer = new StringTokenizer (pane1);

    num1 = Integer.parseInt(tokenizer.nextToken());

    operator = tokenizer.nextToken();

    num2 = Integer.parseInt(tokenizer.nextToken());

    test = (char) '+';

    results = (num1 + num2);

    results2 = (num1 - num2);

    results3 = (num1 * num2);

    results4 = (num1 / num2);



    if (operator = test )
    JOptionPane.showMessageDialog(null, "Your results are " + results + ".", "Your results are...", JOptionPane.INFORMATION_MESSAGE);
    /*
    else if (test == '-')
    JOptionPane.showMessageDialog(null, "Your results are " + results2 + ".", "Your results are...", JOptionPane.INFORMATION_MESSAGE);

    else if (test == '*')
    JOptionPane.showMessageDialog(null, "Your results are " + results3 + ".", "Your results are...", JOptionPane.INFORMATION_MESSAGE);

    else if (test == '/')
    JOptionPane.showMessageDialog(null, "Your results are " + results4 + ".", "Your results are...", JOptionPane.INFORMATION_MESSAGE);



    switch (operator)
    {
    case '+' : JOptionPane.showMessageDialog(null, "Your results are " + results + ".", "Your results are...", JOptionPane.INFORMATION_MESSAGE);

    }

    */






    System.exit(0);

    }





    }
    "Speaking on the subject of conformity: This rotting concept of the unfathomable nostril mystifies the fuming crotch of my being!!! Stop with the mooing you damned chihuahua!!! Ganglia!! Rats eat babies!" ~ happy noodle boy

  • #2
    Try this
    Code:
    import java.util.StringTokenizer;
    
    import java.text.DecimalFormat;
    
    import javax.swing.JOptionPane;
    
    import java.io.*;
    
    
    public class Calculator
    
    {
    
    public static void main(String [] arg) throws IOException
    
    
    {
    
    BufferedReader keyboard = new
    BufferedReader(new InputStreamReader(System.in));
    
    
    StringTokenizer tokenizer;
    
    int results, results2, results3, results4, num1, num2/*, operator*/, test;
    
    String operator;//, num1, num2;
    
    String pane1, pane2;
    
    
    pane1 = JOptionPane.showInputDialog("The purpose of this program is to mimic a calculator."
    + "\n\n\nPlease enter the data in this format | 1 + 2 | .");
    
    
    
    tokenizer = new StringTokenizer (pane1);
    
    num1 = Integer.parseInt(tokenizer.nextToken());
    
    operator = tokenizer.nextToken();
    
    num2 = Integer.parseInt(tokenizer.nextToken());
    
    //test = (char) '+';
    
    results = (num1 + num2);
    
    results2 = (num1 - num2);
    
    results3 = (num1 * num2);
    
    results4 = (num1 / num2);
    
    
    
    if (operator.equals("+") )
    JOptionPane.showMessageDialog(null, "Your results are " + results + ".", "Your results are...", JOptionPane.INFORMATION_MESSAGE);
    /*
    else if (operator.equals("-"))
    JOptionPane.showMessageDialog(null, "Your results are " + results2 + ".", "Your results are...", JOptionPane.INFORMATION_MESSAGE);
    
    else if (operator.equals("*"))
    JOptionPane.showMessageDialog(null, "Your results are " + results3 + ".", "Your results are...", JOptionPane.INFORMATION_MESSAGE);
    
    else if (operator.equals("/"))
    JOptionPane.showMessageDialog(null, "Your results are " + results4 + ".", "Your results are...", JOptionPane.INFORMATION_MESSAGE);
    
    
    */
    
    
    
    
    
    
    System.exit(0);
    
    }
    
    
    
    
    
    }
    "The issue is there are still many people out there that use religion as a crutch for bigotry and hate. Like Ben."
    Ben Kenobi: "That means I'm doing something right. "

    Comment


    • #3
      Was he suppost to write a program that acts like a caculator. I had to do the same thing in my java intro class. Asher program is a better way going about it.
      Donate to the American Red Cross.
      Computer Science or Engineering Student? Compete in the Microsoft Imagine Cup today!.

      Comment


      • #4
        asher: thank you thank you thank you!
        "Speaking on the subject of conformity: This rotting concept of the unfathomable nostril mystifies the fuming crotch of my being!!! Stop with the mooing you damned chihuahua!!! Ganglia!! Rats eat babies!" ~ happy noodle boy

        Comment


        • #5
          And they said Asher wasn't a gawd. HA!
          Life is not measured by the number of breaths you take, but by the moments that take your breath away.
          "Hating America is something best left to Mobius. He is an expert Yank hater.
          He also hates Texans and Australians, he does diversify." ~ Braindead

          Comment


          • #6
            See Asher knows all that open source crap too
            We the people are the rightful masters of both Congress and the courts, not to overthrow the Constitution but to overthrow the men who pervert the Constitution. - Abraham Lincoln

            Comment


            • #7
              The guy who made Java is an alumnus of my uni, we were forced at gunpoint to use Java in our first year due to an abundance of Sun equipment and software.

              dm: no problem.

              Tell him he should catch that IOException he throws if he wants full marks.
              "The issue is there are still many people out there that use religion as a crutch for bigotry and hate. Like Ben."
              Ben Kenobi: "That means I'm doing something right. "

              Comment


              • #8
                I'm also confused why he needs a BufferedReader and stuff, too.
                "The issue is there are still many people out there that use religion as a crutch for bigotry and hate. Like Ben."
                Ben Kenobi: "That means I'm doing something right. "

                Comment


                • #9
                  Because I'm anal and bored at work: a clean version of that program.
                  Code:
                  import java.util.StringTokenizer;
                  import javax.swing.JOptionPane;
                  
                  public class Calculator 
                  {
                  	public static void main(String[] arg)
                  	{
                  		StringTokenizer tokenizer;
                  		int num1, num2, result;
                  		String operator, pane1, pane2;
                  
                  		pane1 = JOptionPane.showInputDialog("The purpose of this program is to mimic a calculator."
                  				+ "\n\n\nPlease enter the data in this format | 1 + 2 | .");
                  		tokenizer = new StringTokenizer(pane1);
                  
                  		num1 = Integer.parseInt(tokenizer.nextToken());
                  		operator = tokenizer.nextToken();
                  		num2 = Integer.parseInt(tokenizer.nextToken());
                  
                  		if (operator.equals("+"))
                  			result = num1 + num2;
                  		else if (operator.equals("-"))
                  			result = num1 - num2;	
                  		else if (operator.equals("*"))
                  			result = num1 * num2;
                  		else
                  			result = num1 / num2;	
                  		JOptionPane.showMessageDialog(null,
                  			"Your results are " + result + ".",
                  			"Your results are...",
                  			JOptionPane.INFORMATION_MESSAGE);
                  		
                  		System.exit(0);
                  	}
                  }
                  Last edited by Asher; October 5, 2003, 18:57.
                  "The issue is there are still many people out there that use religion as a crutch for bigotry and hate. Like Ben."
                  Ben Kenobi: "That means I'm doing something right. "

                  Comment


                  • #10
                    Originally posted by Asher
                    I'm also confused why he needs a BufferedReader and stuff, too.
                    Because using the stupid popup windows as input boxes is gay?

                    Comment


                    • #11
                      But popup windows and input boxes don't need a buffered reader, nor do they throw IOExceptions.
                      "The issue is there are still many people out there that use religion as a crutch for bigotry and hate. Like Ben."
                      Ben Kenobi: "That means I'm doing something right. "

                      Comment


                      • #12
                        So?

                        It looks a lot prettier if you do input from the console.

                        Plus, the chances of it throwing an IOException is nil, unless you've got something seriously wrong anyways.

                        Comment


                        • #13
                          The point was he didn't need a buffered reader or an IOException since he wasn't using console input.
                          "The issue is there are still many people out there that use religion as a crutch for bigotry and hate. Like Ben."
                          Ben Kenobi: "That means I'm doing something right. "

                          Comment


                          • #14
                            Yes he was. Wasn't he?

                            EDIT: n/m, you're right.

                            Comment

                            Working...
                            X