okey, turns out it first calculates it and then puts the result in the String
public static void main(String[] args){
String string = "" + ((85+6) + 9*5);
//string == "136" allready
int i = new Integer(string);
System.out.println(i);
}
Thing is that i want to create a "more flexible" way of input by letting the user litterally give (85+6) + 9*5 in as String so
String = " (85+6) + 9*5";
int i = new Integer(string);
this ain't gonna do it. :mad:
So it turned out that i just wrote 96 lines (inclucing '}' and whitespaces) to get it solving ^^
now it gives me a warning:
method 'solve' is too complexe to analyze by data flow algorithm.
this inspection reports those conditions in the specified inspection scope that are always true or false, as well as points out where a runtimeexception may be thrown, based on data flow analysis of the code
Should i worry? (it runs perfect after all)
the complex method :p
few dutch words:
haakjes = brackets.
kars is the abreviation of karakters = character in dutch
(yes i know caracter is spelled wrong)
^Criticism is allowed ^^ just give some comments with it. Not just "you should change this in that" but also mention why.
private void solve(){
String string = textfield.getText();
char[] kars = string.toCharArray();
ArrayList<String> good = new ArrayList<String>();
List tempArray;
boolean haakjes, digit = false, decimal = false, caracter = false;
int firstIndex, lastIndex, j=1;
double first, last, temp=0;
for(int i=0 ; i<kars.length ; i++){
if(Character.isDigit(kars[i]) || kars[i]=='.' || kars[i]==','){
if(digit){
if(decimal){
good.set(good.size()-1, "" + (Double.parseDouble(good.get(good.size()-1)) + (Double.parseDouble(""+kars[i])/(10*j))));
j++;
}
else{
if(kars[i]!=',' && kars[i]!='.'){
good.set(good.size()-1, "" + (Double.parseDouble(good.get(good.size()-1))*10 + Double.parseDouble(""+kars[i])));
}
}
if(kars[i]=='.' || kars[i]==','){
decimal = true;
}
}
else{
good.add(""+ kars[i]);
digit = true;
}
caracter = false;
}
else{
if(caracter && kars[i]!='(' && kars[i]!=')'){
good.set(good.size()-1, good.get(good.size()-1) + kars[i]);
}
else{
good.add("" + kars[i]);
digit = false;
decimal = false;
j=1;
}
if(Character.isLetter(kars[i]))
caracter = true;
}
}
while(good.size()>1){
haakjes = false;
if(good.contains(")") && good.contains("(")){
lastIndex = good.indexOf(")");
tempArray = good.subList(0,lastIndex);
firstIndex = tempArray.lastIndexOf("(");
if(firstIndex +4 == lastIndex)
haakjes = true;
}
else{
firstIndex = -1;
lastIndex = good.size();
haakjes = false;
}
for(int k=firstIndex+1 ; k<lastIndex; k++){
if(good.get(k).equals("/") || good.get(k).equals("*")){
firstIndex = k-2;
lastIndex = k+2;
break;
}
}
first = Double.parseDouble(good.get(firstIndex +1));
last = Double.parseDouble(good.get(firstIndex +3));
if(good.get(firstIndex +2).equals("+"))
temp = first + last;
else if(good.get(firstIndex +2).equals("-"))
temp = first - last;
else if(good.get(firstIndex +2).equals("/"))
temp = first / last;
else if(good.get(firstIndex +2).equals("*"))
temp = first * last;
good.remove(firstIndex+1);
good.remove(firstIndex+1);
good.remove(firstIndex+1);
good.add(firstIndex+1, "" + temp);
if(firstIndex+4 == lastIndex && haakjes){
good.remove(firstIndex);
good.remove(firstIndex+1);
}
}
solution.setText("\n" + string +" = " + good.get(0));
textfield.setText("");
}
To test this code just change the 2nd last line into a System.out.println() and scan something in the String (first line). Should be all you need to change to get it working without my GUI class
tomorrow or so, squareroot, exponentiation, tan, cos, sin, etc :lol:
hopefully won't give me as much problems and debugging as today.
Edited by wim DC, 05 October 2009 - 11:26 AM.