Results 1 to 5 of 5
  1. #1
    Lexaras is offline Member
    Join Date
    Feb 2012
    Posts
    22
    Rep Power
    0

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

    Hey guys, Im trying to write program which counts odd and even numbers from array, but I got this error.
    Here is all my program, but I got error in line near which Ive wrote Errors :

    package test;

    public class ArgStats {
    static int total=0;
    static double average;
    static int i;
    static int odd;
    static int even;

    public static void main(String[] args) {

    if (args.length == 0) {
    System.out.println("Please supply one or more integers; only integers are allowed");

    }
    else{
    for( i = 0; i < args.length; i++) {
    try {
    total += Integer.parseInt(args[i].trim());
    }
    catch (NumberFormatException nfe){
    System.out.println("Please supply one or more integers; only integers are allowed");
    System.exit(1);
    }
    }
    for (int i = 0; i < args.length; i++) {
    if (args[i] % 2 != 0) { Errors!!!!!!!!!!!!!!!
    odd++;
    } else {
    even++;
    }
    }
    }
    average = ((double)total/i);
    System.out.println("Number of Arguments = " + i );
    for ( i = 0; i < args.length; i++) {
    System.out.println(args[i]);
    }
    System.out.println("Sum = " + total );
    System.out.println("Average = " + average);
    }
    }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

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

    The args variable is an array of Strings. You can't use the % operator on Strings.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Lexaras is offline Member
    Join Date
    Feb 2012
    Posts
    22
    Rep Power
    0

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

    Maybe u know other way to find odds/even in strings array? Or I should convert this array to int?;/

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

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

    You already have the code for converting a String to an int. Look at how you increment your total.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Lexaras is offline Member
    Join Date
    Feb 2012
    Posts
    22
    Rep Power
    0

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

    Thank u very much its works correct now. Cheers!

Similar Threads

  1. Replies: 3
    Last Post: 05-02-2011, 08:51 AM
  2. operator - cannot be applied to java.lang.String,int
    By cc11rocks in forum AWT / Swing
    Replies: 0
    Last Post: 02-11-2011, 04:33 AM
  3. Replies: 2
    Last Post: 11-26-2010, 03:12 AM
  4. Replies: 3
    Last Post: 12-01-2008, 06:40 AM
  5. Replies: 4
    Last Post: 11-27-2008, 07:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •