Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0

    Default I have an error that I cant fix

    In my java 171 class I have to do question 6 out of building java programs on page 297 the question goes like this "write a method called smallestLargest that accepts a Scanner for the console as a parameter and asks the user to enter numbers, then prints the smallest and largest of all the numbers supplied by the user. You may assume that the user enters a valid number greater than 0 for the number of numbers to read, Here is my code" I am getting a return statement error on line 36. can someone please help.


    import java.util.*;

    public class HW4 {
    public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    System.out.print("How many numbers do you have? ");
    int totalNumber = console.nextInt();
    if (totalNumber>0)
    smallestLargest(console);


    }
    public static int smallestLargest(Scanner console){
    int totalNumber = console.nextInt();
    System.out.print(" #" + 1 + "? ");
    int min = console.nextInt();
    int max = min;

    for (int i = 2; i <= totalNumber; i++) {
    System.out.print(" #" + i + "? ");
    int next = console.nextInt();
    if(next < min )
    min = next;

    if (next > max)
    max = next;


    }
    System.out.println("Min: " + min);
    System.out.println("Max: " + max);

    }

    }

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: I have an error that I cant fix

    Hi Kevin_hardwick, welcome to the forums.

    Please enclose your code in [code] tags, it makes reading and referencing specific line easier.
    Your problem is your declaration of the method smallestLargest(). Using the keyword 'int' indicates the method should return an interger value by using a 'return' statement.
    Alternatively, you could declare it void if you are not expecting the method to return anything.

    Regards.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: I have an error that I cant fix

    Please wrap your code in [code] tags [/code] so it retains its formatting.

    Looks to me like your smallestLargest method isn't returning anything.
    It is defined as:
    Java Code:
    public static int smallestLargest(Scanner console)
    That 'int' there says "I return an int".
    Re-reading your requirements it doesn't look like you are expected to return anything from this, so you could just have it return nothing.
    That would be 'void', whIch essentially means "I don't return anything".

    ETA: Gah! Beaten to it...
    And in almost exactly the same words!
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0

    Default Re: I have an error that I cant fix

    thank you i will try that, right away, now can you explain the [code] tags?

  5. #5
    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0

    Default Re: I have an error that I cant fix

    it is working now but the number i enter before it calls my method is not being used to determine the amount of numbers to ask for user input. any ideas?
    Java Code:
    // Finds the average of a sequence of numbers as well as
    // reporting how many of the numbers were negative.
    
    import java.util.*;
    
    public class HW4 {
        public static void main(String[] args) {
            Scanner console = new Scanner(System.in);
     System.out.print("How many numbers do you have? ");
     int totalNumber = console.nextInt();
            if (totalNumber>0) 
                 smallestLargest(console);
            
                 
        }
         public static void smallestLargest(Scanner console){
          int totalNumber = console.nextInt();
     System.out.print("    #" + 1 + "? ");
     int min = console.nextInt();
     int max = min;
         
     for (int i = 2; i <= totalNumber; i++) {
                System.out.print("    #" + i + "? ");
                int next = console.nextInt();
      if(next < min )
       min = next;
                
      if (next > max)
       max = next;
                
      
      }
       System.out.println("Min: " + min);
       System.out.println("Max: " + max);
      
            } 
    
    }

  6. #6
    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0

    Default Re: I have an error that I cant fix

    oops ignore the comment statements

  7. #7
    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0

    Default Re: I have an error that I cant fix

    Thank you very much for your help I fixed the code it looks like this now

    Java Code:
    
    import java.util.*;
    
    public class HW4 {
        public static void main(String[] args) {
            Scanner console = new Scanner(System.in);
     System.out.print("How many numbers do you have? ");
     int totalNumber = console.nextInt();
            if (totalNumber>0) 
                
                 smallestLargest(console, totalNumber);
            
                 
        }
         public static void smallestLargest(Scanner console,int totalNumber){
          //int totalNumber = console.nextInt();
     System.out.print("    #" + 1 + "? ");
     int min = console.nextInt();
     int max = min;
         
     for (int i = 2; i <= totalNumber; i++) {
                System.out.print("    #" + i + "? ");
                int next = console.nextInt();
      if(next < min )
       min = next;
                
      if (next > max)
       max = next;
                
      
      }
       System.out.println("Min: " + min);
       System.out.println("Max: " + max);
      
            } 
    
    }

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default Re: I have an error that I cant fix

    It looks sort of ok now but you should definitely fix your indentation.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0

    Default Re: I have an error that I cant fix

    Quote Originally Posted by JosAH View Post
    It looks sort of ok now but you should definitely fix your indentation.

    kind regards,

    Jos
    I would clean up my quotes to make it easier to read right? no other reason other then cosmetics.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default Re: I have an error that I cant fix

    Quote Originally Posted by Kevin_hardwick View Post
    I would clean up my quotes to make it easier to read right? no other reason other then cosmetics.
    It's not your 'quotes', your indentation is inconsistent (to say the least); it doesn't matter much for a small exercise program such as this but I for one refuse to plough through thousands of lines of code indented as badly as your code. Compare it to reading a novel without punctuation, capitalization etc. Don't under estimate those 'cosmetic'; consider this a tip.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: I have an error that I cant fix

    Quote Originally Posted by Tolls View Post
    ETA: Gah! Beaten to it...
    And in almost exactly the same words!
    :)
    What can I say? I've learnt from the best ;).

    Regards.

Similar Threads

  1. Replies: 3
    Last Post: 05-08-2012, 05:26 PM
  2. Replies: 1
    Last Post: 03-10-2010, 01:53 PM
  3. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  4. Replies: 1
    Last Post: 10-26-2009, 04:38 PM

Posting Permissions

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