Java help please :)

JosephC

Paragon
Joined
Aug 22, 2009
Messages
1,703
Reaction score
1
FP$
599
Hey, need a bit of help with Java, I'm trying to make it calculate a string. For example:

String calc = "1+2+3";

I want to be able to evaluate what's in calc so it'll work out the answer to 1 + 2 + 3.
 
Not sure if this is what you and been a while since doing anything Java but..


No idea if that works sorry. I don't have JDK or anything on this laptop and I haven't used Java in around a year.





EDIT: Coded, not that it matters.
Code:
class AddThreeNumbers 
{
  public static int number1;
  public static int number1;
  public static int number3;

  public static void main(int 1, int 2, int 3) 
  {
  number1= 1;
  number2=2;
  number3=3;
  }
  public int Addition()
  {
  return(number1+number2+number3;
  }
  public static void main(String arg[])
  {
  AddThreeNumbers addthreenumbers = new AddThreeNumbers();
  AddThreeNumbers.main(1,2,3);
  int i= addthreenumbers.Addition();
  System.out.println("value is="+ i);
  }
}
 
Code:
public class stringCalc { //Public Class

	public static void main(String[] args) { //Main 
		
			int arrInt[] = new int[3]; //Create integer with a maximum of three
			arrInt[0] = 1; //Assign 1 as the default value
			int i = 1; //Create integer "i" equal to "1"
			while (i <= 2){ //While i is less than or equal to '2', perform the following
					arrInt[i] = i+1; //If above is true, arrInt will add 1 to i.
					i++; //This rules it out
		} //Ends While
			
			System.out.println(arrInt[0] + " + " + arrInt[1] + " + " + arrInt[2]); //Prints the value of the above
	} //Ends main

} //Ends stringCalc

I commented it for you as well.
Is that what you needed?

EDIT:
Karoshio, please use code tags for code. 🙂
 
Thank you karoshio -

But I would like to be able to do as many numbers as entered.

It's on a GUI where you can enter for example 1+8/6*8+2

How would I be able to have it do all of the calculations entered, I was thinking an array with a loop but I got a bit confused on the different calculation methods.

And Jake that looks a lot like what I need, I would be able to change the new int[3] to like 10 to allow a maximum of 10 calculations? and how would I be able to use other operands other than just +.

Thanks 🙂
 
Oh.. Are you basically saying you're creating a calculator in Java?

Confusing me with what you actually want. The fact it's 4am doesn't help that though.
 
You need a calculator, and when calculated you want it to display what you entered, instead of the answer?

It will take a lot of code to make a calculator in Java, but I can make Triangle Area calcs, Rectangle perimeter & area calcs, square area calcs, etc.
 
I've got the GUI sorted, basically the only part I'm struggling with is having it convert a String which could contain for example 1+2+3-4*5 to a math calculation (evaluate it). so It should be able to calculate that the above calculation is 10.

Ta for help 🙂
 
Yeah, you would need to write a bunch of conditions for a calculator.
 
If you have a number stored in a string variable, with the variable name stringNumber you can convert it to a double as follows;

Code:
String stringNumber = "123";
double doubleNumber = Double.parseDouble(stringNumber);

When you do this, the double variable called doubleNumber should have the value 123.0

You can convert a string to an integer as follows;

Code:
String stringNumber = "123";
int intNumber = Integer.parseInt(stringNumber);

Hopefully that works for you.
 
Thanks, is it not possible to do this with +, -, * or / inside the string? I have an idea how I can do it a different way with the help everyone here's giving me.
 
JosephC said:
...is it not possible to do this with +, -, * or / inside the string?...
I'm not aware of any way to do that. Java recognises a string as being text so it doesn't do calculations on strings. Is there any reason why you would not want to convert your strings to ints or doubles and then do the calculations?

You could convert your ins or doubles back to strings using String.valueOf()

By the way, what IDE are you using?
 
Back
Top Bottom