Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0

    Listing Numbers Largest to Smallest


    Hey ya'll. I am suppose to create a program that list the user's entered numbers from largest to smallest. If they want to stop inputing numbers they would have to enter -99. I'm not sure how to make it loop and ask for more numbers, and stop when I type -99. I'm also unsure on how to list numbers from largest to smallest.
    Here's my code:

    import java.util.Scanner;

    public class homework5
    {
    public static void main(String[] args)
    {
    Scanner keyboard = new Scanner(System.in);

    // Variables
    int num = 0; // user's inputed integer
    int min = num;

    // Asks for integer
    System.out.println("Enter an integer, you many keep entering untill you're done. (Enter -99 to quit)");
    num = keyboard.nextInt();

    while(num != -99)
    {
    for(int max = num; max <= max; max++)
    {
    max = max;
    if(min < max)
    System.out.println(max);
    }

    }
    }
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    The statement that changes the value of the num variable needs to be inside the while loop where its value can be tested and used to end the looping when it is -99.
    If the code is supposed to order/sort the numbers and show them, it needs to save all the numbers the user entered in an array or arraylist so that they can be sorted after the user enters the -99.


    Please edit the post and wrap the code in code tags.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0
    I'm not sure how to store the numbers. Can I store them in a txt file and read from there?
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    how to store the numbers
    Use an array (make it really big) or use an arraylist which will automatically resize as needed.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0
    Originally Posted by NormR
    Use an array (make it really big) or use an arraylist which will automatically resize as needed.
    I have no idea how to use an array. I'm still learning this in class.
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    That's an unusual assignment for someone that does not know how to use an array?
    Are you sure its stated correctly?
    list the user's entered numbers from largest to smallest
    That requires that the user's input be some place it can be sorted and printed.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0
    What? How do I use an array to store it?
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Assign the value read in from the user to the next slot in the array.

    See the tutorial about arrays:http://docs.oracle.com/javase/tutori...ts/arrays.html
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0
    How will I be able to set the number of array placements to the number of times the user entered a number, and how do I store the inputed numbers in a separate array?
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Either make an array much larger than the number of numbers the user will enter
    or use a class like ArrayList that doesn't have a size limit.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0
    Yea I used 100 as the max, but go do I store each inputed number in the list? That's the only part I'm confused on.
  22. #12
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Store each number read in from the user into the next slot in the array:
    Code:
    theArray[theIndex] = theValue;  //  save value in array
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0
    Here's my current code. I set up the array so the max is 100.

    // Variables
    int num = 0; // user's inputed integer
    int[] array = new int [100];
    int counter = 0;

    // Asks for integer(s)
    System.out.println("Enter an integer, you many keep entering untill you're done. (Enter -99 to quit)");
    num = keyboard.nextInt();

    while(num == -99) // goes through the loop and stores integers
    break; // stops loop if '-99' is entered
    {
    while(counter < 100)
    {
    array[counter++] = input.nextInt();

    }
    for(int i:array)
    System.out.println(i);
    }
  26. #14
  27. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Does that work ok?
    For debugging print out the array's contents with:
    Code:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0
    I got one error:

    homework5.java:29: error: cannot find symbol
    array[counter++] = input.nextInt();
    ^
    symbol: variable input
    location: class homework5
    1 error
    Process javac exited with code 1

    its pointing at the "input.nextInt();"

    I tried doing num.nextIn();

    But it doesn't work.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo