Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Operator % cannot be applied to java.lang.String,int?

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    35
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Operator % cannot be applied to java.lang.String,int?

    i cant help myself...
    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");
                    }
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    35
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Operator % cannot be applied to java.lang.String,int?

    hmmm.. i use this?

    a=Integer.valueOf(String);

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Operator % cannot be applied to java.lang.String,int?

    Try it and see what happens:
    System.out.println("valueOf123=" + Integer.valueOf("123"));

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    35
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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
    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 ^_^
    Last edited by helloworld922; July 12th, 2011 at 10:12 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Operator % cannot be applied to java.lang.String,int?

    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.

  7. #7
    Member
    Join Date
    Jun 2011
    Posts
    35
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Operator % cannot be applied to java.lang.String,int?

    i must say all characters except from 0-9/numbers should be invalid.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Operator % cannot be applied to java.lang.String,int?

    What does your program do if you enter invalid data?

  9. #9
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default 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.
    Improving the world one idiot at a time!

Similar Threads

  1. Replies: 1
    Last Post: January 15th, 2010, 01:32 AM
  2. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM
  3. he operator / is undefined for the argument type(s) String, int
    By mtbr00x in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 8th, 2009, 08:34 PM
  4. How to reverse a String using java.lang.StringBuilder
    By JavaPF in forum Java SE API Tutorials
    Replies: 0
    Last Post: July 22nd, 2009, 09:42 AM
  5. [SOLVED] Facing java error "cannot be applied to (java.lang.String)"
    By tazjaime in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2009, 10:19 AM