0

I am working on a program that reads a text file that holds an entire dictionary and will take the word, pronunciation, and definition and add it to an AVL tree. I am working on the loop that reads through the .txt file and stores the different things(word, definition, ...) into strings. Whenever the loop runs, it adds an extra enter in the definition string. This is my Code:

package text;

import java.util.*;
import java.io.*;
public class Text {
    public static void main(String args[]) throws IOException{

        File file = new File(args[0]);
        Scanner scanner = new Scanner(file);
        int cap = 0, proLine = 0;
        String word="", pro="",def="";
        String strLine;
        while (scanner.hasNext()){
            strLine = scanner.nextLine();
            if(isCaps(strLine))
                cap++; proLine = 1;
            if((isCaps(strLine)) && cap == 1)
                word = strLine;
            if(proLine == 1 && cap ==1)
                pro = strLine;
            if(isDefn(strLine)){
                def = strLine;
            }
            if(cap == 2){
                System.out.println(word+"\n"+pro+"\n"+def);
                cap = 0; 
                proLine = 0;                
            }
        }
    }
    private static boolean isCaps(String str){return (str.equals(str.toUpperCase()));}

    private static boolean isDefn(String str){return str.contains("Defn");}
}

This is the text file i have been using
"ABAFT

A*baft", adv. (Naut.)

Defn: Toward the stern; aft; as, to go abaft.

ABAISANCE

A*bai"sance, n. Etym: [For obeisance; confused with F. abaisser, E. abase]

Defn: Obeisance. [Obs.] Jonson."

This is my output every time
image

  • `proLine` is always `1`, since `proLine = 1;` is not governed by the `if` statement, contrary to what the code looks like. Just because `cap++;` and `proLine = 1;` are on the same line, doesn't change the fact that an `if` statement without `{ }` only governs **one statement**, i.e. `cap++;` in this case. – Andreas Apr 23 '20 at 02:27
  • I did not know that, thanks! Sadly this did not solve my problem of the loop adding 2 enters before printing the definition. I also do not know why the loop wont continue on to the next word in the dictionary in the text file. – Ethan Bridgehouse Apr 23 '20 at 02:32
  • Have you tried **debugging** your code? --- [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149) – Andreas Apr 23 '20 at 02:35
  • I can't even help with that, since I cannot copy the text file content. --- [Why not upload images of code on SO when asking a question?](https://meta.stackoverflow.com/q/285551/5221149) --- But that's fine, since you should do your own debugging, not ask us to debug your code for you. – Andreas Apr 23 '20 at 02:37
  • Yeah, I am new to posting here so I didn't know the proper technique. I just wanted to see if anyone noticed something I did wrong since I can't go to see my professors anymore to get help. Thank you for trying though. – Ethan Bridgehouse Apr 23 '20 at 02:52
  • Thank you for the debugger link. I fixed my problem. – Ethan Bridgehouse Apr 23 '20 at 21:39

0 Answers0