The program works just fine, the thing is when trying to print the dictionary tree out, it doesn't print the word itself:
+++++Spanisch Woerterbuch+++++++
Bitte waehlen Sie eine Aktion aus!
public class Node{
private String word, definition;
private Node left, right;
public Node (String w, String def){
this.word = w;
this.definition = def;
}
public String getW(){ return this.word; }
public String getD(){ return this.definition; }
public Node getL(){ return this.left; }
public Node getR(){ return this.right; }
public void setL(Node l){ this.left = l; }
public void setR(Node r){ this.right = r; }
}
public class Tree{
Node root;
// sorting insert
public void insert(Node newWord){
if ( root == null) root = newWord;
else insert(root, newWord);
}
protected void insert(Node temp, Node newWord){ // Hello, Tree
if (newWord.getW().compareTo(temp.getW()) < 0){
if (temp.getL() == null ) temp.setL(newWord);
else insert(temp.getR(), newWord);
} else{
if (temp.getR() == null) temp.setR(newWord);
else insert(temp.getR(), newWord); // "end", "Tree"
}
}
// Output of the entire tree/ dictionary
public void print(){
print(root);
}
protected void print(Node temp){
if (temp.getL() != null ) print(temp.getL());
System.out.println("Wort: " + temp.getW());
System.out.println("Bedeutung: " + temp.getD());
if (temp.getR() != null ) print(temp.getR());
}
public void search(String word){
search(root, word);
/**
@return this.definition or null
*/
}
protected String search(Node root, String toBeFound){
if ( root == null) return null;
if ( root.getW().compareTo(toBeFound) == 0){
System.out.println("Bedeutung vom gesuchten Wort: " + root.getD());
return root.getD();
}
if (root.getW().compareTo(toBeFound) > 0){
return search(root.getL(), toBeFound);
}
return search(root.getR(), toBeFound);
}
// optional
public void delete(Node word){
}
}
import java.util.*;
public class Dict{
static Tree t = new Tree();
static int menu;
static String word, definition;
static Scanner s = new Scanner(System.in);
static boolean programRunning = true;
public static void main(String[] args){
System.out.println("+++++Spanisch Woerterbuch+++++++");
while ( programRunning ){
showMenu();
}
s.close();
}
private static void showMenu(){
System.out.println("Bitte waehlen Sie eine Aktion aus!");
System.out.printf("1. Einfuegen\t 2. Suchen\t 3. Beenden");
System.out.println();
menu = s.nextInt();
switch (menu){
case 1:
System.out.println("Bitte geben Sie ein Wort ein:");
word = s.nextLine();
s.nextLine();
System.out.println("Bitte geben Sie die Bedeutung ein:");
definition = s.nextLine();
Node node = new Node(word, definition);
t.insert(node);
t.print();
break;
case 2:
System.out.println("Bitte geben Sie das zu suchende Wort ein:");
s.nextLine();
word = s.nextLine();
t.search(word);
break;
case 3:
programRunning = false;
break;
default:
System.out.println("Ungueltige Eingabe!");
}
}
}
Fairly straightforward issue.
Scanner#nextInt()
does not read the newline symbol, and you wrote
word = s.nextLine();
s.nextLine();
which means the first nextLine()
will consume the \n
and output an empty string, while the second will actually read your inputted text, but simply discard it.
Thus, swap them
s.nextLine();
word = s.nextLine();
and you're done. Keep also in mind debuggers exist, try to use them if possible.