1

So, I have this assignment to read the text from one file and write to another, changing all the text to uppercase. I'm not sure why, but when I run it, it freezes after I enter the file to write to. Any help is greatly appreciated.

import java.io.*;
import java.util.Scanner;

public class McKinneyBenjaminUpperFile
{
   public static void main(String [] args)throws IOException
   {
      Scanner keyboard = new Scanner(System.in);

      System.out.println("Benjamin's Uppercase File Converter\n");

      String file1;
      String file2;

      System.out.println("Enter the name of the file to read from.");
      file1 = keyboard.nextLine();

      File file1open = new File(file1);

      if(!file1open.exists())
      {
         System.out.println("The file you entered does not exists.");
         System.exit(0);
      }

      Scanner infile = new Scanner(file1);

      System.out.println("Enter the name of the file to write to: ");
      file2 = keyboard.nextLine();

      File file2open = new File(file2);

      if(file2open.exists())
      {
         System.out.println("The file you entered already exists.");
         System.exit(0);
      }

      PrintWriter outfile = new PrintWriter(file2);

      while(infile.hasNext())
      {
         for(int i=0;i<file2open.length();i++)
         {
            String line = infile.nextLine().substring(0,1);
            String newLine = line.toUpperCase();
            outfile.println(newLine);
         }
      }

      System.out.println("Conversion complete. " + file1 + " is now converted to all uppercase in " + file2);
   }
}
MicroBM
  • 7
  • 5
  • where are you giving the file path? – karthick23 Feb 25 '16 at 06:25
  • I was unaware I needed to, and I'm not sure how to do that. I have some C++ experience, but files in Java appear to be entirely different – MicroBM Feb 25 '16 at 06:28
  • I don't really understand the intent of your code, but it looks like you never enter the for loop, since `file2open.length()` is always zero. As such, you are never consuming anything from `infile`, so `infile.hasNext()` continues to evaluate to true. – Andy Turner Feb 25 '16 at 06:31
  • Thank you for pointing that out, I didn't notice it at all. Truly appreciate it. – MicroBM Feb 25 '16 at 06:31

2 Answers2

5

but when I run it, it freezes after I enter the file to write to

In this loop:

for(int i = 0; i < file2open.length(); i++) { ... }

file2open.length() will return 0 since you have not created the destination file yet. See also File.length():

Returns: The length, in bytes, of the file denoted by this abstract pathname, or 0L if the file does not exist.

Hence the loop will never get executed. This results in infile.nextLine() never being called, hence your outer while loop will loop forever since hasNext() will continously return true.

Note that a File is only a representation of the File's meta data (like name and attributes). To write to a file, use a stream or writer class, like FileOutputStream.

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
  • So now, I'm having an issue with substring. i only included it so that my string line would read in one character at a time, but I'm not sure how to get the rest of the file to read in. – MicroBM Feb 25 '16 at 06:34
  • That's because you read the whole line and then only uppercase the first letter of it. Remove the substring part entirely and just uppercase the line `i.e. line.toUpperCase()` – W.K.S Feb 25 '16 at 06:37
  • You probably should give a concrete example of what you actually want to achieve - do you want **each** character be upper case, or only the first character of each line? – Andreas Fester Feb 25 '16 at 06:38
2

It is your while and for loop

 while(infile.hasNext())
  {
     for(int i=0;i<file2open.length();i++)
     {
        String line = infile.nextLine().substring(0,1);
        String newLine = line.toUpperCase();
        outfile.println(newLine);
     }
  }

file2open.length() is to blame because .length() returns the length of the file denoted by the abstract pathname. Since it is a long, I assume it is doing some crazy calculations and the reason why your program freezes is because it is actually just doing a lot of calculations and still running. Nevertheless...

This would be a better way to covert:

  while(infile.hasNextLine())
  {
        String line = infile.nextLine();
        String newLine = line.toUpperCase();
        outfile.println(newLine);
  }

There is no reason to do individual substrings.

EDIT: Didn't read code correctly so the .length is in fact returning 0. But regardless, the .length is doing something different that what you think I'm pretty sure.

  • Thank you for the help, I greatly appreciate it. – MicroBM Feb 25 '16 at 06:40
  • Just tried this and got this error: "Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Scanner.java:1540) at McKinneyBenjaminUpperFile.main(McKinneyBenjaminUpperFile.java:45)" – MicroBM Feb 25 '16 at 06:40
  • @MicroBM Actually I would not even use a `Scanner` to read the source file - use a `BufferedReader` and its `readLine()` method. See also http://stackoverflow.com/questions/5868369/how-to-read-a-large-text-file-line-by-line-using-java – Andreas Fester Feb 25 '16 at 06:43
  • Haven't learned about BufferedReader yet. The professor for the class has briefly touched on files, but nothing too in depth @AndreasFester – MicroBM Feb 25 '16 at 06:44
  • Sorry should be: file.hasNextLine() – Trace Carrasco Feb 25 '16 at 06:52