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:
Code :
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.
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.