Results 1 to 8 of 8

Thread: Java Help

  1. #1
    Join Date
    Oct 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Java Help

    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.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    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.
    Last edited by Twey; 10-10-2006 at 07:32 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Oct 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  4. #4
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,626
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    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.

    Code:
    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.
    Last edited by codeexploiter; 10-11-2006 at 05:36 AM.

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Oct 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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 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.

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    This isn't homework, is it?
    Code:
    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;
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,626
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Twey i am sure that it is home work

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •