In this project we are meant to be taking control line arguments and then input
ID: 3632576 • Letter: I
Question
In this project we are meant to be taking control line arguments and then input and outputting the translation (in either pigLatin english or rot13). Here is the code I have so far, but when I try to run it, it doesnt output anything, I think it is because of how I am reading the input, I am meant to take any non-letter character and pretend it is a space but I am not sure how.
sample input ;
SampleInput1.txt
"The problem of viruses is temporary and will be solved in two years."
– John McAfee, 1988
"Computer viruses are an urban legend."
– Peter Norton, 1988
"I have always wished that my computer would be as easy to use as my telephone. My wish has come true. I no longer
know how to use my telephone." - Bjarne Stroustrup
output would be;
"heTay roblempay ofway irusesvay isway emporarytay andway illway
ebay olvedsay inway wotay earsyay."
– ohnJay cAfeeMay, 1988
"omputerCay irusesvay areway anway urbanway egendlay."
– eterPay ortonNay, 1988
"Iway avehay alwaysway ishedway hattay ymay omputercay ouldway
ebay asway easyway otay useway asway ymay elephonetay. yMay
ishway ashay omecay ruetay. Iway onay ongerlay nowkay owhay otay
useway ymay elephonetay." - jarneBay troustrupSay
/**
Description: Program which translates a string using Piglatin and ROC13
*/
import java.util.*;
public class HW5 {
public static void main(String[] args) {
String translatedWord = "", word = "";
// loop to translate each of the words in the sentence
if (args[0].equals("piglatin")) {
translatedWord = pigLatin(word);
}
if (args[0].equals("piglatin_inv")){
translatedWord = pigLatinInv(word);
}
if (args[0].equals("rot13")) {
translatedWord = rot13(word);
}
// Printing out answer
System.out.print(translatedWord);
}
// Piglatin method
private static String pigLatin(String word) {
Scanner kb = new Scanner(System.in);
while (kb.hasNext()) {
String input = kb.nextLine();
String[] words = input.split(" ");
for (int i = 0; i < words.length; i++) {
String origWord = words[i];
if (origWord.charAt(0) == 'a' || origWord.charAt(0) == 'e'
|| origWord.charAt(0) == 'i'
|| origWord.charAt(0) == 'o'
|| origWord.charAt(0) == 'u') {
origWord = origWord + "way";
} else {
origWord = origWord.substring(1) + origWord.charAt(0)
+ "ay";
}
word.concat(origWord);
}
}
return word;
}
// Inverse piglatin method
private static String pigLatinInv(String word) {
Scanner kb = new Scanner(System.in);
while (kb.hasNext()) {
String input = kb.next();
String[] words = input.split(" ");
for (int i = 0; i < words.length; i++) {
String origWord = words[i];
if (origWord.endsWith("way")) {
int length = origWord.length();
length = length - 3;
origWord = origWord.substring(0, length);
} else if (origWord.endsWith("ay")) {
int length = origWord.length();
length = length - 2;
origWord = origWord.charAt(length - 1)
+ origWord.substring(0, length - 1);
}
word.concat(origWord);
}
}
return word;
}
// Rot13 method
private static String rot13(String word) {
Scanner kb = new Scanner(System.in);
while (kb.hasNext()) {
String input = kb.next();
String[] words = input.split(" ");
for (int i = 0; i < words.length; i++) {
String origWord = words[i];
for (int j = 0; j < origWord.length(); j++) {
char c = origWord.charAt(j);
if (c >= 'a' && c <= 'm')
c += 13;
else if (c >= 'n' && c <= 'z')
c -= 13;
else if (c >= 'A' && c <= 'M')
c += 13;
else if (c >= 'N' && c <= 'Z')
c -= 13;
origWord += c;
}
word.concat(origWord);
}
}
return word;
}
}
Explanation / Answer
You do 10+40 and 30+48. Add it up and that is your answer.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.