user13143967
user13143967

Reputation:

How do I get this return statement to work properly?

This program is meant to import a text file and export the text file and the pig latin translations. The input is a single line text file that reads "It was a dark and stormy night" and the output needs to look as follows:

It     ITWAY
was    ASWAY
a      AWAY
dark   ARKDAY
and    ANDWAY
stormy ORMYSTAY
night  IGHTNAY 

I only get the english words, not the pig latin words. When I initialize pigLatin, it prints whatever the initialization is, but I can't get the return value to actually update the variable. Please help!

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class PigLatinTranslator {

    public static void main(String[] args) throws FileNotFoundException {
        File textFile = new File("/Users/juliansilvestre/eclipse-workspace/ProgrammingExercise4/src/ProgrammingExercise4TextFile.txt");
        Scanner scan = new Scanner(textFile);
        String line = scan.nextLine();
        String[] wordsInFile = new String[7];
        String pigLatin = "";

        for (int i = 0; i < wordsInFile.length; i++) {
            wordsInFile = line.split(" ");
            translatePigLatin(wordsInFile[i], pigLatin);
            System.out.println(wordsInFile[i] + "\t" + pigLatin);
        }

    }

    public static  String translatePigLatin(String english, String pigLatin) {
        String upperCaseWord = english.toUpperCase();
        int index = -1;
        char ch;

        for (int i = 0; i < upperCaseWord.length(); i++) {
            ch = upperCaseWord.charAt(i);

            if (isVowel(ch)) {
                index = i;
                break;
            }
        }

        if (index == 0) {
            return pigLatin = upperCaseWord + "WAY";
        }
        else {
            String x = english.substring(index);
            String y = english.substring(0, index);
            return pigLatin = x + y + "AY";
        }
    }

    public static Boolean isVowel(char ch) {
        if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
            return true;
        }
        return false;
    }

}

Upvotes: 1

Views: 94

Answers (2)

HectorLector
HectorLector

Reputation: 1911

You never assign the return value. I think your function should look like this:

    wordsInFile = line.split(" ");
    for (int i = 0; i < wordsInFile.length; i++) {
        String pigLatin = translatePigLatin(wordsInFile[i]);
        System.out.println(wordsInFile[i] + "\t" + pigLatin);
    }


public static String translatePigLatin(String english) {
   //Do translation logic
   return "YourResultString";
}

You do not need the second method parameter. Think of the method as a blackbox, which has some (needed) inputs and produces one output (no side effects).

Strings in Java are immutable. See this question: can-i-change-string-objects-value-passed-to-my-method

Edit: Thanks for the comment, put the split outside the loop.

Upvotes: 1

Martin Fernandes
Martin Fernandes

Reputation: 176

Here is a working solution for you. One: Your file reader code was incorrect. You need to use a BufferReader to read a file. Scanners are used to read inputs from the console. Also, the scanner obj was never assigned to the String array that was used in the for loop. That's why you must've received a null pointer exception. Two: There was a lot of redundant code which I have removed from your translator. The use of index was unnecessary. Ultimately both the if and the else statement would give you the same output, so you do not need it.

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;

public class PigLatinTranslator {

    public static void main(String[] args) throws FileNotFoundException {
        String[] wordsInFile = readFile("/Users/....").split(" ");
        String pigLatin = "";

        for (int i = 0; i < wordsInFile.length; i++) {
            pigLatin = translatePigLatin(wordsInFile[i]);
            System.out.println(wordsInFile[i] + "\t" + pigLatin);
        }

    }

    public static String readFile(String filename) {
        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader br = new BufferedReader(new FileReader(filename));
            try {
                String line = br.readLine();

                while (line != null) {
                    sb.append(line);
                    line = br.readLine();
                }
                return sb.toString();
            } finally {
                br.close();
            }
        }
        catch (Exception e){
            sb.append("");
        }
        return sb.toString();
    }

    public static String translatePigLatin(String english) {
        String upperCaseWord = english.toUpperCase();
        char ch;

        for (int i = 0; i < upperCaseWord.length(); i++) {
            ch = upperCaseWord.charAt(i);

            if (isVowel(ch)) {
                return english.substring(i).toUpperCase() + "WAY";
            }
        }
        return "";
        }

    public static Boolean isVowel(char ch) {
        if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
            return true;
        }
        return false;
    }

}

Cheers.

Upvotes: 0

Related Questions