Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-05-2012, 05:22 PM   PM User | #1
Jposemato
New to the CF scene

 
Join Date: Oct 2012
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Jposemato is an unknown quantity at this point
String to strings? Help please

Hi guys, Im pretty new to Java so bare with me as I make a complete fool of myself. Im trying to start out by making a simple calculator, capable of the functions plus, minus, divide, multiply. I had no trouble doing this within eclipse, however turning it into a windowed program made it a lot harder for me.

The basic problem im having is using the method:
String = JOptionPane.showInputDialog("text")

Mine looks like this:
Code:
inStr1 = JOptionPane.showInputDialog("Please Input an Equation\n" +
				"* = multiply, / = Divide,\n" +
				"Include spaces and Decimals (Using Doubles!)\n" +
				"For example: 2.0 + 4.2, Then ENTER!");
This works all fine and dandy however I can no longer perform math with this, as it is a string. I know how to use the Double.parseDoube(), however I need it to break a line like "2.0 + 4.2" into Double num1 = 2.0; String sign = +; Double num2 = 4.2;

A good chunk of the problematic area, As you can see I tried playing around with a few things but I am completely lost at this point lol, thanks in advance.
Code:
import java.util.*;
import javax.swing.*;
import java.io.*;
import java.util.regex.*;


public class Calculator {

    static Scanner console = new Scanner(System.in);
    
	public static void main(String[] args)
	{
//		System.out.println("Please Input an Equation\n" +
//				"* = multiply, / = Divide,\n" +     <--- Non-Windowed
//				"Include spaces and Decimals (Using Doubles!)\n" +
//				"For example: 2.0 + 4.2, Then ENTER!");

		double num1;
		double num2;
		double sum;
		String sign;
		String num1a;
		String num2a;
		String inStr1;
		String inStr2;
		String outStr;
		int repeats;
		num1a = "a";
		num2a = "b";
		sum = 1;
		
        inStr1 = JOptionPane.showInputDialog("Please Input an Equation\n" +
				"* = multiply, / = Divide,\n" +
				"Include spaces and Decimals (Using Doubles!)\n" +
				"For example: 2.0 + 4.2, Then ENTER!");
		String data = inStr1;
		String[] values = data.split(" ");
		num1a = String[0];
		sign = String[1];
		num2a = String[2];
		
 		num1 = Double.parseDouble(num1a);
		num2 = Double.parseDouble(num2a);
		
//		num1 = console.nextDouble();  <--- Old code (The none window
//		sign = console.next();                                   way)
//		num2 = console.nextDouble();
		if (sign.equals("+")){
			sum = num1 + num2;
		}
		if (sign.equals("-")){
			sum = num1 - num2;
		}
		if (sign.equals("*")){
			sum = num1 * num2;
		}
		if (sign.equals("/")){
			sum = num1 / num2;
		}
I believe the part here: String[] values = data.split(" "); is the proper way of doing it, however I don't think the String[0] part is right.
Jposemato is offline   Reply With Quote
Old 10-05-2012, 05:58 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,661
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
This isn't javascript, it's java. Moving to the Java forum.

Given the rules you have specified here, you can tokenize and cast the strings. Pulling from an array of String broken with a space is also sufficient.
This is almost entirely correct. The problem is you cannot pull from String[x] as String is a datatype, not a variable. You need to pull from values[x] instead.
Next to this, a try/catch should be used:
PHP Code:
        try
        {
            
String[] values data.split(" ");
            
num1a values[0];
            
sign values[1];
            
num2a values[2];
            
num1 Double.parseDouble(num1a);
            
num2 Double.parseDouble(num2a);
            
double dResult 0.0;
            if (
sign.equals("+"))
            {
                
dResult num1 num2;
            }
            else if (
sign.equals("-"))
            {
                
dResult num1 num2;
            }
            else if (
sign.equals("*"))
            {
                
dResult num1 num2;
            }
            else if (
sign.equals("/"))
            {
                
dResult num1 num2;
            }
            
JOptionPane.showMessageDialog(null"The result is: " dResult);
        }
        catch (
NumberFormatException ex)
        {
            
JOptionPane.showMessageDialog(null,
                    
"There has been an error in input: " ex.getMessage(),
                    
"Error"JOptionPane.ERROR_MESSAGE);
        }
        catch (
ArrayIndexOutOfBoundsException ex)
        {
            
JOptionPane.showMessageDialog(null,
                    
"There appears to be an error in input format (double op double expected)",
                    
"Error",
                    
JOptionPane.ERROR_MESSAGE);
        } 
Switch can also be used on the sign, but only if it's cast to a char (Java does not allow switches on objects, and String is an object not a primitive).
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
Jposemato (10-09-2012)
Old 10-09-2012, 06:19 AM   PM User | #3
Jposemato
New to the CF scene

 
Join Date: Oct 2012
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Jposemato is an unknown quantity at this point
Hey, didn't notice I posted it in Java Script. Thought I had it in Java Forums. My bad and sorry for the inconvenience.

That was extremely helpful and I learned something new :] The calculator now works fine for everything- Aside from input errors.

Two more questions I had though if someone could answer, I'll make a new thread for it though and link it here: http://www.codingforums.com/showthre...64#post1277864

Last edited by Jposemato; 10-09-2012 at 06:30 AM..
Jposemato is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:18 AM.


Advertisement
Log in to turn off these ads.