Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java program help. I have this program but I receive an error message. import java.util.*; import java.io.*; public class Stuffy { public static void main(String[]

Java program help. I have this program but I receive an error message.

import java.util.*;

import java.io.*;

public class Stuffy {

public static void main(String[] args) {

TextIO.put("Enter the path for the file (example: /home/rock/file.txt): ");

String file = TextIO.getlnString();

String translation = translateIntoPigLatin(readFile(file));

TextIO.put(translation);

writeFile(translation, "PigLatinOutput.txt");

TextIO.put("nThe translation has been saved to the current directory as PigLatinOutput.txt");

}

private static String readFile(String filename) {

File file = new File(filename);

StringBuffer contents = new StringBuffer();

BufferedReader reader = null;

try {

reader = new BufferedReader(new FileReader(file));

String text = null;

while ((text = reader.readLine()) != null) {

contents.append(text).append(

System.getProperty("line.separator"));

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (reader != null) {

reader.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

return contents.toString();

}

public static void writeFile(String input, String filename){

try {Writer output = null;

File file = new File(filename);

output = new BufferedWriter(new FileWriter(file));

output.write(input);

output.close();

} catch (IOException e) {

TextIO.put("I/O error");

}

}

private static String translateIntoPigLatin(String input) {

String output = "";

StringTokenizer st = new StringTokenizer(input);

while (st.hasMoreTokens()) {

output += translateWord(st.nextToken()) + " ";

}

return output;

}

private static String translateWord(String word) {

if (isVowel(word.substring(0, 1))) {

word = word + "-ay";

} else {

int vowel_position = 0;

for (int i = 0; i

String letter = word.substring(i, i + 1);

if (isVowel(letter)) {

vowel_position = i;

break;

}

}

if (vowel_position != word.length()) {

word = word.substring(vowel_position, word.length()) + "-"

+ word.substring(0, vowel_position) + "ay";

}

}

word = word.replaceAll("[^a-zA-Z0-9]", "");

return word;

}

private static boolean isVowel(String input) {

String vowels[] = { "a", "e", "i", "o", "u" };

if (Arrays.asList(vowels).contains(input.toLowerCase())) {

return true;

} else {

return false;

}

}

} image text in transcribed

Packzge Expicrer Task List Lsport java.util' ipurt jav.iu All Actvate. 5 Text.put("Enter the 2ath tor the t11e 6 String-Iextio.getinString) (exa"olei cee/rock11e.txt): ..); n 9 8 textin."titranslatinn); Mrt tertletranslstton, -Plglst1nOutput.txt. Text.put("nThe translation has Deen saved to the current directory as Pia;LatinD.tput.txt..); private ststic Sstring ad.(Srine filena) rile file -new rile(Filen) Stringdutfer contents-new Stringtutter); 5 EutteredRescer reader null; wader- ufforedeaderew Filil. in stric textnul; 28 contants.aacend(text.ppend catch (IOException e) [ b w.printStakira 9 Latin 0 try 9 if(reader !. null) { eader.clos 0% public static void nriteF11e(String input. String t11enareH try iter cutpt-1; 2 output.write(inpt); output . close 44 catch (IOExceptien Except1cn in thre57 .#81 10vs.ang. Error : esol ed Unresolved CORD11ation pro-ees: cannot be r Textto canct be resolved r Lotin.nain Latin :) mart In 3:14

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions