PDA

View Full Version : Java Help



derbys
10-10-2006, 06:36 PM
Yeh, so im totally and utterly useless at java and could do with some help with the following, maybe with someone giving a walk through of what i have to do (every step would be good ;) )

Write a Java program which:
Creates a one dimensional array of String which has 10 elements.
Uses the input dialogue box to read names of 10 people and store them into the array.
Sorts these names in alphabetical order and display them in an output dialogue box.

So yeh i have no idea of how to start it and such so if anyone could be of help i'd appreciate it.

Twey
10-10-2006, 07:17 PM
import javax.swing.JOptionPane;
import java.util.Arrays;

class NameHolder {
String[] names = new String[10];

public NameHolder() {
int i;
String op = "";

// Get names
for(i = 0; i < names.length; ++i)
names[i] = (String)JOptionPane.showInputDialog(null, "Please enter a name:");

// Sort names
Arrays.sort(names);

// Join names
for(i = 0; i < names.length; ++i)
op += names[i] + "\n";

// Output names
JOptionPane.showMessageDialog(null, op);
}

public static void main(String[] args) {
// Instantiate.
new NameHolder();
}
}There are a few other things you might want to do (such as check if the entered value is empty or null [the user pressed cancel]), but that's the core of it.

derbys
10-10-2006, 08:29 PM
Do you by any chance have the option pane and the arrays files? Im using a program called jEdit, and that doesn't have all of the imported files. So it's not allowing the program to run.

But cheers, i actually make sense of some of that.

codeexploiter
10-11-2006, 05:29 AM
Do you have JDK installed in your machine?

If yes then make sure that your classpath points to the correct location.

Instead of trying to execute the program from an IDE try to execute it from Command prompt.

The IDE can trigger some error due to the erroneous settings it has.

Twey's program will definitely work.



import javax.swing.JOptionPane;
import java.util.Arrays;


These are standard Java packages once you install JDK properly then they'll be there. If you search you JDK folder there will be a src.zip file inside that you can find these files. I am working with Java SDK 1.4.2_04.

Twey
10-11-2006, 01:11 PM
The class files are stored in a JAR called rt.jar, and as codeexploiter says, should be part of the default installation of any Java runtime.

derbys
10-11-2006, 01:37 PM
Ok i have got that to work, and it's looking good and i've managed to get it to check whether or not a value has been entered cheers. One more question relating to another thing in java i need to do, (ive managed to do two others which im quite pleased with :cool: lol)

Declare a two dimensional array with 10 rows and 10 columns and initialise:
a) All the elements of its third row to 10
b) All the elements of its 6th column to 20
c) And all the other elements to 5
in that order.

And i've grinded to a hault with that one, any help again would be appreciated.

Twey
10-11-2006, 01:44 PM
This isn't homework, is it?
int[][] nums;

for(int y = 0; y < nums.length; ++y)
for(int x = 0; x < nums[i].length; ++x)
if(y == 2)
nums[y][x] = 10;
else if(x == 19)
nums[y][x] = 20;
else
nums[y][x] = 5;

codeexploiter
10-12-2006, 05:00 AM
Twey i am sure that it is home work :D