Java help needed, brain ready to explode!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • derbys
    New Member
    • Oct 2006
    • 6

    Java help needed, brain ready to explode!

    Im about ready to throw myself out of a window for this piece of work that i need to do. I need an array of English sentences. Which will list all those words from all the sentences that contain one or more character 'm'. Use an input dialog box to read the sentences into the array and an output dialog box to display the required words (7 words per line). The number of sentences will not exceed 5.

    Now i *think* ive figured out that i need to use some of the following :

    String [] array = new String[5];

    Then im guessing i need to store the string in an array

    array[0] = theInputSentenc e;

    Then i need to somehow create a loop that checks the sentence for thats that have the letter 'm' in them.


    But other then that my mind has come to an absolute standstill and its pretty damn annoying. Especially with the loop bit that will check for the 'm'.
    Really startign to get worked up about it too
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by derbys
    Im about ready to throw myself out of a window for this piece of work that i need to do. I need an array of English sentences. Which will list all those words from all the sentences that contain one or more character 'm'. Use an input dialog box to read the sentences into the array and an output dialog box to display the required words (7 words per line). The number of sentences will not exceed 5.

    Now i *think* ive figured out that i need to use some of the following :

    String [] array = new String[5];

    Then im guessing i need to store the string in an array

    array[0] = theInputSentenc e;

    Then i need to somehow create a loop that checks the sentence for thats that have the letter 'm' in them.


    But other then that my mind has come to an absolute standstill and its pretty damn annoying. Especially with the loop bit that will check for the 'm'.
    Really startign to get worked up about it too
    Write the shell for the program. The class, the main method and import javax.swing. Then try to get the sentences into the array. Your guesses are correct. After writing the shell, post and we can see where the loops go.

    Comment

    • derbys
      New Member
      • Oct 2006
      • 6

      #3
      Originally posted by r035198x
      Write the shell for the program. The class, the main method and import javax.swing. Then try to get the sentences into the array. Your guesses are correct. After writing the shell, post and we can see where the loops go.
      Hey, ive managed to complete this one, but thanks for your time anyway. I was wondering if you could help me with a piece of code that i wrote a while ago.

      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();
        }
      }
      What that does (you can probably tell) is that it allows a user to enter 10 names, which then get put in to alphabetical order and then displayed. What im wondering is it possible to only display names alphabetically after a certain name i.e "Jack". For example if i enter the names "Ben, Ted, James, Zack" it would only display the names "Ted, James and Zack" as there after Jack in the alphabet - if that makes any sense at all? :D

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by derbys
        Hey, ive managed to complete this one, but thanks for your time anyway. I was wondering if you could help me with a piece of code that i wrote a while ago.

        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();
        }
        }
        What that does (you can probably tell) is that it allows a user to enter 10 names, which then get put in to alphabetical order and then displayed. What im wondering is it possible to only display names alphabetically after a certain name i.e "Jack". For example if i enter the names "Ben, Ted, James, Zack" it would only display the names "Ted, James and Zack" as there after Jack in the alphabet - if that makes any sense at all? :D

        Code:
         import javax.swing.JOptionPane; 
        import java.util.Arrays;
         
        class NameHolder {
         String[] names = new String[10];
         public NameHolder() {
          String op = "";
          // Get names
          for(int i = 0; i < names.length; ++i) {
           names[i] = (String)JOptionPane.showInputDialog(null, "Please enter a name:");
          }
          // Sort names
          Arrays.sort(names);
          int pos = -1;
          String start = (String)JOptionPane.showInputDialog(null, "Please enter a name to start from:");
          for(int i = 0; i < names.length; ++i) {
           if(names[i].equals(start)) {
        	pos = i;
        	break;
           }
          }
          // Join names
          if(pos == -1) {
           JOptionPane.showMessageDialog(null, "Name not found");
          }
          else {
           for(int i = pos; i < names.length; ++i) {
        	op += names[i] + "\n";
           }
           JOptionPane.showMessageDialog(null, op);
          }
         }
         public static void main(String[] args) {
         // Instantiate.
          new NameHolder();
         }
        }
        Like this?

        Comment

        • derbys
          New Member
          • Oct 2006
          • 6

          #5
          Erm not really like that no. The names that are entered in to the input box would be then compared with the name "jack", then the names alphabetically after the name "jack" should be displayed.

          So for example if these Names were entered:

          Ben Toby Allen Zack Sally Richard

          The following would be displayed at the end:

          Richard Sally Toby Zack (in alphabetical order)

          Im probably not explaining it in the most simplest of ways here.

          Comment

          • derbys
            New Member
            • Oct 2006
            • 6

            #6
            Although what you did was quite similar to what i want now that ive looked at it properly. But is it possible that the name "jack" could be in the code instead of having to enter it in an input box after entering all of the other names? If so would that be using string.comparet o?

            Java beats me so much :D

            Comment

            • sicarie
              Recognized Expert Specialist
              • Nov 2006
              • 4677

              #7
              You could just set names[0] = "Jack"; and bump up the for loop initializer (starts at i = 0, start it at i = 1 so it doesn't overwrite names[0]).

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Sorry for the break. Network problems.
                Code:
                 
                int pos = -1;
                  //String start = (String)JOptionPane.showInputDialog(null, "Please enter a name to start from:");
                  for(int i = 0; i < names.length; ++i) {
                   if(names[i].equals("Jack")) {
                	pos = i;
                	break;
                   }
                  }
                The more times java beats you, the better your chances of beating it next time.

                Comment

                • sicarie
                  Recognized Expert Specialist
                  • Nov 2006
                  • 4677

                  #9
                  Code:
                   
                  int pos = -1;
                    //String start = (String)JOptionPane.showInputDialog(null, "Please enter a name to start from:");
                    for(int i = 0; i < names.length; ++i) {
                     if(names[i].equals("Jack")) {
                  	pos = i;
                  	break;
                     }
                    }
                  Ah r035198x is right, sorry, I didn't realize what you were asking - serves me right for only scanning it.

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by sicarie
                    Code:
                     
                    int pos = -1;
                    //String start = (String)JOptionPane.showInputDialog(null, "Please enter a name to start from:");
                    for(int i = 0; i < names.length; ++i) {
                    if(names[i].equals("Jack")) {
                    	pos = i;
                    	break;
                    }
                    }
                    Ah r035198x is right, sorry, I didn't realize what you were asking - serves me right for only scanning it.
                    In design of course your solution also works. Only that he/she would have to increase the size of the array by one and test with
                    Code:
                     if(names[i].equals(name[0])) {

                    Comment

                    • gabs
                      New Member
                      • Feb 2008
                      • 3

                      #11
                      ...could you please give me some of your javax swing knowledge that input words ranges 2 to 20 and arrange it into alphabetical order...if the words is less than 2 and greater than 20...it indicate out of range...


                      i hope you can help me...this is a very big help...



                      tnx!
                      Im a java explorer & beginner...

                      God Bless...

                      Comment

                      • gabs
                        New Member
                        • Feb 2008
                        • 3

                        #12
                        using java swing with dialog box.tnx!

                        Comment

                        Working...