Question

Please help me with this Java project. I'm trying to create a loop so if user...

Please help me with this Java project. I'm trying to create a loop so if user enter value > 12, will prompt them a message and take them back to "How many elements do you wan to enter?". Thanks

public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
      
   int i, j;
  
   System.out.print("\n How meny elements do you want to enter? (N should be no more than 12) ");
   int num = sc.nextInt();
     
   if (num > 12) {
       System.out.print("N should be no more than 12");
       return ;
   }
   else {

   int[] arrayElements = new int[num];
  
   System.out.println("\n Enter array elements: ");
  
   for (i = 0; i < num; i++)
   {
   do
   {
   arrayElements[i] = sc.nextInt();
   }while(arrayElements[i] < 1 || arrayElements[i] > num);
   }
  
   System.out.println("\n\n Array Elements: \n");
   for(i=0; i<num; i++)
   {
   System.out.print(" \t " + arrayElements[i]);
   }
  
   int temp;
  
   for(i=0; i<num; i++)
   {
   for(j=i+1; j<num; j++)
   {
   if(arrayElements[i] > arrayElements[j])
   {
   temp = arrayElements[i];
   arrayElements[i] = arrayElements[j];
   arrayElements[j] = temp;
   }
   }
   }
  
   boolean duplicate;
   int count;
  
   System.out.println("\n\n");
  
   for (i = 0; i < num;)
   {
   duplicate = false;
   count = 1;
  
   for (j = i+1; j < num; j++)
   {
   if(arrayElements[j] != arrayElements[i])
   break;
  
   if (arrayElements[i] == arrayElements[j] && j!=i)
   {
   count ++;
   duplicate = true;
   }
   }
  
   if(duplicate)
   {
   i = i+count-1;
   System.out.println("\n Element: " + arrayElements[i] + " \t " +"Count: "+ count + " times. \n");
   }
   else
   i = i+1;
   }
   }
   }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;
public class Main{
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    int i, j;

    System.out.print("\n How many elements do you want to enter? (N should be no more than 12) ");
    int num = sc.nextInt();

    while (num > 12) {
      System.out.print("N should be no more than 12");
      System.out.print("\n How many elements do you want to enter? (N should be no more than 12) ");
      num = sc.nextInt();
    }

    int[] arrayElements = new int[num];

    System.out.println("\n Enter array elements: ");

    for (i = 0; i < num; i++)
    {
      do
      {
        arrayElements[i] = sc.nextInt();
      }while(arrayElements[i] < 1 || arrayElements[i] > num);
    }

    System.out.println("\n\n Array Elements: \n");
    for(i=0; i<num; i++)
    {
      System.out.print(" \t " + arrayElements[i]);
    }

    int temp;

    for(i=0; i<num; i++)
    {
      for(j=i+1; j<num; j++)
      {
        if(arrayElements[i] > arrayElements[j])
        {
          temp = arrayElements[i];
          arrayElements[i] = arrayElements[j];
          arrayElements[j] = temp;
        }
      }
    }

    boolean duplicate;
    int count;

    System.out.println("\n\n");

    for (i = 0; i < num;)
    {
      duplicate = false;
      count = 1;

      for (j = i+1; j < num; j++)
      {
        if(arrayElements[j] != arrayElements[i])
          break;

        if (arrayElements[i] == arrayElements[j] && j!=i)
        {
          count ++;
          duplicate = true;
        }
      }

      if(duplicate)
      {
        i = i+count-1;
        System.out.println("\n Element: " + arrayElements[i] + " \t " +"Count: "+ count + " times. \n");
      }
      else
        i = i+1;
    }

  }

}


Add a comment
Know the answer?
Add Answer to:
Please help me with this Java project. I'm trying to create a loop so if user...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a java class that user put two inputs and first input generate n length array...

    Create a java class that user put two inputs and first input generate n length array of randomly generated numbers. and the second input changes that number of multiples in the array into zero. for example, if the user puts 3 and 5 then it generates 34 60 10 and since the second input is 5 then the multiple of 5 eliminates so it generates 34 0 0 here is the main method that I'm going to use class Main...

  • PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java,...

    PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java, Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 arrays of this size timing the run to get an average time to sort an array of this size. Then do the following: Initiate a variable running_time to 0 Create a for loop that iterates 1000 times....

  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • In the code shown above are two parts of two different exercises. HOW WE CAN HAVE...

    In the code shown above are two parts of two different exercises. HOW WE CAN HAVE BOTH OF THEM ON THE SAME CLASS BUT SO WE CAN RECALL them threw different methods. Thank you. 1-First exercise import java.util.Scanner; public class first { public static int average(int[] array){    int total = 0;    for(int x: array)        total += x;    return total / array.length;    } public static double average(double[] array){    double total = 0;    for(double x: array)        total += x;    return total /...

  • use the same code. but the code needs some modifications. so use this same code and...

    use the same code. but the code needs some modifications. so use this same code and modify it and provide a output Java Program to Implement Merge Sort import java.util.Scanner Class MergeSort public class MergeSort Merge Sort function / public static yoid sortfintfl a, int low, int high) int N-high-low; if (N1) return; int mid- low +N/2; Il recursively sort sort(a, low, mid); sort(a, mid, high); I/ merge two sorted subarrays int] temp new int[N]; int i- low, j-mid; for...

  • Hello this is my java sorting algorithm program i need all of my errors corrected so...

    Hello this is my java sorting algorithm program i need all of my errors corrected so I can run it. thank you!! import java.util.Scanner;    public class SortingAlogs { public static void main(String[]args){ int array [] = {9,11,15,34,1}; Scanner KB = new Scanner(System.in); int ch; while (true) { System.out.println("1 Bubble sort\n2 Insertion sort\n3 Selection sort\n"); ch = KB.nextInt(); if (ch==1)    bubbleSort(array); if (ch==2)    insertion(array); if (ch==3)    Selection(array); if (ch==4) break;    print(array);    System.out.println(); }    }...

  • Provide comments for this code explaining what each line of code does import java.util.*; public class...

    Provide comments for this code explaining what each line of code does import java.util.*; public class Chpt8_Project {    public static void main(String[] args)       {        Scanner sc=new Scanner(System.in);        int [][]courses=new int [10][2];        for(int i=0;i<10;i++)        {            while(true)            {                System.out.print("Enter classes and graduation year for student "+(i+1)+":");                courses[i][0]=sc.nextInt();                courses[i][1]=sc.nextInt();                if(courses[i][0]>=1...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT