3 Replies - 8742 Views - Last Post: 25 September 2010 - 01:35 AM Rate Topic: -----

#1 Guest_talha*


Reputation:

UpperCase File Converter

Posted 23 September 2010 - 06:11 PM

the question

Write a program that asks the user for the names of two files. The first file should be opened for reading and the second file should be opened for writing. The program should read the contents of the first file, change all characters to uppercase, and store the results in the second files. The second file will be a copy of the first file, except that all the characters will be uppercase. Have some sort of input validation. Use Notepad to create a simple file that can be used to test the program.

Ive tried and tried.Any help at this point would be great.
Heres what I have so far
import java.util.Scanner;
import java.io.*;

public class UppercaseFileConverter2
{
	public static void main(String[] args)throws IOException
	{
		String filename;
		String message;
		String filename2;
		
		//Create a Scanner object for keyboard input.
		Scanner keyboard = new Scanner(System.in);
		
		//Get the filename.
		System.out.print("Enter the filename: ");
		filename = keyboard.nextLine();
		
		//Open the file.
		FileWriter fwriter = new FileWriter(filename);
		PrintWriter outputFile = new PrintWriter(fwriter);
		
		//Get the message to be written in the file.
		System.out.println("Enter a message: ");
		message = keyboard.nextLine();
		
		//Write the message to the file.
		outputFile.println(message);
		
		//Close the file.
		outputFile.close();
		
		//Get the second file's name.
		System.out.println("Enter the name of the second file: ");
		filename2 = keyboard.nextLine();
		
		//Open an input file.
		FileReader freader = new FileReader(filename2);
		BufferedReader inputFile = new BufferedReader(freader);
		String str;
		
		//Read the first item.
		str = inputFile.readLine();
		
		//If the item was read, display it in UpperCase.
		while (str != null)
		{
			System.out.println(str);
			String upper = message.toUpperCase();
			str = inputFile.readLine(upper);
		}

		//Close the file.
		inputFile.close();
	}
}



Is This A Good Question/Topic? 0

Replies To: UpperCase File Converter

#2 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: UpperCase File Converter

Posted 23 September 2010 - 06:22 PM

What exactly is the problem you have? Telling us makes it much easier to help you :)

And you will want to write the new Strings to your new file in the loop.
Was This Post Helpful? 0
  • +
  • -

#3 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: UpperCase File Converter

Posted 23 September 2010 - 08:52 PM

According to what you said above the file that you are going to read already exist:

Quote

Use Notepad to create a simple file that can be used to test the program.

But in the lines from 24 to 31 you are asking for the user to input a message and you save it to the file. You should just get the name of the file and wait until you read the other file in order to write within it. I think that you should remove the lines I mentioned. So you already have the outputFile object where you are going to write the lines in upper case. In the line 46 you already have a 'while' loop and in this loop you have the str object which is going to contain each line from the source file. So you should convert this line to upper case and use the outputFile object in order to save the new line in upper case inside of the output file and that's all.
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2774
  • View blog
  • Posts: 11,735
  • Joined: 20-September 08

Re: UpperCase File Converter

Posted 25 September 2010 - 01:35 AM

You can make your app more efficient by ignoring lines. Make sure you read the file name before you start reading/writing files

int buf = -1;
while((buf = freader.read()) > -1) {
	if(Character.isLetter(buf)) {
		buf = Character.toUpperCase(buf);
	}
	fwriter.write(buf);
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1