Connecting Tech Pros Worldwide Forums | Help | Site Map

Java help needed, brain ready to explode!

Newbie
 
Join Date: Oct 2006
Posts: 6
#1: Nov 6 '06
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] = theInputSentence;

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

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Nov 7 '06

re: Java help needed, brain ready to explode!


Quote:

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] = theInputSentence;

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.
Newbie
 
Join Date: Oct 2006
Posts: 6
#3: Nov 7 '06

re: Java help needed, brain ready to explode!


Quote:

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.

Expand|Select|Wrap|Line Numbers
  1. import javax.swing.JOptionPane;
  2. import java.util.Arrays;
  3.  
  4. class NameHolder {
  5.   String[] names = new String[10];
  6.  
  7.   public NameHolder() {
  8.     int i;
  9.     String op = "";
  10.  
  11.     // Get names
  12.     for(i = 0; i < names.length; ++i)
  13.       names[i] = (String)JOptionPane.showInputDialog(null, "Please enter a name:");
  14.  
  15.     // Sort names
  16.     Arrays.sort(names);
  17.  
  18.     // Join names
  19.     for(i = 0; i < names.length; ++i)
  20.       op += names[i] + "\n";
  21.  
  22.     // Output names
  23.     JOptionPane.showMessageDialog(null, op);
  24.   }
  25.  
  26.   public static void main(String[] args) {
  27.     // Instantiate.
  28.     new NameHolder();
  29.   }
  30. }
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
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: Nov 7 '06

re: Java help needed, brain ready to explode!


Quote:

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.

Expand|Select|Wrap|Line Numbers
  1. import javax.swing.JOptionPane;
  2. import java.util.Arrays;
  3.  
  4. class NameHolder {
  5. String[] names = new String[10];
  6.  
  7. public NameHolder() {
  8. int i;
  9. String op = "";
  10.  
  11. // Get names
  12. for(i = 0; i < names.length; ++i)
  13. names[i] = (String)JOptionPane.showInputDialog(null, "Please enter a name:");
  14.  
  15. // Sort names
  16. Arrays.sort(names);
  17.  
  18. // Join names
  19. for(i = 0; i < names.length; ++i)
  20. op += names[i] + "\n";
  21.  
  22. // Output names
  23. JOptionPane.showMessageDialog(null, op);
  24. }
  25.  
  26. public static void main(String[] args) {
  27. // Instantiate.
  28. new NameHolder();
  29. }
  30. }
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


Expand|Select|Wrap|Line Numbers
  1.  import javax.swing.JOptionPane; 
  2. import java.util.Arrays;
  3.  
  4. class NameHolder {
  5.  String[] names = new String[10];
  6.  public NameHolder() {
  7.   String op = "";
  8.   // Get names
  9.   for(int i = 0; i < names.length; ++i) {
  10.    names[i] = (String)JOptionPane.showInputDialog(null, "Please enter a name:");
  11.   }
  12.   // Sort names
  13.   Arrays.sort(names);
  14.   int pos = -1;
  15.   String start = (String)JOptionPane.showInputDialog(null, "Please enter a name to start from:");
  16.   for(int i = 0; i < names.length; ++i) {
  17.    if(names[i].equals(start)) {
  18.     pos = i;
  19.     break;
  20.    }
  21.   }
  22.   // Join names
  23.   if(pos == -1) {
  24.    JOptionPane.showMessageDialog(null, "Name not found");
  25.   }
  26.   else {
  27.    for(int i = pos; i < names.length; ++i) {
  28.     op += names[i] + "\n";
  29.    }
  30.    JOptionPane.showMessageDialog(null, op);
  31.   }
  32.  }
  33.  public static void main(String[] args) {
  34.  // Instantiate.
  35.   new NameHolder();
  36.  }
  37. }
  38.  
  39.  
Like this?
Newbie
 
Join Date: Oct 2006
Posts: 6
#5: Nov 7 '06

re: Java help needed, brain ready to explode!


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.
Newbie
 
Join Date: Oct 2006
Posts: 6
#6: Nov 7 '06

re: Java help needed, brain ready to explode!


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

Java beats me so much :D
sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,929
#7: Nov 7 '06

re: Java help needed, brain ready to explode!


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]).
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#8: Nov 7 '06

re: Java help needed, brain ready to explode!


Sorry for the break. Network problems.
Expand|Select|Wrap|Line Numbers
  1.  
  2. int pos = -1;
  3.   //String start = (String)JOptionPane.showInputDialog(null, "Please enter a name to start from:");
  4.   for(int i = 0; i < names.length; ++i) {
  5.    if(names[i].equals("Jack")) {
  6.     pos = i;
  7.     break;
  8.    }
  9.   } 
  10.  
The more times java beats you, the better your chances of beating it next time.
sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,929
#9: Nov 7 '06

re: Java help needed, brain ready to explode!


Expand|Select|Wrap|Line Numbers
  1.  
  2. int pos = -1;
  3.   //String start = (String)JOptionPane.showInputDialog(null, "Please enter a name to start from:");
  4.   for(int i = 0; i < names.length; ++i) {
  5.    if(names[i].equals("Jack")) {
  6.     pos = i;
  7.     break;
  8.    }
  9.   } 
  10.  
Ah r035198x is right, sorry, I didn't realize what you were asking - serves me right for only scanning it.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#10: Nov 7 '06

re: Java help needed, brain ready to explode!


Quote:

Originally Posted by sicarie

Expand|Select|Wrap|Line Numbers
  1.  
  2. int pos = -1;
  3. //String start = (String)JOptionPane.showInputDialog(null, "Please enter a name to start from:");
  4. for(int i = 0; i < names.length; ++i) {
  5. if(names[i].equals("Jack")) {
  6.     pos = i;
  7.     break;
  8. }
  9.  
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
Expand|Select|Wrap|Line Numbers
  1.  if(names[i].equals(name[0])) {
Newbie
 
Join Date: Feb 2008
Posts: 3
#11: Feb 20 '08

re: Java help needed, brain ready to explode!


...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...
Newbie
 
Join Date: Feb 2008
Posts: 3
#12: Feb 20 '08

re: Java help needed, brain ready to explode!


using java swing with dialog box.tnx!
Reply