简体   繁体   中英

Printing methods from main method not working - Java

So Ive been instructed to write a class that generates an array (with return), sorts the generated array, reverses the sorted array (with return), and then check the sorted to see if any 2 adjacent #'s are the same (return True if there are, or False if there aren't).

I've managed to write everything out so far from what I can tell however my last set of instructions are to fill main with print statements that call on the lower methods I wrote to be displayed in the order written.

I managed to get the original randomly generated array to be called and printed, however I've had no luck at all on calling any of the other methods like I did the first and have tried everything I can think of to get them to just print their results with the provided "This is the blah blah blah:"

If anyone could point me in the right direction as far as being able to call the other methods to print their results, along with each print out to include a basic statement like "The Random array is: ", "The Sorted array is: ", "The Reversed array is: ", "It is (TRUE/FALSE) this array has adjacent duplicates." I would be very much appreciative. Nothing I've tried at this point has worked and im at a complete standstill. I've been trying to work friends who are familiar with java as well but we seem to be stuck too. This is what I've written so far...

import java.util.*;

public class SortedSequence 
{
   public static void main(String[] args) 
{
  int[] randomNumbers = new int[20];
  randomNumbers = generateRandom(20);
  printArray(randomNumbers);

 // This is where Im getting stuck at. Everything I've tried makes a compile error

}

  public static int[] generateRandom(int n) 
  {
     int[] genNumbers = new int[n];

     Random rand = new Random();
        for (int i = 0; i < genNumbers.length; i++) 
     {
        int bubble = rand.nextInt(100);
        genNumbers[i] = bubble; 
     }
        return genNumbers;
     }

  public static void sortArray(int[] genNumbers) 
     {
        Arrays.sort(genNumbers); 
     }

  public static int[] reverse(int[] x) 
      { 
        int[] sortArray = new int[x.length]; 
        for (int i = 0; i < x.length; i++) { 
        sortArray[i] = x[x.length - 1 -i];
      }
        return sortArray; 
      }


  public static boolean adjacentDuplicates(int[] boo)
     {
        boolean duplicates = false;
        for (int i = 0; !duplicates && i < boo.length-1; i++)
        if (boo[i] == boo[i+1]);
     {
        duplicates = true;
     }
        return duplicates;
     }

  public static void printArray(int[] print)
     {
        for (int i = 0; i < print.length; i++) {
        System.out.print(print[i] + " "); }
        System.out.println();
     }
}

I compiled the code, and I'm not getting any errors whatsoever.

I added a printArray(reverse(randomNumbers)); to the main class, like so:

   public static void main(String[] args) 
{
  int[] randomNumbers = new int[20];
  randomNumbers = generateRandom(20);
  printArray(randomNumbers);
  printArray(reverse(randomNumbers));

 // This is where Im getting stuck at. Everything I've tried makes a compile error

}

and upon running the program, it returned:

28 87 13 22 85 0 60 59 90 30 52 15 32 72 9 76 83 89 36 39 
39 36 89 83 76 9 72 32 15 52 30 90 59 60 0 85 22 13 87 28 

It looks like it's an issue with your compiler, or how you were calling that class.


Answers to subsequent questions.

Here's what you needed for the sorted class. The Arrays.sort() method takes the array and manually moves them around inside it. :

  public static int[] sortArray(int[] genNumbers) 
     {
        Arrays.sort(genNumbers); 
        return genNumbers;
     }

Also, you had the return for it set as void, not int[].


Adjacent Duplicates

I honestly have no idea what the hell was going on in the adjacentDuplicates. Looks like the if and for methods did nothing, since you put a ; directly after the ) .

Your code needs to come between the end of the ) and the ; , or you need to use {} after the for loop declaration (shown below).

Also, Using those nextline brackets is terrible for readability, and I would advise against it. Here's what the adjacentDuplicates method should look like.

  public static boolean adjacentDuplicates(int[] boo) {
        boolean duplicates = false;
        for (int i = 0; !duplicates && i < boo.length-1; i++) {
            if (boo[i] == boo[i+1]) duplicates = true;
        }
        return duplicates;
     }

Running that

Now, here's how we can print all that out and get readable info:

   public static void main(String[] args) 
{
  int[] randomNumbers = new int[20];
  randomNumbers = generateRandom(20);


  System.out.print("Array: ");
  printArray(randomNumbers);
  System.out.print("Reversed: ");
  printArray(reverse(randomNumbers));
  System.out.print("Sorted: ");
  printArray(sortArray(randomNumbers));
  System.out.print("Adjacent Duplicates?: ");
  System.out.println(adjacentDuplicates(randomNumbers));
  // If we sort out the numbers, then any duplicates will become adjacent to each other
  System.out.print("Duplicates at all?: ");
  System.out.println(adjacentDuplicates(sortArray(randomNumbers)));

 // This is where Im getting stuck at. Everything I've tried makes a compile error

}

Test run:

Array: 92 18 5 16 68 10 85 58 50 56 91 48 45 28 63 98 94 15 93 64 
Reversed: 64 93 15 94 98 63 28 45 48 91 56 50 58 85 10 68 16 5 18 92 
Sorted: 5 10 15 16 18 28 45 48 50 56 58 63 64 68 85 91 92 93 94 98 
Adjacent Duplicates?: false
Duplicates at all?: false

Here's the full class. Listed again at bottom.


Newline Stuff

It appears that you want it formatted differently. I'll let you have the enjoyment of getting to do that, but do note:

System.out.print("bla")

Will print this and not add a newline at the end

System.out.println("blap");

Will print the string and add a newline.

So

System.out.print("bla");
System.out.println("blap");
System.out.print("blo");

Is exactly like running

System.out.print("bla blap\n blo")

Both of which would print:

bla blap
blo

Here's all of that code

So I got the 3 int arrays to print.

Not sure how to get the boolean T or F to print from main...

Also not sure how I manged to get the initial random array to print on the same line as the the txt i needed to present prior to the array but it did, especially with all the other code being identical and not doing the same thing...

Recommendations on getting the arrays to print on same line as the the descriptive text? What about my boolean situation?

 /**
 * This class generates a random array of 20 int long, sorts
 * the array, reverses the array, check to see if there are
 * any duplicate adjacent numbers, prints true if there are or
 * false if there aren't, and then prints the results.
 *
 *@author Matthew Jackson
 *@version 9/5/2017
 *
 */
 import java.util.*;

 public class SortedSequence 
 {
  public static void main(String[] args) 
  {
  System.out.print("The Random array is: ");
  int[] randomNumbers = new int[20];
  randomNumbers = generateRandom(20);
  printArray(randomNumbers);
  System.out.println();

  System.out.println("The Sorted array is: ");
  sortArray(randomNumbers);
  printArray(randomNumbers);
  System.out.println();

  System.out.println("The Reverse array is: ");
  printArray(reverse(randomNumbers));
  System.out.println();

  System.out.println("It is" + "this array has adjacent duplicates");
  if(adjacentDuplicates(randomNumbers))
  {
     // Put something here to print the boolean T/F?
  }

}

 /**
  * Generate an array of type int and size n that returns
  * this array from the method
  *
  *@param n Generate random number array
  *@return Return array list that was generated
  */

  public static int[] generateRandom(int n) 
  {
     int[] genNumbers = new int[n];

     Random rand = new Random();
        for (int i = 0; i < genNumbers.length; i++) {
        int bubble = rand.nextInt(100);
        genNumbers[i] = bubble; }
        return genNumbers;
   }

 /**
  *Sort array of randomly generated numbers
  *
  *@param genNumbers Calls method of randomly generated number array
  */

  public static void sortArray(int[] genNumbers) 
     {
        Arrays.sort(genNumbers); 
     }

  /**
   *Reverse array of sorted array numbers
   *
   *@param Reverses list of sorted array numbers.
   *@return Return array in reverse original order
   */

  public static int[] reverse(int[] x) 
      { 
        int[] sortArray = new int[x.length]; 
        for (int i = 0; i < x.length; i++) { 
        sortArray[i] = x[x.length - 1 -i];
      }
        return sortArray; 
      }

   /**
    *Check array list to see if there are any duplicates
    *adjacent to each other
    *
    *@param duplicates True if adjacent numbers are same,
    *                  false if not.
    *@return Returns True/False if there are adjacent duplicates or not
    */

  public static boolean adjacentDuplicates(int[] boo)
     {
        boolean duplicates = false;
        for (int i = 0; !duplicates && i < boo.length-1; i++)
        if (boo[i] == boo[i+1]);
        { //else infront of the {?
        duplicates = true;
     }
        return duplicates;
     }

   /**
     *Prints given array
     *
     *@param print Prints any method called to it
     */ 

  public static void printArray(int[] print)
     {
        for (int i = 0; i < print.length; i++) {
        System.out.print(print[i] + " "); }
        System.out.println();
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM