![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2006
Posts: 2
Rep Power: 0
![]() |
Java arrays - help!
Hi, I'm having some trouble with a java assignment. Can someone help me out plz: emergency!
here's the problem: [i]Write a program that reads numbers from the 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 number 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 eahe element. The list should be sorted on entries in the first column, largest to smallest. thanks! |
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
What have you got so far?
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#3 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4
![]() |
Please attempt to atleast produce a general idea of what to do (psuedo-code) to solve the problem.
People will not take their time to do your homework, despite what you may think. If you have a specific question about the assignment, then perhaps an individual will be more inclined to help you out.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Apr 2006
Posts: 2
Rep Power: 0
![]() |
Sorry for not being precise enough; this is what i got so far; i plan to do the main method last, I did the input method, but i'm stuck on the creation of the list.
import java.util.Scanner;
public class ArrayProg
{
public static final int Max_Entries = 50;
private static int[] entries = new int[Max_Entries];
public static void main (String[] args)
{
}
public static int fillArray()
{
System.out.print("Enter up to " + entries.length + " non decimal number: ");
Scanner keyboard = new Scanner(System.in);
int index = 0;
int next = keyboard.nextInt();
while (index < entries.length)
{
entries [index] = next;
index++;
next = keyboard.nextInt();
}
return index;
}
public static int createList()
{
}
}Last edited by Mjordan2nd; Apr 19th, 2006 at 12:36 AM. Reason: Code Tags |
|
|
|
|
|
#5 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Please use code-tags next time.
Also, what I would do is have a method like this:
public static void method()
{
for(int i = 0; i<entries.length; i++)
{
// if entries[i] = any of the elements before it then
//continue
int counter = 1;
for(int j = i; k<entries.length; j++)
{
// if entries[j] is the same as entries[i] add 1 to counter
}
// print entries[i] and then counter
}
}That would be the way I'd go.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower Last edited by Mjordan2nd; Apr 19th, 2006 at 7:55 AM. |
|
|
|
|
|
#6 |
|
Sexy Programmer
|
Good point MJ, I was going to suggest the same but then I read your post and it looks like you got it under control.
"for" loops are better for assigning arrays their values in a consecutive way.
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|