0

So I currently am having, this issue when I run my program it repeats the second display line. ("unsorted line")

I also am having trouble with the alignment when I run the program

What I need this program to do:

  1. Generate 10 random integer numbers between 1 and 100, and place each random number in a different element of a single-dimension array starting with the first number generated.

  2. Locate the largest of the 10 numbers and display its value.

  3. Display the array's contents in the order the numbers are initially inserted. This is called an unsorted list.

  4. Using the bubble sort, now sort the array from smallest integer to the largest. The bubble sort must be in its own method; it cannot be in the main method.

This is what I currently have programmed.. I just need help on adjustments when its ran.

import java.util.Arrays;

public class Chpt7_Project2 {
    //Ashley Snyder
    public static void main(String[] args) {
        //create an array of 10 integers
        int[] list = new int[10];

        //initialize array of 10 random integers between 0 and 100
        for (int i = 0; i < list.length; i++) {
            list[i] = (int) (Math.random() * 100 + 1);
        }

        //Find the maximum of the list of random numbers generated
        int maximum = -1;
        int minimum = 999;

        for (int i = 0; i < list.length; i++) {
            if (maximum < list[i])
                maximum = list[i];
            if (minimum > list[i])
                minimum = list[i];
        }

        //Display the maximum from the randTen array
        System.out.println("The largest value is: " + maximum);

        //Display the unsorted list of numbers from the randTen array
        for (int i = 0; i < list.length; i++) {
            System.out.print(list[i] + "The unsorted list is: ");
        }

        //Display the sorted array numbers
        bubbleSort(list);
        System.out.println("The sorted list is: " + Arrays.toString(list) + " ");
    }

    public static void bubbleSort(int[] list) {
        //Sort randomly generated integers in randArray from lowest to highest
        int temp;
        for (int i = list.length - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                if (list[j] > list[j + 1]) {
                    temp = list[j];
                    list[j] = list[j + 1];
                    list[j + 1] = temp;
                }
            }
Community
  • 1
  • 1
  • 4
    I would consider changing your tag from javascript to java. – FujiRoyale Apr 08 '21 at 02:32
  • The program is producing output as expected. Are you looking for ways to correct the alignment of the output, like printing on separate lines? – Justin Mathew Apr 08 '21 at 03:55
  • If I can note: "find the 10 highest numbers" is already same as "sort your numbers, and print the last 10" and is something that Collections.sort will happily do for you. Also, while unlikely to be useful for homework, be aware that [copyOfRange](https://stackoverflow.com/questions/11001720/get-only-part-of-an-array-in-java) exists, and is rather useful. And on a final note: remember to look at your post, and if you see any bad indentation, _edit your post to clean that up_. [Good posts](/help/how-to-ask) get good answers – Mike 'Pomax' Kamermans Apr 08 '21 at 04:11
  • I am having trouble with the second line repeating itself and the alignment – FallenAngelAsh Apr 08 '21 at 05:14

1 Answers1

0

You're really close to making this work. Please notice that System.out.print("The unsorted list is: " + list[i]); is inside a loop. That means that "The unsorted list is" will print 10 times. You probably want to move the label part of the output before the loop while keeping the list[i] inside the loop. Then don't forget to put a System.out.println after the loop finishes. You will then see that each item is pushed together. To fix that just put list[i] + " " as part of the print.

It appears that your are a bit confused about two separate items. First is writing (printing) to the console. System.out.print("whatever"); and System.out.println("something");. The first one will write a line of text to the console but will not go to the next line. The second one writes and then goes (advances) to the next line.

Secondly, and really importantly is for. Whenever you see for (...) { whatever }; think that something (the whatever) is going to be repeated. The number of times that it will repeat is mostly / usually determined by the part in parenthesis. The thing that is repeated is between the braces {}.

I think this is what you want:

//Display the unsorted list of numbers from the randTen array
System.out.print("The unsorted list is: ");
for (int i = 0; i < list.length; i++) {
    System.out.print(list[i] + " ");
}
System.out.println();

Please notice that the first System.out.print does not output to a new line, nor does the second one. The second System.output.print is repeated 10 times. Finally, System.out.println() starts a new line of output.

Community
  • 1
  • 1
Bill
  • 1,588
  • 12
  • 21
  • I switched up the placement of the loop and statement so I reads as I have it now in the post.. but when I ran the program it was still repeating itself.. – FallenAngelAsh Apr 08 '21 at 05:12
  • Change `System.out.print(list[i] + "The unsorted list is: ");` to `System.out.print(list[i] + " ");` – Bill Apr 08 '21 at 05:26
  • Thank you, that stopped the repeat but how do i get the numbers to show up next to a statement with "The unsorted list is: " ? also the sorted list display statement is on the same line as the second one ... – FallenAngelAsh Apr 08 '21 at 06:17