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

Thread: Prompting user to open file, then printing in System.out using toUpperCase

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Prompting user to open file, then printing in System.out using toUpperCase

    The assignment instructions themselves are quite vague, but here is what is needed:

    "Using a JFileChooser, prompt the user for a file to open. Using a Scanner, read one line from this file
    at a time, until the end, printing out each line in upper case to System.out."

    I take it he means a .txt file. So if "I like cats" is in the .txt file I would have to get it to print through System.out as "I LIKE CATS", correct? If so, I'm having a bit of trouble. I'm able to prompt the user to open a file, but I'm not exactly sure how I would go about getting it to print in all uppercase characters. Here's what I have so far:

    import javax.swing.JFileChooser;
    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException; 
    public class Program2
    {
        public static void main (String[] args) throws FileNotFoundException
        {
            JFileChooser chooser = new JFileChooser();
            Scanner in = null;
            if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) 
            {
                File selectedFile = chooser.getSelectedFile();
                in = new Scanner(selectedFile);
     
                while(in.hasNextLine())
                {
                    String str = new String(in.nextLine());
                    System.out.println(str.toUpperCase());
                }
            }
        }
    }

    Thanks for the help in advance.

    Edit: Solved. All I had to do was put the Scanner in a String object so I could gain access to the toUpperCase() method. Updated code to signify this just in case anyone else was having the same problem.
    Last edited by Demias; June 15th, 2014 at 12:44 AM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Prompting user to open file, then printing in System.out using toUpperCase

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    Good job figuring it out, and thanks for updating your post with the solution. You can also mark the thread "Solved" using the Thread Tools pull down at the top of the thread frame.

Similar Threads

  1. [SOLVED] Why is system.println printing a reference number
    By cowsquad in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 19th, 2013, 01:33 PM
  2. Printing arrays from user inputs (2 dimensional)
    By kevinsauerzxc in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 13th, 2013, 07:28 AM
  3. Not prompting string user input.
    By Abhi01 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 8th, 2012, 10:26 PM
  4. [SOLVED] Printing an ArrayList of user-defined Objects
    By er1111 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 2nd, 2012, 11:06 AM
  5. char (toUppercase?)
    By chronoz13 in forum Java Theory & Questions
    Replies: 4
    Last Post: December 12th, 2009, 10:01 AM