View Full Version : Java: Is there a way to write this shorter
I know I could use a case statement but is their a way I can write this piece of java code quickly with an if statement, rather than having to write it and make it look like this:
if (mainCalculation.charAt(counter) == '1' ||
mainCalculation.charAt(counter) == '2' ||
mainCalculation.charAt(counter) == '3' ||
mainCalculation.charAt(counter) == '4' ||
mainCalculation.charAt(counter) == '5' ||
mainCalculation.charAt(counter) == '6' ||
mainCalculation.charAt(counter) == '7' ||
mainCalculation.charAt(counter) == '8' ||
mainCalculation.charAt(counter) == '9' ||
mainCalculation.charAt(counter) == '0' ||
mainCalculation.charAt(counter) == '/' ||
mainCalculation.charAt(counter) == '*' ||
mainCalculation.charAt(counter) == '-' ||
mainCalculation.charAt(counter) == '+' ||
mainCalculation.charAt(counter) == ' ')
E.g. a way so that I don't always have to write "mainCalculation.charAt(counter)" first. I'm saying a if statement as it's a preferable option for me and later on it might be the only option.
For those who don't speak Java (:p) it's basically doing this "Is the first character of this string equal to 1 OR is the first character of this string equal to 2 OR is the first character of this string equal to 3" etc and as the counter increments it changes from "first character" to "second character" and then "third character" etc.
Lorem-Ipsum
19-02-11, 19:00
I don't know java very well at all, but have a look at case.
I think this is what you were trying to do.
char[] chars = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '/', '*', '-', '+',};
for (char c : chars) {
for (int i = 0; i < mainCalculation.length(); i++) {
if (mainCalculation.charAt(i) == c) {
System.out.println("Word contains character: " + c);
}
}
}
hmm, Java .... it's been a while
boolean mainCalc = false;
expr = new Regex("[1-9][/] [-][+]");
mainCalc = expr.didMatch(mainCalculation.charAt(counter));
if(mainCalc)
continue.......
That will need testing, it's been a long time :D
DT.
hmm, Java .... it's been a while
boolean mainCalc = false;
expr = new Regex("[1-9][/] [-][+]");
mainCalc = expr.didMatch(mainCalculation.charAt(counter));
if(mainCalc)
continue.......
That will need testing, it's been a long time :D
DT.
Not sure of the actual syntax off the top of my head for the Java, but the regex you should probably change slightly to this:
expr = new Regex("^[1-9\/\*\-\+]$");
For the simple reason that /, *, - and + are usually reserved characters which need escaping, and also because your previous regex said to look for 5 characters in a row, any number 0-9 followed by /*-+
Oh great posts guys, will try them soon cba with programming at the moment :p.
Ok tried out some:
@Ryan your idea is good and it's probably the one I understand the most, however it is still slightly long and considering I'm already using a lot of for loops and if statements it's getting a little messy.
@DoubleTop
Your method seems to be a slightly better solution as it's less messy but like you said their are problems with the syntax causing errors with the words "expr" and "Regex" which I have no idea what they do lol.
@Peter
Your fix gets rid of the errors with "expr" and "Regex" but causes a new error saying: "Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\)". on the actual ("^[1-9\/\*\-\+]$") part
Added after 11 minutes:
Btw I tried something like this:
if (mainCalculation.charAt(counter) == ('1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | '0' | '/' | '*' | '-' | '+'))
Since the || operator was not allowed in that type of format I used the bitwise operator for OR | but the only problem is I have never used it before and have no idea how it works. Also after compiling and testing it, it does NOT work the same way as the first statement (in my OP). I was possibly looking for something as short as that, if it's possible?
Alright, this should fix it:
if (Pattern.matches("^[0-9/*-+]$", mainCalculation.charAt(counter)))Don't forget to add this at the top of your java file:
import java.util.regex.*;Don't use bitwise, it converts the value to a binary format and then performs a bitwise comparison on them using the method you selected, so for example, 6 | 10 = 14. The reason for this is the following:
6 in binary is - 0110
10 in binary is - 1010
0 OR 1 = 1
1 OR 0 = 1
1 OR 1 = 1
0 OR 0 = 0
So new binary value = 1110 = 14.
If you don't understand what I just said then don't use them. :P
Alright, this should fix it:
if (Pattern.matches("^[0-9/*-+]$", mainCalculation.charAt(counter)))Don't forget to add this at the top of your java file:
import java.util.regex.*;Don't use bitwise, it converts the value to a binary format and then performs a bitwise comparison on them using the method you selected, so for example, 6 | 10 = 14. The reason for this is the following:
If you don't understand what I just said then don't use them. :P
Ahh ok I'll try it out soon, I understand binary, binary addition etc, yes, but it's a bit complicated putting it in with programming so I'll stay away from that for now :D.
Powered by vBulletin® Version 4.1.12 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.