java programing
Write a program that reads numbers fromthe keyboard into an array of type int[].
You may assume that there will be 50 or fewer entries in the array .Your program allows any numbers of numbers to be entered , up to 50 numbers. The output is to be a two-column list. The first column is a list of the distinct array elements ; the second column is the count of the number of occurrences of each element. The list should be sorted on entries int the first column, largest to smallest.
for the array:
-12 3 -12 4 1 1 -12 1 -1 1 2 3 4 2 3 -12
the output shoulb be:
N Count
4 2
3 3
2 2
1 4
-1 1
-12 4
>>
I need your help about this...
View Answers
February 16, 2010 at 9:02 AM
Hi Medinz,
Here is my version of code. Hope it helps you.
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Scanner;
public class IntArray {
/**
* @variable hash : holds the non duplicate numbers as KEY and its count of
* duplicates as VALUE.
*
* @variable final_array : holds the sorted nun duplicate numbers to be
* printed
**/
Hashtable hash = new Hashtable();
int[] final_array;
public static void main(String[] args) {
System.out.println("How many numbers you wanna enter? : ");
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int[] a = new int[n];
IntArray obj = new IntArray();
System.out.println("Enter " + n + " numbers: ");
Scanner scan = new Scanner(System.in);
for (int i = 0; i < a.length; i++) {
int num = scan.nextInt(); // Get each of the integer given as input and insert into an array
a[i] = num;
}
obj.countDuplicates(a);
obj.construtFinalArray();
obj.sort(obj.final_array);
obj.print(obj.final_array);
}
/** Prints the sorted array(given) and its duplicate count. **/
public void print(int[] arr) {
System.out.println("\nSorted numbers and its no.of occurances are: ");
System.out.println("\nNumber Count");
System.out.println("------ -----");
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i] + " " + hash.get(arr[i]));
}
}
/** Sorts the given array into Descending order. **/
public int[] sort(int[] a) {
int temp = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a;
}
/**
* Counts the duplicate numbers present in the given values and places in an
* HashTable where number would as the KEY and the count of its duplicates
* as the VALUE
**/
public void countDuplicates(int[] a) {
for (int i = 0; i < a.length; i++) {
int temp = 1;
if (!(hash.containsKey(a[i]))) // If hash doesn't contains the number insert it as the key.
hash.put(a[i], temp);
else { // If hash does has the number(as KEY),increment its count(VALUE of hash) to 1.
temp = Integer.parseInt(hash.get(a[i]).toString());
hash.put(a[i], temp + 1);
}
}
}
/**
* constructs 'final_array' i.e sorted array without duplicates used to
* print
**/
public int[] construtFinalArray() {
final_array = new int[hash.size()]; // Set the final_array size equal to HashTable hash as it has non duplicated numbers
Enumeration e = hash.keys();
int temp = 0;
int i = 0;
while (e.hasMoreElements()) {
String tempString = e.nextElement().toString(); // Take the key of hash because it contains the non duplicated numbers.
temp = Integer.parseInt(tempString); // As key is of type Object and the value to be inserted in final_array to be an int value,change key to string and then convert to int by using Integer.parseInt().
final_array[i++] = temp;
}
return final_array;
}
}
Regards,
javaquest2010
Related Tutorials/Questions & Answers:
Java programing Java programing need help wrting this
Write a program that has an array of 5 Strings, and determines which ones are
palindromes (letter-case does not matter).
thanks
Advertisements
java programingjava programing reverse a multidigit number using recursive definition
java programingjava programing write a program to input a number or find & print the highest digit
java programingjava programing write a program to input a number or find & print the highest digit. example:-2639 than print 9
java programingjava programing Integers from 0 to X stored in an array of size X+1.
Each integer appears once & only once, but in random order
e.g. Consider array of size 5 as below...
Arr[0]=3;
Arr[1]=0;
Arr[2]=1;
Arr[3]=4;
Arr[4]=2
Java ProgramingJava Programing I)WAP to print following series:
1)
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
2)
(5!/x)+(10!/xx)+(15!/xx*x)+.....n terms.
II)Write a menudriven program to perform the following as long as user
SOCKET PROGRAMING IN THE JAVA FOR NETWORKING CONCEPTSOCKET
PROGRAMING IN THE
JAVA FOR NETWORKING CONCEPT I WANT TO KNOW ABOUT THE
JAVA SOCKET
PROGRAMING FROM BASIC LEVEL AND HOW CAN I WORK ON IT I KNOW THE
JAVA PROGRAMING.SO PLEASE HELP ME HOW CAN I DO SOCKET
PROGRAMING Java programing helpJava programing help Can you help me create this program?
Write a console program that repeatedly prompts the user to enter data until they type done (any case, Upper, Lower, or Mixed). As they enter the data, assign
java programing problem - Java Beginnersjava programing problem Create a class named Movie that can be used with your video rental business. The Movie class should track the Motion picture Association of America rating , ID Number, and movie title with appropriate
java programing - Java Beginnersjava programing How to Design and create a
java class for address book object that contains a person's name, home address and phone number..., and pager. Write a
java GUI application to add, edit,delete and find the address
java programing - Java Beginnersjava programing hi!! I Wish i am welcome in this site. As You can see i am trying to teach my self how to program using
java language and manually is by not using the function in
java language. please help me
Java Programing - Java BeginnersJava Programing I used setMnemonic method to put shortcut key...);
}
}
---------------------------------------------------
Visit for more information.
http://www.roseindia.net/
java/example/
java/swing/
Thanks
java programing - Java Beginnersjava programing Write a program that reads numbers fromthe keyboard into an array of type int[].
You may assume that there will be 50 or fewer entries in the array .Your program allows any numbers of numbers to be entered , up
java IO programingjava IO programing how
Java source file is used as input the program will echo lines
Java programing - Development processJava programing 1.What is system.out and system.in? 2.write down the steps to run
Java application 3.what are the function of the following methods a.show input dialogue message. b.show output dialogue message
java programing - JDBCjava programing A retail company has got the records of its customers which contains name, home address and phone number, business address and phone number, and numbers for their fax machine, cellular phone, and pager, marital
beginning java programingbeginning
java programing Hi. I am trying to write a piece of code that not allow a duplicate item to be added to the list (and allow for either upper or lowercase names). But the same name keep adding onto the list. Can someone
programingprograming Using
Java, Design a simple interface that can capture information of your choice with the following controls; Labels, Text Fields, Checkbox, Radio buttons and command buttons. You may have to use the following files
guys,, need help,, in java programing,, arraysguys,, need help,, in
java programing,, arrays create a program where you will input 10 numbers and arrange it in ascending way using arrays
programing questionprograming question how do i use loops(for,while) to add components in
java
Hi Friend,
The below code might help you.ADS_TO_REPLACE_1
import java.awt.*;
import javax.swing.*;
class TextFieldArray extends JFrame
programing - IoC and Tutorials on
Java visit to :
http://www.roseindia.net/
java/
Thanks
programing - WebSevices);
}
}
For read more information,Examples and Tutorials on
Java visit to :
http://www.roseindia.net/
java/
Thanks
cnc programing coursecnc
programing course Hi,
I am beginner in Data Science and machine learning field. I am searching for
the tutorials to learn:
cnc
programing... the
topic "cnc
programing course". Also tell me which is the good
javascript programing error - JSP-Servletjavascript
programing error hi deepak
i m using explorer to run javascript program in this there is no option to check error will u plz tell me the way to check error in explorer as we can check with mozila
cnc programing classes near mecnc
programing classes near me Hi,
I am beginner in Data Science...
programing classes near me
Try to provide me good examples or tutorials links so that I can learn the
topic "cnc
programing classes near me". Also tell
help with substring in c programinghelp with substring in c programing Hi
I could not found programming in C in the "select category" so I am posting my question in
java section.
Please help with substring function,prompt the user for two strings and display
Threading in Java is very important
in
Java Programing language. A thread is a sequential path... Threading in
Java
...
Priorities and Scheduler
In
Java, thread scheduler can use the thread