Question

In: Computer Science

Java Code using Queue Write a program that opens a text file and reads its contents...

Java Code using Queue

Write a program that opens a text file and reads its contents into a queue of characters, it should read character by character (including space/line change) and enqueue characters into a queue one by one. Dequeue characters, change their cases (upper case to lower case, lower case to upper case) and save them into a new text file (all the chars are in the same order as the original file, but with different upper/lower case)

use linked list class, use nextLine() to read one line into a String, then use String's charAt(i) in a for loop to get characters out one by one, add one extra line change char at the end as '\n'

Ex input file:

The Brown Fox

Jumped

EX output file:

tHE bROWN fOX

jUMPED

Solutions

Expert Solution

Raw_code:

import java.util.*; // importing LinkedList and Scanner class
import java.io.*; // importing FileWriter and PrintWriter class

public class main{
// main function
public static void main(String[] args)
throws Exception{
// crating file object on input text file and Scanner object of File object
File filein = new File("input.txt");
Scanner input = new Scanner(filein);
// creating FileWriter object on output text file and PrintWriter object on FileWriter object
FileWriter fileout = new FileWriter("output.txt");
PrintWriter output = new PrintWriter(fileout);

// creating LinkedList of Character
LinkedList<Character> queue = new LinkedList<Character>();
// temporary String variable for storing each line read from input file

String temp_str;
while(input.hasNext()){
// reading line by line from input file and storing in temp_str
temp_str = input.nextLine();
// iterating over the temp_str
for(int ind = 0; ind<temp_str.length(); ind++)
// adding each character in string to queue
queue.add(temp_str.charAt(ind));
// adding new character to queue for each new line read from input file
queue.add('\n');
}

// temporary Character variable for storing each Character in queue
Character temp_char;
// iterating over each Character in queue
for(int ind = 0; ind < queue.size(); ind++){
// getting the Character and storing in temp_char
temp_char = queue.get(ind);
// checking whether Character is alphabet or not
if(Character.isAlphabetic(temp_char))
// if character is in lower case
if(Character.isLowerCase(temp_char))
// replacing the lower case character by converting upper case
queue.set(ind, Character.toUpperCase(temp_char));
// if character is in upper case
else if (Character.isUpperCase(temp_char))
// replacing the upper case character by converting lower case
queue.set(ind, Character.toLowerCase(temp_char));
}

// iterating thorugh each Character in queue
for(var character: queue)
// writing each Character to output file
output.write(character);
// for debugging
System.out.println("Written to output file");
// closing both input and output file streams
output.close();
input.close();
}
}


Related Solutions

Java Code using Stack Write a program that opens a text file and reads its contents...
Java Code using Stack Write a program that opens a text file and reads its contents into a stack of characters, it should read character by character (including space/line change) and push into stack one by one. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse of their order in the first file. Ex input file: Good...
C Programming Write a program in C that reads in a file, stores its contents as...
C Programming Write a program in C that reads in a file, stores its contents as a character array/pointer (char*) into an unsigned character array/pointer (unsigned char* message). Note: the input file can have one line or multiple lines and vary in length
Write a JAVA program that reads a text file into RAM efficiently, takes a regular expression...
Write a JAVA program that reads a text file into RAM efficiently, takes a regular expression from the user, and then prints every line that matches the RE.
Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Write a program that reads a file called document.txt which is a text file containing an...
Write a program that reads a file called document.txt which is a text file containing an excerpt from a novel. Your program should print out every word in the file that contains a capital letter on a new line to the stdout. For example: assuming document.txt contains the text C++
Write Java program Lab52.java which reads in a line of text from the user. The text...
Write Java program Lab52.java which reads in a line of text from the user. The text should be passed into the method: public static String[] divideText(String input) The "divideText" method returns an array of 2 Strings, one with the even number characters in the original string and one with the odd number characters from the original string. The program should then print out the returned strings.
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type...
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces another text file in Which blank lines are removed, multiple blanks are replaced with a single blank, and no lines are longer than some given length (let say 80). Put as many words as possible on the same line (as close as possible to 80 characters). You will have to break some lines of the given file, but do not break any words or...
You are given a text file containing a short text. Write a program that 1. Reads...
You are given a text file containing a short text. Write a program that 1. Reads a given text file : shortText.txt 2. Display the text as it is 3. Prints the number of lines 4. Prints the occurences of each letter that appears in the text. [uppercase and lowercase letter is treated the same]. 5. Prints the total number of special characters appear in the text. 6. Thedisplayofstep3,4and5aboveshouldbesaveinanoutputfile:occurencesText.txt write it in C++ programing Language
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT