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

Thread: Problem with returning integer values

  1. #1
    Junior Member
    Join Date
    Nov 2018
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with returning integer values

    Hello! I am finishing up my final project for my Intro to Computer Science class that requires me to create a menu method that utilizes other class programs. One of the classes is stressing me out the most and its because I cant figure out a way to return several integers. Would anyone be able to help me come up with the lines of code that allow me to return the "Ball" values of the program as a list?

     
    import java.util.Random;
    import java.util.*;
     
    public class LottoClass
    {
       public static int Lottery()
       {
       int ball1 = 0;
       int ball2 = 0;
       int ball3 = 0;
       int ball4 = 0;
       int ball5 = 0;
       int powerBall = 0;
     
       ball1 = (int)(Math.random()*59) + 1;
       powerBall = (int)(Math.random()*32) + 1;
     
       while (ball2 == ball1 || ball2 == ball3 || ball2 == ball4 || ball2 == ball5
            || ball3 == ball1 || ball3 == ball4 || ball3 == ball5 || 
            ball4 == ball5 || ball4 == ball1 ||  
           ball5 == ball1 ) 
       {
         ball2 = (int)(Math.random()*59) + 1;
         ball3 = (int)(Math.random()*59) + 1;
         ball4 = (int)(Math.random()*59) + 1;
         ball5 = (int)(Math.random()*59) + 1;
       }
     
       /**
       return ball1, ball2, ball3, ball4, ball5
       */
     
       return;
     
          }
       public static int Powerball()
       {
          int powerBall = 0;
     
          powerBall = (int)(Math.random()*32) + 1;
     
          return (powerBall);
       }   
     
    }
    Last edited by Spootdude; December 1st, 2018 at 03:04 PM.

  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: Problem with returning integer values

    return the "Ball" values of the program as a list?
    Define a List like an ArrayList, add the "Ball" values to the list and return the list.
    Define the method to return a List instead of an int.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Problem with returning integer values

    In addition to what Norm said, if, say in the future, you want to return several arbitrary values (e.g. a String, an int, and a List) all at once, simply create a simple class to house the values and return an instance of that.

    Regards,
    Jim

  4. #4
    Junior Member
    Join Date
    Nov 2018
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with returning integer values

    Thank you so much for the replies, and I was able to have the class return the array list values, however I'm having trouble in my main program access these values. I/m not able to define it as an int so how do I go by doing this to print it?

     
       /**
       Part of class code
       */
     
       ArrayList<Integer> numbers = new ArrayList<Integer>();
       numbers.add(ball1);
       numbers.add(ball2);
       numbers.add(ball3);
       numbers.add(ball4);
       numbers.add(ball5);
     
       return (numbers);

     
    /** 
    Part of main code
    */
     
    public static void LottoUI() 
       {
          Scanner userInput = new Scanner(System.in);
     
          System.out.println();
          System.out.println("Hello, here's your lottery numbers: ");
     
          LottoClass myLotto = new LottoClass();
     
          Integer LTO = myLotto.Lotto();
          /**
          Add list of arrays
          */
     
          int PB = myLotto.Powerball();
     
          System.out.print(LTO);
     
          System.out.println("Power ball number is: " + PB);
          System.out.println();
          System.out.println();
     
     
       }


    --- Update ---

    Not more than 5 minutes after I was able to figure it out! Thank you for the help!

  5. #5
    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: Problem with returning integer values

    not able to define it as an int
    What error message do you get from the compiler?
    Please copy the full text of the error message and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. HW Help: Returning Values with Methods
    By santafebound in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 25th, 2014, 02:33 AM
  2. Returning Boolean Values
    By triumvirate1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 7th, 2013, 02:40 AM
  3. returning an integer
    By Tubbly in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 8th, 2012, 04:42 PM
  4. Returning multiple values from a method.
    By atar in forum Java Theory & Questions
    Replies: 14
    Last Post: July 31st, 2012, 04:59 PM
  5. Program returning wrong values.
    By cam25 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 11th, 2012, 11:59 PM