1

Java 11.6

I am trying to write a java class that will take in a filename as a parameter then read line by line of that file. After it is done reading it will convert the file to equivalent morse code. My other methods such as "print morse table" "convert string to morse" works as it should but for the file function, every time I type the file name it just gives me an empty output. Not sure where exactly is the problem. I am posting the method underneath.

 public void encodeFile(String s){
    try{
        BufferedReader br = new BufferedReader(new FileReader(s));

        while((br.readLine()) != null){
            String st="";
            if( st ==" "||st==null){
                System.out.println("");
            }else{
                int length = st.length();
                for(int i=0;i<length;i++){
                    char c = st.charAt(i);
                    int x = c;
                    if(x>96 && x<123){
                        c = Character.toUpperCase(c);
                    }
                    if(c==' '){
                        System.out.printf(" ");
                    }else{
                        for(int j=0; j<listCodes.length; j++){
                            if(c==listCodes[j].getCharacter()){
                                System.out.printf(listCodes[j].getEncoding() + " ");
                                break;
                            }
                            if(listCodes[j+1]==null){
                                System.out.printf("?");
                                break;
                            }
                        }
                    }
                }
            }
            System.out.printf("%n");
        }
        br.close();
    }catch (Exception e){
        System.out.println("java.lang.Exception: Invalid File Name: " + s);
    }
}
galo2324
  • 129
  • 1
  • 9
  • You call `readLine()` which returns the next line from the file, but you don't store the value in a variable so you can use it. Change code to: `String st; while ((st = br.readLine()) != null) { ...` – Andreas Mar 23 '20 at 22:44
  • The first `if` statement can never be true. You already know that `st` is not null, and you can't use `==` to compare strings. --- See: [How do I compare strings in Java?](https://stackoverflow.com/q/513832/5221149) – Andreas Mar 23 '20 at 22:45
  • 1
    Instead of `if(x>96 && x<123)`, use `if (c >= 'a' && c <= 'z')` to make the code more self-documenting. Then get rid of the now-unused `x` variable. – Andreas Mar 23 '20 at 22:47
  • 1
    Don't call `printf` unless you actually supply a format string. Call `print` instead. – Andreas Mar 23 '20 at 22:48
  • 1
    although it is not related to qs, You should close file/buffer in finally block.. 101 – Sendi_t Mar 23 '20 at 22:58

1 Answers1

0

The problem is that you are not storing the value of br.readLine() into st. Apart from this, you should use .equals instead of == to compare two strings.

Do it as follows:

public void encodeFile(String s){
    try{
        BufferedReader br = new BufferedReader(new FileReader(s));
        String st="";
        while((st = br.readLine()) != null){ // Store the value of `br.readLine()` into `st` and check if it is not null
            if(st.equals(" ")){ // You need not check `st` for null again.
                System.out.println("");
            }else{
                //...
            }
            //...
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110