I hope you are all doing well. This is my first time on Stack Overflow, so I apologize in advance for any mistakes that I may make. So, let's get to it!
import java.math.BigInteger;
public class Classes {
static int i; // "i" is initialized
static int x = 200; // FYI, x is the upper limit for primes to be found (ex, in this case, 0 - 200)
public static void main(String[] args) {
for (i = 0; i < x;) { // "i" is assigned the value of 0
BigInteger one = BigInteger.valueOf(i); // The following lines find the prime(s)
one = one.nextProbablePrime(); // Order by which primes are found - int > BigInteger > int
i = one.intValue(); //'i" is re-assigned the a value
if (i >= x) {
System.exit(i);
}
switch (i) {
case i < 100: // ERROR HERE, "Type mismatch: cannot convert from boolean to int"
hex();
break;
case i > 100: // ERROR HERE, "Type mismatch: cannot convert from boolean to int"
baseTen();
break;
}
}
}
static void hex() { //Probably unimportant to solving the problem, but this is used to convert to hex / print
String bla = Integer.toHexString(i);
System.out.println(bla);
}
static void baseTen(){ //Probably unimportant to solving the problem, but this is used print
System.out.println(i);
}
}
I wrote the above code as a practice piece while learning Java and have been using it since to practice and play around with Java. The program was made to find prime numbers and has been working for some time now.
Ever since I decided to try out switch statements, I have been having problems. When I go to run the code, the Eclipse IDE says "Type mismatch: cannot convert from boolean to int" and because of this my program refuses to run.
I have commented my code with the points at which I cast types and nowhere do I cast "i" into the type "boolean". If you have any idea as to why this problem occurs, please let me know. If you require any additional info, please do ask! Thank You!
switch(i){
can only switch for single values of i
for each case.
Use the following instead:
if(i < 100){
hex();
}
if(i > 100){
baseTen();
}
I'd also handle the i==100
case as well. This is left as a simple exercise to the reader.
You can only switch
for an Enum
or if you have distinct int
values that you only care about single value cases, not ranges.