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 4 of 4

Thread: Whats wrong here??

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Whats wrong here??

    It keeps saying that the method must return a result of type int but I already made return the result.


    import java.util.Scanner;
    public class chap9 {

    public static void main (String[] args) {


    String message = "statement";

    char letter = 'a';

    int num = count(message,letter);

    }

    public static int count(String str, char a){



    Scanner input = new Scanner(System.in);


    System.out.println("Enter the string");


    str = input.nextLine();
    int length = str.length();

    for(int i =0; i<length; i++) {

    a= 'a';


    int occurence =0;

    char b = str.charAt(i);

    if( b==a){

    occurence++;
    }




    return occurence;

    }
    }

    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Whats wrong here??

    saying that the method must return a result of type int
    Please copy the full text of the error message and paste it here.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    The unformatted code makes it hard to read and understand.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Whats wrong here??

    It says this method must return a result of type int.

    import java.util.Scanner;
    public class chap9 {
     
    public static void main (String[] args) {
     
     
    String message = "statement";
     
    char letter = 'a';
     
    int num = count(message,letter);
     
    }
     
    public static int count(String str, char a){
     
     
     
    Scanner input = new Scanner(System.in);
     
     
    System.out.println("Enter the string");
     
     
    str = input.nextLine();
    int length = str.length();
     
    for(int i =0; i<length; i++) {
     
    a= 'a';
     
     
    int occurence =0;
     
    char b = str.charAt(i);
     
    if( b==a){
     
    occurence++;
    }
     
     
     
     
    return occurence;
     
    }
    }
     
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Whats wrong here??

    The formatting needs to be fixed. Nested statements should be indented 3-4 spaces.
    All statements should not start in the first column.

    Make sure ALL possible exit points from the method have a return statement.
    With the poor formatting, the code is hard to read to see where the exit points are.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Whats going wrong here?
    By choll4c in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 24th, 2012, 03:39 PM
  2. WHATS WRONG
    By subodhkr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 8th, 2012, 07:47 AM
  3. Whats wrong here?
    By Java Sucks in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 8th, 2011, 08:33 PM
  4. whats wrong?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2011, 02:35 PM
  5. could you tell me whats wrong....
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 6th, 2010, 04:35 PM