Java help needed, brain ready to explode! | Newbie | | Join Date: Oct 2006
Posts: 6
| | |
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
| | | 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
| | | 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. - 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
| | Lives Here | | Join Date: Sep 2006
Posts: 12,070
| | | 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. - 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 - 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?
| | Newbie | | Join Date: Oct 2006
Posts: 6
| | | 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
| | | 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
|  | Moderator | | Join Date: Nov 2006 Location: USA
Posts: 3,929
| | | 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
| | | re: Java help needed, brain ready to explode!
Sorry for the break. Network problems. -
-
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.
|  | Moderator | | Join Date: Nov 2006 Location: USA
Posts: 3,929
| | | re: Java help needed, brain ready to explode! -
-
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.
| | Lives Here | | Join Date: Sep 2006
Posts: 12,070
| | | re: Java help needed, brain ready to explode! Quote:
Originally Posted by sicarie -
-
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 - if(names[i].equals(name[0])) {
| | Newbie | | Join Date: Feb 2008
Posts: 3
| | | 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
| | | re: Java help needed, brain ready to explode!
using java swing with dialog box.tnx!
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|