Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: writing to an output...

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default writing to an output...

    I wrote this code to access a file for input, but now i need to make the input into an output converting all characters to upper.
    import java.util.Scanner;
    import java.io.*;
     
    public class IoDemo
    {
    	public static void main(String[] args) throws IOException
    	{
    		//Create scanner object
    		Scanner keyboard = new Scanner(System.in);
     
    		//Get file name
    		System.out.println("Enter the name of the input file: ");
    		String filename = keyboard.nextLine();
     
    		//open the file
    		File file = new File(filename);
    		Scanner inputFile = new Scanner(file);
     
    		//Close the file
    		inputFile.close();
     
    	}
    }

    the planned output should go:
    Enter the name of the input file: quotations.txt
    Enter the name of the output file: allupper.txt
    thats all that the program will display but it turns the text in the input file into a new output file that is now all upper.
    How do I write the code to directly create a new text file while turning all the text into uppercase?
    Last edited by lairel; February 28th, 2011 at 10:15 PM.


  2. #2
    Junior Member
    Join Date
    Feb 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: writing to an output...

    import java.util.Scanner;
    import java.io.*;
     
    public class IoDemo
    {
    	public static void main(String[] args) throws IOException
    	{
    		//Create scanner object
    		Scanner keyboard = new Scanner(System.in);
     
    		//Get file name
    		System.out.print("Enter the name of the input file: ");
    		String filename = keyboard.nextLine();
     
    		//open the file
    		File file = new File(filename);
    		Scanner inputFile = new Scanner(file);
     
    		//create new file
    		System.out.print("Enter the name of the output file: ");
    		String newFile = keyboard.nextLine();
     
    		//open new file
    		PrintWriter outputFile = new PrintWriter(newFile);
     
    		//convert to upper
    		String toUpper = filename.toUpperCase();
    		outputFile.println(toUpper);
     
    		//Close the file
    		inputFile.close();
    		outputFile.close();
     
    	}
    }

    this is what i have, but it isn't working correctly, instead of converting the contents of the input file into uppercase and then putting it in the new output file, it converts the input of the file name into uppercase in the output file.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: writing to an output...

    		//convert to upper
    		String toUpper = filename.toUpperCase();
    		outputFile.println(toUpper);
    That's because you're converting the filename to upper case rather than the data inside the file.

    Read the text in from the file first, then convert it to upper case and write it back out.

    To read in from a file:

    StringBuilder fileTextBuilder = new StringBuilder();
    while(inputFile.hasNext())
    {
        fileText.append(inputFile.nextLine());
        if(inputFile.hasNext())
        {
            fileText.append("\n");
        }
    }
    String fileText = fileTextBuilder.toString();
    // TODO: convert fileText to upper case
    // TODO: write fileText out to the new file

  4. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: writing to an output...

    i'm still getting errors, this is what i have so far
       import java.util.Scanner;
       import java.io.*;
     
       public class IoDemo
       {
          public static void main(String[] args) throws IOException
          {
          //Create scanner object
             Scanner keyboard = new Scanner(System.in);
     
          //Get file name
             System.out.print("Enter the name of the input file: ");
             String filename = keyboard.nextLine();
     
          //open the file
             File file = new File(filename);
             Scanner inputFile = new Scanner(file);
     
          //create new file
             System.out.print("Enter the name of the output file: ");
             String newFile = keyboard.nextLine();
     
          //open new file
             PrintWriter outputFile = new PrintWriter(newFile);
     
          //convert to upper
             StringBuilder fileTextBuilder = new StringBuilder();
             while(inputFile.hasNext())
             {
                fileTextBuilder.append(inputFile.nextLine());
                if(inputFile.hasNext())
                {
                   fileTextBuilder.append("\n");
                }
             }
     
            String output = fileTextBuilder.toUpperCase();
            outputFile.println(output);
     
             String fileText = fileTextBuilder.toString();
          	outputFile.flush();
     
          //Close the file
             inputFile.close();
             outputFile.close();
     
          }
       }

    symbol : method toUpperCase()
    location: class java.lang.StringBuilder
    String output = fileTextBuilder.toUpperCase();
    ^
    1 error

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: writing to an output...

    The issue is with:

    String output = fileTextBuilder.toUpperCase();

    The method toUpperCase() is undefined for the type StringBuilder

    I can see you have took onboard what helloworld922 said but you need to change the code slightly.

    Try this:

             String fileText = fileTextBuilder.toString();
             String output = fileText.toUpperCase();
     
             outputFile.println(output);
          	 outputFile.flush();
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. writing java code help
    By jaisan72980 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 12th, 2011, 03:39 PM
  2. Help writing a Template Class
    By mamipapi in forum Object Oriented Programming
    Replies: 2
    Last Post: October 5th, 2010, 09:34 PM
  3. writing to JEditorPane
    By nasi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 8th, 2010, 09:23 PM
  4. Writing Output To New File
    By Scottj996 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: January 6th, 2010, 07:25 PM
  5. I need help writing this program
    By kev2000 in forum Algorithms & Recursion
    Replies: 5
    Last Post: June 4th, 2009, 03:14 AM