6 Replies - 204 Views - Last Post: 13 July 2011 - 07:50 AM Rate Topic: -----

#1 Rikka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 21-June 11

problem with not having return statement

Posted 12 July 2011 - 10:45 PM

class Judge{


    double judScores (double [] fiveScores){

        Scanner input = new Scanner (System.in);

        int conloop;

        for (conloop=0; conloop<fiveScores.length; conloop++){

                System.out.print("\t --> Enter Score for Contestant #" + conloop+1 + ":");
                fiveScores[conloop] = input.nextDouble();

         return fiveScores;
        }
    }


This code have an error of not having return statement but it has :( :whatsthat:

Is This A Good Question/Topic? 0
  • +

Replies To: problem with not having return statement

#2 kamran619  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 74
  • Joined: 05-July 11

Re: problem with not having return statement

Posted 12 July 2011 - 10:50 PM

View PostRikka, on 12 July 2011 - 10:45 PM, said:

class Judge{


    double judScores (double [] fiveScores){

        Scanner input = new Scanner (System.in);

        int conloop;

        for (conloop=0; conloop<fiveScores.length; conloop++){

                System.out.print("\t --> Enter Score for Contestant #" + conloop+1 + ":");
                fiveScores[conloop] = input.nextDouble();

         return fiveScores;
        }
    }


This code have an error of not having return statement but it has :( :whatsthat:

I think it says this because you are returning a type that is a double[], not double. A type double would be a specific element in the array. You either want to change your method header to take a double[] or change the code in the method. I hope this makes sense. Example:

I don't know if this method serves the purpose that you want but this would be a way to get rid of the error:
double[] judScores (double [] fiveScores){

or:

 double judScores (double [] fiveScores){

        Scanner input = new Scanner (System.in);

        int conloop;

        for (conloop=0; conloop<fiveScores.length; conloop++){

                System.out.print("\t --> Enter Score for Contestant #" + conloop+1 + ":");
                fiveScores[conloop] = input.nextDouble();

         return fiveScores[any element in the array];
         //this would return a double value as declared in your method
        }
    }

This post has been edited by kamran619: 12 July 2011 - 10:57 PM

Was This Post Helpful? 0
  • +
  • -

#3 saSBH2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 02-March 10

Re: problem with not having return statement

Posted 13 July 2011 - 01:17 AM

View PostRikka, on 12 July 2011 - 10:45 PM, said:

class Judge{


    double judScores (double [] fiveScores){

        Scanner input = new Scanner (System.in);

        int conloop;

        for (conloop=0; conloop<fiveScores.length; conloop++){

                System.out.print("\t --> Enter Score for Contestant #" + conloop+1 + ":");
                fiveScores[conloop] = input.nextDouble();

         return fiveScores;
        }
    }


This code have an error of not having return statement but it has :( :whatsthat:


Hi, as Kamran said you must have:
double[] judScores (double [] fiveScores)


And then "return fiveScores" should be out of for loop.
Was This Post Helpful? 0
  • +
  • -

#4 Rikka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 21-June 11

Re: problem with not having return statement

Posted 13 July 2011 - 07:33 AM

View PostsaSBH2, on 13 July 2011 - 01:17 AM, said:

View PostRikka, on 12 July 2011 - 10:45 PM, said:

class Judge{


    double judScores (double [] fiveScores){

        Scanner input = new Scanner (System.in);

        int conloop;

        for (conloop=0; conloop<fiveScores.length; conloop++){

                System.out.print("\t --> Enter Score for Contestant #" + conloop+1 + ":");
                fiveScores[conloop] = input.nextDouble();

         return fiveScores;
        }
    }


This code have an error of not having return statement but it has :( :whatsthat:


Hi, as Kamran said you must have:
double[] judScores (double [] fiveScores)


And then "return fiveScores" should be out of for loop.


I tried it. It turned to be a garbage value :(
Was This Post Helpful? 0
  • +
  • -

#5 kamran619  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 74
  • Joined: 05-July 11

Re: problem with not having return statement

Posted 13 July 2011 - 07:37 AM

View PostRikka, on 13 July 2011 - 07:33 AM, said:

View PostsaSBH2, on 13 July 2011 - 01:17 AM, said:

View PostRikka, on 12 July 2011 - 10:45 PM, said:

class Judge{


    double judScores (double [] fiveScores){

        Scanner input = new Scanner (System.in);

        int conloop;

        for (conloop=0; conloop<fiveScores.length; conloop++){

                System.out.print("\t --> Enter Score for Contestant #" + conloop+1 + ":");
                fiveScores[conloop] = input.nextDouble();

         return fiveScores;
        }
    }


This code have an error of not having return statement but it has :( :whatsthat:


Hi, as Kamran said you must have:
double[] judScores (double [] fiveScores)


And then "return fiveScores" should be out of for loop.


I tried it. It turned to be a garbage value :(

How are you checking what this value is? Recall when you print out an array you must loop through each element and print it. The garbage value you are referring to is the reference point in memory. System.out.println(judScores); will only give you the location judScores is allocated in the memory.. The values inside the array can be checked like this:
System.out.println("The values in judScores are:");
//loop through each element in the array and print it out
for (int i = 0; i < judScores.length; i++){
System.out.print(judScores[i] + " ");
}


My answer above came from the assumption that you printed out the array incorrectly. If you printed it out the way I did above and are still getting a garbage value(in relevance to your specific program) then please give a description of exactly what you're trying to do with that method and maybe we can help.

Hope I helped.

This post has been edited by kamran619: 13 July 2011 - 07:41 AM

Was This Post Helpful? 1
  • +
  • -

#6 Rikka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 21-June 11

Re: problem with not having return statement

Posted 13 July 2011 - 07:40 AM

View Postkamran619, on 13 July 2011 - 07:37 AM, said:

View PostRikka, on 13 July 2011 - 07:33 AM, said:

View PostsaSBH2, on 13 July 2011 - 01:17 AM, said:

View PostRikka, on 12 July 2011 - 10:45 PM, said:

class Judge{


    double judScores (double [] fiveScores){

        Scanner input = new Scanner (System.in);

        int conloop;

        for (conloop=0; conloop<fiveScores.length; conloop++){

                System.out.print("\t --> Enter Score for Contestant #" + conloop+1 + ":");
                fiveScores[conloop] = input.nextDouble();

         return fiveScores;
        }
    }


This code have an error of not having return statement but it has :( :whatsthat:


Hi, as Kamran said you must have:
double[] judScores (double [] fiveScores)


And then "return fiveScores" should be out of for loop.


I tried it. It turned to be a garbage value :(

How are you checking what this value is? Recall when you print out an array you must loop through each element and print it. The garbage value you are referring to is the reference point in memory. The values inside the array can be checked like this:
System.out.println("The values in judScores are:");
for (int i = 0; i < judScores.length; i++){
System.out.print(judScores[i] + " ");
}


Hope I helped.


Thank you so much! :bananaman: But, isn't it possible to pass the array itself to the main? :)
Was This Post Helpful? 0
  • +
  • -

#7 kamran619  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 74
  • Joined: 05-July 11

Re: problem with not having return statement

Posted 13 July 2011 - 07:50 AM

View PostRikka, on 13 July 2011 - 07:40 AM, said:

View Postkamran619, on 13 July 2011 - 07:37 AM, said:

View PostRikka, on 13 July 2011 - 07:33 AM, said:

View PostsaSBH2, on 13 July 2011 - 01:17 AM, said:

View PostRikka, on 12 July 2011 - 10:45 PM, said:

class Judge{


    double judScores (double [] fiveScores){

        Scanner input = new Scanner (System.in);

        int conloop;

        for (conloop=0; conloop<fiveScores.length; conloop++){

                System.out.print("\t --> Enter Score for Contestant #" + conloop+1 + ":");
                fiveScores[conloop] = input.nextDouble();

         return fiveScores;
        }
    }


This code have an error of not having return statement but it has :( :whatsthat:


Hi, as Kamran said you must have:
double[] judScores (double [] fiveScores)


And then "return fiveScores" should be out of for loop.


I tried it. It turned to be a garbage value :(

How are you checking what this value is? Recall when you print out an array you must loop through each element and print it. The garbage value you are referring to is the reference point in memory. The values inside the array can be checked like this:
System.out.println("The values in judScores are:");
for (int i = 0; i < judScores.length; i++){
System.out.print(judScores[i] + " ");
}


Hope I helped.


Thank you so much! :bananaman: But, isn't it possible to pass the array itself to the main? :)


Yes, that is possible. In your case, since you are modifying the array in another method you don't even have to pass it to the main method if you don't want to. When you pass the array back to the method that call it you can use it as normal. When you actually want to see the values for yourself you have to loop through the elements to print it.

Example:

public static void main(String[] args){
int[] test = {1,2};
System.out.println("The values of test before the swap are " + test[0] + " and " + test[1]);
swap(test);
System.out.println("The values of test after the swap are " + test[0] + " and " + test[1]);
}

public static void swap(int[] test){
int temp = test[0];
test[0] = test[1];
test[1] = temp;
}


This would still swap the values because the swap method modified the contents of the array at it's memory location. If you wanted to you could also pass the value to the main method.

public static void main(String[] args){
int[] test = {1,2};
System.out.println("The values of test before the swap are " + test[0] + " and " + test[1]);
int[] swapped = swap(test);
System.out.println("The values of test after the swap are " + swapped[0] + " and " + swapped[1]);
}

public static int[] swap(int[] test){
int temp = test[0];
test[0] = test[1];
test[1] = temp;
return test;
}

This post has been edited by kamran619: 13 July 2011 - 07:55 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1