Home > Enterprise >  Separate one array with a hyphen
Separate one array with a hyphen

Time:03-30

I'm stuck on a school project. I'm supposed to print an array with [input] amount of randomized digits between 1-999 that have been sorted. First by odd and even but also ascending / descending. I'll give you guys a quick demonstration of how the program is supposed to work:

  • Enter the amount of digits you want to randomize: 10;
  • Your numbers are: 5, 818, 916, 935, 464, 89, 275, 470, 209, 388
  • Your numbers sorted: 388, 464, 470, 818, 916, 935, 275, 209, 89, 5
  • Out of the above 10 numbers, 5 were even and 5 were odd.

I got the sorting right, with the ascending even numbers to the left and the descending odd numbers to the right. But here's the problem I got. I want to separate these 2 categories with a hyphen (and I can't use arrayList). Like following:

  • Your numbers sorted: 388, 464, 470, 818, 916 - 935, 275, 209, 89, 5

I can print my full code here for anyone willing to help me think!

class Main {
  public static void main(String[] args) {
    System.out.println("How many random numbers in the range 0 - 999 are desired?");

    int randNum = 0;

    Scanner scan = new Scanner(System.in);
    randNum = scan.nextInt();

    int min = 1;
    int max = 1000;
    int evenAmount = 0;
    int oddAmount = 0;
    int[] arr = new int[randNum];
    
      for (int i = 0; i < arr.length; i  ) {
        arr[i] = ThreadLocalRandom.current().nextInt(min, max);
      }  

      for (int i = 0; i < arr.length; i  ) {
        if (arr[i] % 2 == 0)
        {
          evenAmount  ;
        }
        else 
        {
          oddAmount  ;
        }
      }
  
      Integer[] even = new Integer[evenAmount];
      Integer[] odd = new Integer[oddAmount];
      int a = 0;
      int b = 0;

      for (int i = 0; i < arr.length; i  ) {
        if (arr[i] % 2 == 0)
        {
          even[a  ] = arr[i];
        }
        else
        {
          odd[b  ] = arr[i];
        }
      }
    
    Arrays.sort(even);
    Arrays.sort(odd, Collections.reverseOrder());
    
    System.out.println("\n"   "Here are the random numbers: "   "\n");
    
      for (int i = 0; i < arr.length; i  ) {
        System.out.print(arr[i]   ", ");
        System.out.print("");
      }
    
    Integer[] result = new Integer[evenAmount   oddAmount];
    
    String[] strEven = new String[evenAmount];
    String[] strOdd = new String[oddAmount];
    String[] strSeparate = {"-"};

      for (int i = 0; i < evenAmount; i  ) 
      {
        strEven[i] = String.valueOf(even[i]);
      }

      for (int i = 0; i < oddAmount; i  )
      {
        strOdd[i] = String.valueOf(odd[i]);
      }
  
    
    System.arraycopy(even, 0, result, 0, evenAmount);  
    System.arraycopy(odd, 0, result, evenAmount, oddAmount);

    

    System.out.println("\n"   "\n"   "Here are the random numbers arranged: "   "\n");

      for (int i = 0; i < result.length; i  ) {
        System.out.print(result[i]   ", ");
      }
    
    System.out.println("\n"   "Out of the above "   arr.length   " numbers, "   evenAmount   " were even and "   oddAmount   " were odd.");  
    
    scan.close();
  }
}```

CodePudding user response:

But here's the problem I got. I want to separate these 2 categories with a hyphen (and I can't use arrayList). Like following:

Your numbers sorted: 388, 464, 470, 818, 916 - 935, 275, 209, 89, 5

You already know how many even numbers there are with your evenAmount variable. You can use that to determine when to print a "-" instead of a ",". You can also not print a "," after the last item with a little more checking:

System.out.print("Your numbers sorted: ");
for (int i = 0; i < result.length; i  ) {
    System.out.print(result[i]);

    if (i < (result.length - 1)) {
        System.out.print(((i   1) == evenAmount) ? " - " : ", ");
    }
}
System.out.println();

CodePudding user response:

I would use arrays.toString() to convert the array to a string so you can insert the hyphen.

CodePudding user response:

Here's an alternative solution in pseudocode. I will leave turning this into actual code as an exercise:

Use a for loop to print the even numbers from index 0 to index evenAmount
Print a hyphen
Use a for loop to print the odd numbers from index evenAmount to the end
  • Related