Operator % cannot be applied to java.lang.String,int?
i cant help myself...
Code Java:
import java.io.*;
public class A
{
public static void main (String arum[])throws IOException{
BufferedReader tag=new BufferedReader (new InputStreamReader(System.in));
String a;
int b;
System.out.println();
System.out.println();
System.out.println();
System.out.println("Enter any number: ");
a=tag.readLine();
b=a%2;
System.out.println();
System.out.println();
if(b==1)
{
System.out.println("The number you entered is ODD");
}
else if(b<0)
{
System.out.println("The number you entered is NEGATIVE");
}
else if(a==0)
{
System.out.println("The number you entered is ZERO");
}
else if(b==0)
{
System.out.println("The number you entered is EVEN");
}
else
{
System.out.println("You have entered INVALID VALUES");
}
}
}
Re: Operator % cannot be applied to java.lang.String,int?
What are you trying to do with a String and the % operator?
What would the results of "hi there" % 4 be?
It makes no sense.
If the String contains numeric digits: "123" then use an Integer class method to convert it to an int value that will work with the % operator.
Re: Operator % cannot be applied to java.lang.String,int?
hmmm.. i use this?
a=Integer.valueOf(String);
Re: Operator % cannot be applied to java.lang.String,int?
Try it and see what happens:
System.out.println("valueOf123=" + Integer.valueOf("123"));
Re: Operator % cannot be applied to java.lang.String,int?
how can i declare that the value entered is invalid? it now can say ODD, NEGATIVE, EVEN, ZERO
Code Java:
import java.io.*;
public class A
{
public static void main (String arum[])throws IOException{
BufferedReader in=new BufferedReader (new InputStreamReader(System.in));
int a;
System.out.println();
System.out.println();
System.out.println();
System.out.println("Enter any number: ");
a=Integer.parseInt(in.readLine());
System.out.println();
System.out.println();
if(a % 2 == 1)
{
System.out.println("The number you entered is ODD");
}
else if(a % 2 < 0)
{
System.out.println("The number you entered is NEGATIVE");
}
else if(a == 0)
{
System.out.println("The number you entered is ZERO");
}
else if(a % 2 == 0)
{
System.out.println("The number you entered is EVEN");
}
else
{
System.out.println("You have entered INVALID VALUES");
}
}
}
Thanks for always being there Sir norms ^_^
Re: Operator % cannot be applied to java.lang.String,int?
Quote:
how can i declare that the value entered is invalid
First you must define what values are invalid.
Then you can write code to detect them.
Re: Operator % cannot be applied to java.lang.String,int?
i must say all characters except from 0-9/numbers should be invalid.
Re: Operator % cannot be applied to java.lang.String,int?
What does your program do if you enter invalid data?
Re: Operator % cannot be applied to java.lang.String,int?
I have a problem with the logic of your program. If a user enters -1 (which is ODD and NEGATIVE) the output will be ODD only.