here is a sample of my code:
case 3:
System.out.print("Enter the word: ");
String wordEnter = bleedy.nextLine();
for(int i = 0; i < words.toArray().length; i++)
{
wurd = wordEnter.compareTo(words.get(i));
if (wurd == 0){
System.out.println("The definition of " + wordEnter +" is: " + definitions.get(i));
System.out.println("-------------------------------------------------\n");
}
else if(wurd != 0)
{
System.out.println("The word was not found!");
System.out.println("-------------------------------------------------\n");
}
}
break;
when I run the program I get this:
1. Add a word and definition
2. Display the word list
3. Search for a word and display its definition
4. Delete a Word
5. Quit
Choose a number: 1
Enter the Word: paper
Enter the word's definition: a substance made from wood pulp, rags, straw, or other fibrous material
Word and definition added!
-------------------------------------------------
1. Add a word and definition
2. Display the word list
3. Search for a word and display its definition
4. Delete a Word
5. Quit
Choose a number: 1
Enter the Word: dictionary
Enter the word's definition: a book containing a selection of the words of a language
Word and definition added!
-------------------------------------------------
1. Add a word and definition
2. Display the word list
3. Search for a word and display its definition
4. Delete a Word
5. Quit
Choose a number: 1
Enter the Word: pen
Enter the word's definition: any of various instruments for writing or drawing with ink
Word and definition added!
-------------------------------------------------
1. Add a word and definition
2. Display the word list
3. Search for a word and display its definition
4. Delete a Word
5. Quit
Choose a number: 3
Enter the word: dictionary
The word was not found!
-------------------------------------------------
The definition of dictionary is: a book containing a selection of the words of a language
-------------------------------------------------
The word was not found!
-------------------------------------------------
How do I get this program to only display the definition of dictionary, and not display "The word was not found!" ?
urgent java programming help!
Started by bleedy3, Jan 26 2010 03:16 PM
4 replies to this topic
#1
Posted 26 January 2010 - 03:16 PM
|
|
|
#2
Posted 26 January 2010 - 03:46 PM
Post your whole code please. Its hard to tell what the error is, since from what I see its probably in other cases.
#3
Posted 26 January 2010 - 04:25 PM
ok, hey it is:
import java.util.*;
public class WordDiction {
public static void main(String [] arguments){
int wurd = 0;
ArrayList<String> words = new ArrayList<String>();
ArrayList<String> definitions = new ArrayList<String>();
int choice = 0;
while (choice != 5)
{
System.out.println("1. Add a word and definition\n" +
"2. Display the word list\n3. Search for a word" +
" and display its definition\n4. Delete a Word\n5. Quit");
System.out.print("Choose a number: ");
Scanner bleedy = new Scanner(System.in);
choice = bleedy.nextInt();
System.out.println();
switch (choice)
{
case 1: System.out.print("Enter the Word: ");
bleedy.nextLine();
String word = bleedy.nextLine();
words.add(word);
System.out.print("Enter the word's definition: ");
String definition = bleedy.nextLine();
definitions.add(definition);
System.out.println("Word and definition added!");
System.out.println("-------------------------------------------------\n");
break;
case 2:
System.out.printf("%-10s"+"%s" ,"NUMBER", "WORD");
System.out.println();
bleedy.nextLine();
for(int i = 0; i < words.toArray().length; i++)
{
System.out.printf("%-10s" + "%s" , (i+1), words.get(i));
System.out.println();
}
System.out.println("-------------------------------------------------\n");
break;
case 3:
bleedy.nextLine();
System.out.print("Enter the word: ");
String wordEnter = bleedy.nextLine();
for(int i = 0; i < words.toArray().length; i++)
{
wurd = wordEnter.compareTo(words.get(i));
if (wurd == 0){
System.out.println("The definition of " + wordEnter +" is: " + definitions.get(i));
System.out.println("-------------------------------------------------\n");
}
else if(wurd != 0)
{
System.out.println("The word was not found!");
System.out.println("-------------------------------------------------\n");
}
}
break;
case 4:
System.out.print("Enter the word to delete: ");
String delWord = bleedy.nextLine();
System.out.println("-------------------------------------------------\n");
}
}
}
}
import java.util.*;
public class WordDiction {
public static void main(String [] arguments){
int wurd = 0;
ArrayList<String> words = new ArrayList<String>();
ArrayList<String> definitions = new ArrayList<String>();
int choice = 0;
while (choice != 5)
{
System.out.println("1. Add a word and definition\n" +
"2. Display the word list\n3. Search for a word" +
" and display its definition\n4. Delete a Word\n5. Quit");
System.out.print("Choose a number: ");
Scanner bleedy = new Scanner(System.in);
choice = bleedy.nextInt();
System.out.println();
switch (choice)
{
case 1: System.out.print("Enter the Word: ");
bleedy.nextLine();
String word = bleedy.nextLine();
words.add(word);
System.out.print("Enter the word's definition: ");
String definition = bleedy.nextLine();
definitions.add(definition);
System.out.println("Word and definition added!");
System.out.println("-------------------------------------------------\n");
break;
case 2:
System.out.printf("%-10s"+"%s" ,"NUMBER", "WORD");
System.out.println();
bleedy.nextLine();
for(int i = 0; i < words.toArray().length; i++)
{
System.out.printf("%-10s" + "%s" , (i+1), words.get(i));
System.out.println();
}
System.out.println("-------------------------------------------------\n");
break;
case 3:
bleedy.nextLine();
System.out.print("Enter the word: ");
String wordEnter = bleedy.nextLine();
for(int i = 0; i < words.toArray().length; i++)
{
wurd = wordEnter.compareTo(words.get(i));
if (wurd == 0){
System.out.println("The definition of " + wordEnter +" is: " + definitions.get(i));
System.out.println("-------------------------------------------------\n");
}
else if(wurd != 0)
{
System.out.println("The word was not found!");
System.out.println("-------------------------------------------------\n");
}
}
break;
case 4:
System.out.print("Enter the word to delete: ");
String delWord = bleedy.nextLine();
System.out.println("-------------------------------------------------\n");
}
}
}
}
#4
Posted 26 January 2010 - 05:20 PM
You didn't explain your problem properly. I thought it gets you only one line of definition not found, but never mind, the error is in your for loop in case 3: you compare the query 'word' to every member of the list and do the printings for each member of the list, while you should first determine after iterating over all the members whether you found the word or not, and only then do the printing.
Here's the fixed code:
I also rearranged it a bit so it looks a bit nicer but it still needs rewriting to follow code conventions.
Here's the fixed code:
I also rearranged it a bit so it looks a bit nicer but it still needs rewriting to follow code conventions.
package help;
import java.util.*;
public class WordDiction {
public static void main(String [] arguments){
int wurd = 0;
ArrayList<String> words = new ArrayList<String>();
ArrayList<String> definitions = new ArrayList<String>();
int choice = 0;
while (choice != 5){
System.out.println(
"1. Add a word and definition\n" +
"2. Display the word list\n"+
"3. Search for a word and display its definition\n"+
"4. Delete a Word\n"+
"5. Quit");
System.out.print("Choose a number: ");
Scanner bleedy = new Scanner(System.in);
choice = bleedy.nextInt();
System.out.println();
switch (choice){
case 1: System.out.print("Enter the Word: ");
bleedy.nextLine();
String word = bleedy.nextLine();
words.add(word);
System.out.println("\nadded: "+ word);
System.out.print("Enter the word's definition: ");
String definition = bleedy.nextLine();
definitions.add(definition);
System.out.println("Word and definition added!");
System.out.println("-------------------------------------------------\n");
break;
case 2:
System.out.printf("%-10s"+"%s" ,"NUMBER", "WORD");
System.out.println();
bleedy.nextLine();
for(int i = 0; i < words.toArray().length; i++)
{
System.out.printf("%-10s" + "%s" , (i+1), words.get(i));
System.out.println();
}
System.out.println("-------------------------------------------------\n");
break;
case 3:/*my addition*/
boolean wasDefinitionFound = false;
String tempDefinition = null;
bleedy.nextLine();
/*my addition end*/
System.out.print("Enter the word: ");
String wordEnter = bleedy.nextLine();
for(int i = 0; i < words.toArray().length; i++){
wurd = wordEnter.compareTo(words.get(i));
if (wurd == 0){
/*System.out.println("The definition of " + wordEnter +" is: " + definitions.get(i));
System.out.println("-------------------------------------------------\n");
*/
wasDefinitionFound = true;
tempDefinition = definitions.get(i);
break;
}
/*my addition*/
/*else if(wurd != 0){
System.out.println("The word was not found!");
System.out.println("-------------------------------------------------\n");
}*/
/*my addition end*/
}
/*my addition*/
if (wasDefinitionFound){
System.out.println("The definition of " + wordEnter +" is: " + tempDefinition);
System.out.println("-------------------------------------------------\n");
}
else{
System.out.println("The word was not found!");
System.out.println("-------------------------------------------------\n");
}
/*my addition end*/
break;
case 4:
System.out.print("Enter the word to delete: ");
String delWord = bleedy.nextLine();
System.out.println("-------------------------------------------------\n");
}
}
}
}
#5
Posted 26 January 2010 - 07:13 PM
thanks


Sign In
Create Account


Back to top









