Question: I got this help using charArray but is there anyway not using charArray but only using if -else if- else statement , switch statement and
I got this help using charArray but is there anyway not using charArray but only using if-else if-else statement, switch statement and several kinds of loop statement to have same output?
The question is
1) Import the file in specific places
2) Reverse the case of all letters so that all lower-case letters in input.txt will show up in output.txt as upper-case, all upper-case letters in input.txt will show up in output.txt as lower-case.
3) Replace All numbers/digits with a hyphen-separated list of the words for the digits in that number.
Some examples: the number 123 in input.txt will show up in output.txt as one-two-three; the number 7 in input.txt will show up in output.txt as seven.
4) Lastly, output.txt should end up with the same number of lines as in input.txt
Remember: As lines are written to the output file, output.txt, they should also be displayed to the console for the user to see.
So far, I got the part 1, and I know what the code of part 2, but It doesn't work when I compile them to check whether I did it correctly. And I have no idea about part 3 and 4.
It is long question to solve it but I really want to know how to approach this question for the upcoming exam.
Please help
import java.util.Scanner; import java.io.*; public class PracticeReverse { public static void main(String[] args) throws IOException { // input file File file = new File("input.txt"); Scanner inputFile = new Scanner(file); // buffered writer to write into output.txt BufferedWriter out = new BufferedWriter(new FileWriter("output.txt")); // Read lines from the file until no more are left. while (inputFile.hasNext()) { // Read the line from file String line = inputFile.nextLine(); String outputLine = ""; // convert String to character array by using toCharArray char[] lineArray = line.toCharArray(); for (int i = 0; i< lineArray.length; i++) { // check if the character is alphabet or not if(Character.isAlphabetic(lineArray[i])){ // check if upper case or not if(Character.isUpperCase(lineArray[i])){ // convert it to lower case and append to result => for part 2 outputLine = outputLine + Character.toLowerCase(lineArray[i]); } else { // characte is lower case so convert to upper case => for part 2 outputLine = outputLine + Character.toUpperCase(lineArray[i]); } } // check if character is digit or not else if(Character.isDigit(lineArray[i])){ // find corresponding string for number => for part 3 String numberString =""; switch(lineArray[i]){ case '0' : numberString = "zero"; break; case '1' : numberString = "one"; break; case '2' : numberString = "two"; break; case '3' : numberString = "three"; break; case '4' : numberString = "four"; break; case '5' : numberString = "five"; break; case '6' : numberString = "six"; break; case '7' : numberString = "seven"; break; case '8' : numberString = "eight"; break; case '9' : numberString = "nine"; break; default : numberString = lineArray[i]+""; } // check if previous character was number too if(i-1>=0 && Character.isDigit(lineArray[i-1])){ // add - at front outputLine = outputLine + "-" + numberString; } else{ // add number string to line without - outputLine = outputLine + numberString; } } else{ // when character is not digit and not alphabet add it as it is outputLine = outputLine + lineArray[i]; } } // write output on conosle System.out.println(outputLine); // write in output.txt out.write(outputLine); // new line in file after writing each line => for part 4 if(inputFile.hasNext()){ out.newLine(); } } // Close the file. inputFile.close(); out.close(); } }
Input text is this:
Here is A TEXT
123 File To Be
Processed FOR Lab09.
There are now 3218 ways to
DESIGN this kind of PROGRAM.
Here's hoping you can figure OUT 1, 2
or 3 of these designs -
Or AT LEAST come close, even if
you don't find all 3218
Output text is this:
hERE IS a text one-two-three fILE tO bE pROCESSED for lABzero-nine. tHERE ARE NOW three-four-five-nine WAYS tO design THIS KIND OF program. hERE'S HOPING YOU CAN FIGURE out one, two OR three OF THESE DESIGNS - oR at least COME CLOSE, EVEN IF YOU DON'T FIND ALL three-four-five-nine
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
