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 3 of 3

Thread: Input/Output file help

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Input/Output file help

    Upper File Converter
    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 file. The second file will be a copy of the first file, except that all the characters will be uppercase. Use Notepad or another text editor to create a simple file that can be used to test the program.

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package javaapplication6;
    import java.util.Scanner;
    import java.io.*;
     
    /**
     *
     * @author uuuuuuu
     */
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws IOException{
            Scanner keyboard = new Scanner(System.in);
     
            System.out.println("Enter first file name.");
            String file1 = keyboard.nextLine();
     
            System.out.println("Enter second file name.");
            String file2 = keyboard.nextLine();
     
            File file = new File(file1);
            Scanner inputFile = new Scanner(file);
     
            PrintWriter outputFile = new PrintWriter(file2);
     
            while(inputFile.hasNext())
            {
                file2 = inputFile.nextLine();
                outputFile.println(file1.toUpperCase());
                System.out.println(file2);
            }
            inputFile.close();
            outputFile.close();
        }
     
    }

    This is the Notepad file that I used (testfile.txt):
    Frankie owned a
    ferocious feline named
    Freddy as a pet.
    Freddy was funny and
    furry.
    His fur is bright red
    with black stripes.

    I inserted the statement:
    System.out.println
    so I can see what is written in file 2.

    What is wrong with my code...the output of file 2 is still lowercase.
    Why is it still lowercase?
    Last edited by Plural; October 25th, 2010 at 08:26 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Lightbulb Re: Input/Output file help

    Quote Originally Posted by Plural View Post
    Upper File Converter
    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 file. The second file will be a copy of the first file, except that all the characters will be uppercase. Use Notepad or another text editor to create a simple file that can be used to test the program.

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package javaapplication6;
    import java.util.Scanner;
    import java.io.*;
     
    /**
     *
     * @author uuuuuuu
     */
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws IOException{
            Scanner keyboard = new Scanner(System.in);
     
            System.out.println("Enter first file name.");
            String file1 = keyboard.nextLine();
     
            System.out.println("Enter second file name.");
            String file2 = keyboard.nextLine();
     
            File file = new File(file1);
            Scanner inputFile = new Scanner(file);
     
            PrintWriter outputFile = new PrintWriter(file2);
     
            while(inputFile.hasNext())
            {
                file2 = inputFile.nextLine();
                outputFile.println(file1.toUpperCase());
                System.out.println(file2);
            }
            inputFile.close();
            outputFile.close();
        }
     
    }

    This is the Notepad file that I used (testfile.txt):
    [java=highlight/]Frankie owned a
    ferocious feline named
    Freddy as a pet.
    Freddy was funny and
    furry.
    His fur is bright red
    with black stripes.
    [/highlight]

    I inserted the statement:
    System.out.println
    so I can see what is written in file 2.

    What is wrong with my code...the output of file 2 is still lowercase.
    Why is it still lowercase?
    Because,

    file1.toUpperCase() only makes the String name the user entered go to upper case. It doesn't make the data in that file go to upper case.

    You need not have file2 as file1.nextLine();

    String str = file1.nextLine(); would probably be nicer, especially if, you probably don't, you ever need to do anything with that file 2 again, and you've changed its name.

    String str;
    = inputFile.nextLine();
    outputFile.println(str.toUpperCase());
    System.out.println(str);

    or

    System.out.println(str.toUpperCase()) depending on if you're hoping to print out the upper case here too or not.

    However, I'm not sure if the while loop will print them all out, or just one line (i.e. the last line).

    I know it'll work better either way.
    Last edited by javapenguin; October 25th, 2010 at 08:39 PM.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Input/Output file help

    Quote Originally Posted by javapenguin View Post
    Because,

    file1.toUpperCase() only makes the String name the user entered go to upper case. It doesn't make the data in that file go to upper case.
    What do I have to change in order for it to happen?


    Ah, I found out why the output wasn't Upper Case.
    It was because I didn't put .toUpperCase() in back of it.
    I think the file would've been Upper Cased.
    Last edited by Plural; October 25th, 2010 at 08:57 PM.

Similar Threads

  1. Input/Output file help
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 23rd, 2010, 06:26 PM
  2. Output an int i to a file
    By NightFire91 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 17th, 2010, 10:45 AM
  3. [SOLVED] java me how to: input - math operation - output
    By Lifer in forum Java ME (Mobile Edition)
    Replies: 3
    Last Post: April 7th, 2010, 05:36 PM
  4. Simple Input/Output program Acting weird
    By drexasaurus in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 19th, 2010, 02:15 PM
  5. Replies: 8
    Last Post: January 6th, 2010, 09:59 AM