• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 425
  • Last Modified:

File input convert to lower case

I have to be able to read a file convert all upper case to lower case and output it to another file. I have it working but it only outputs one line. If more then one line it outputs only the last line. Please give me some idea of how to make it do this for the whole file/document. It seems to ignore the rest of the file.


import java.io.*;
 
public class ConverttoLowercase{
 
  String convertAllUpperCaseToLowerCase(String original){
 
    StringBuffer sb = new StringBuffer(original);
    for (int i = 0; i < sb.length(); ++i){
      char c = sb.charAt(i);
      if (Character.isUpperCase(c)){
        c = Character.toLowerCase(c); // c = c - ('A' - 'a')
      }
      sb.setCharAt(i, c);
    }
    return new String(sb);
      }
  static String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ";
 
  boolean isAlpha(char c){
    if (alpha.indexOf(c) != -1){ 
      return true;
    }
    else{
      return false;
    }
  }
    public static void main(String[] args) throws Exception{ 
    String line;
    boolean gotSpace;
 
    ConverttoLowercase eho = new ConverttoLowercase();
 
    gotSpace = false;
    System.out.print("");
    BufferedReader br = new BufferedReader(new FileReader("input.txt"));
    while ((line = br.readLine()) != null){
      if (line.length() < 1){ 
        break; 
        	}
      line = eho.convertAllUpperCaseToLowerCase(line);
      for (int i = 0; i < line.length(); ++i){
        char c = line.charAt(i);
        if (eho.isAlpha(c)){
          gotSpace = true;
          	 PrintWriter out = new PrintWriter(new BufferedWriter (new FileWriter("out.txt")));
          out.print(line);
          out.close();
        }
        else if (c == ' ' || c == '\t'){ 
          if (gotSpace){
            continue; 
          }
          else{
            }
        }
        
      } 
      	
    } 
    } 
}

Open in new window

0
navyretired
Asked:
navyretired
1 Solution
 
navyretiredAuthor Commented:
Sorry it only outputs the last line not the first.
0
 
objectsCommented:
>                  PrintWriter out = new PrintWriter(new BufferedWriter (new FileWriter("out.txt")));

move that *outside* your loop
ie. before the loop starts

0
 
objectsCommented:
   BufferedReader br = new BufferedReader(new FileReader("input.txt"));
    PrintWriter out = new PrintWriter(new BufferedWriter (new FileWriter("out.txt")));
    while ((line = br.readLine()) != null){
      if (line.length() < 1){
        break;
                }
      line = eho.convertAllUpperCaseToLowerCase(line);
      for (int i = 0; i < line.length(); ++i){
        char c = line.charAt(i);
        if (eho.isAlpha(c)){
          gotSpace = true;
          out.print(line);
          out.close();
        }
        else if (c == ' ' || c == '\t'){
          if (gotSpace){
            continue;
          }
          else{
            }
        }
       
      }

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
navyretiredAuthor Commented:
Now it ony does the first line
0
 
objectsCommented:
>           out.close();

sorry, and move the close() to after everything is done

0
 
navyretiredAuthor Commented:
Is this what you mean? I still cannot get it to do more then one line. I know it needs cleaned up but I wanted to get it working first. Again thanks for the help.
import java.io.*;
 
public class ConverttoLowercase{
 
  String convertAllUpperCaseToLowerCase(String original){
 
    StringBuffer sb = new StringBuffer(original);
    for (int i = 0; i < sb.length(); ++i){
      char c = sb.charAt(i);
      if (Character.isUpperCase(c)){
        c = Character.toLowerCase(c); // c = c - ('A' - 'a')
      }
      sb.setCharAt(i, c);
    }
    return new String(sb);
      }
  static String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ";
 
  boolean isAlpha(char c){
    if (alpha.indexOf(c) != -1){ 
      return true;
    }
    else{
      return false;
    }
  }
    public static void main(String[] args) throws Exception{ 
    String line;
    boolean gotSpace;
 
    ConverttoLowercase eho = new ConverttoLowercase();
    gotSpace = false;
    
   BufferedReader br = new BufferedReader(new FileReader("input.txt"));
    PrintWriter out = new PrintWriter(new BufferedWriter (new FileWriter("out.txt")));
    while ((line = br.readLine()) != null){
      if (line.length() < 1){ 
        break; 
                }
      line = eho.convertAllUpperCaseToLowerCase(line);
      for (int i = 0; i < line.length(); ++i){
        char c = line.charAt(i);
        if (eho.isAlpha(c)){
          gotSpace = true;
          out.print(line);
          out.close();
        }
        else if (c == ' ' || c == '\t'){ 
          if (gotSpace){
            continue; 
          }
          else{
            }
        }
        out.print(line);
          out.close();
      } 
 
 
      
    } 
    } 
} 

Open in new window

0
 
objectsCommented:
     public static void main(String[] args) throws Exception {
            String line;
            boolean gotSpace;

            ConverttoLowercase eho = new ConverttoLowercase();
            gotSpace = false;

            BufferedReader br = new BufferedReader(new FileReader("input.txt"));
            PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
                        "out.txt")));
            while ((line = br.readLine()) != null) {
                  if (line.length() < 1) {
                        break;
                  }
                  line = eho.convertAllUpperCaseToLowerCase(line);
                  for (int i = 0; i < line.length(); ++i) {
                        char c = line.charAt(i);
                        if (eho.isAlpha(c)) {
                              gotSpace = true;
                              out.print(line);
                        } else if (c == ' ' || c == '\t') {
                              if (gotSpace) {
                                    continue;
                              } else {
                              }
                        }
                        out.print(line);
                  }

            }
            out.close();
      }

0
 
navyretiredAuthor Commented:
Now it is doing both lines, however now it is repeating. Sorry man I did not mean for this to be as big of a pain as it is. Again thanks for the help.

Input file
JAMES Antonakos 12345
This IS a TesT

Output file
james antonakos 12345 james antonakos 12345 james antonakos 12345 james antonakos 12345>>>>>
this is a testthis is a testthis is a testthis is a testthis is a testthis is a testthis is a testthis is a test >>>>
0
 
navyretiredAuthor Commented:
This is what I have
import java.io.*;
 
public class ConverttoLowercase{
 
  String convertAllUpperCaseToLowerCase(String original){
 
    StringBuffer sb = new StringBuffer(original);
    for (int i = 0; i < sb.length(); ++i){
      char c = sb.charAt(i);
      if (Character.isUpperCase(c)){
        c = Character.toLowerCase(c); // c = c - ('A' - 'a')
      }
      sb.setCharAt(i, c);
    }
    return new String(sb);
      }
  static String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ";
 
  boolean isAlpha(char c){
    if (alpha.indexOf(c) != -1){ 
      return true;
    }
    else{
      return false;
    }
  }
    public static void main(String[] args) throws Exception {
            String line;
            boolean gotSpace;
 
            ConverttoLowercase eho = new ConverttoLowercase();
            gotSpace = false;
 
            BufferedReader br = new BufferedReader(new FileReader("input.txt"));
            PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
                        "out.txt")));
            while ((line = br.readLine()) != null) {
                  if (line.length() < 1) {
                        break;
                  }
                  line = eho.convertAllUpperCaseToLowerCase(line);
                  for (int i = 0; i < line.length(); ++i) {
                        char c = line.charAt(i);
                        if (eho.isAlpha(c)) {
                              gotSpace = true;
                              out.print(line);
                        } else if (c == ' ' || c == '\t') {
                              if (gotSpace) {
                                    continue;
                              } else {
                              }
                        }
                        out.print(line);
                  }
 
            }
            out.close();
      }
 
}

Open in new window

0
 
objectsCommented:
thats a problem in your logic, you output the whole line each time you check a char
0
 
BattelleITSCommented:
Hi,
I think there are two things that might help you: 1) use a BufferedWriter to write out full lines; and 2) use String.toLowerCase() instead of trying to lowercase everything yourself. With those two changes, the code gets very clear. I hope this helps!
import java.io.*;
 
public class ConverttoLowercase{
 
  String convertAllUpperCaseToLowerCase(String original){
    return original.toLowerCase();
  }
 
  public static void main(String[] args) throws Exception{
    String line;
    boolean gotSpace;
 
    ConverttoLowercase eho = new ConverttoLowercase();
 
    gotSpace = false;
    System.out.print("");
    BufferedReader br = new BufferedReader(new FileReader("input.txt"));
    BufferedWriter writer = new BufferedWriter( new FileWriter( "out.txt" ) );
 
    while ((line = br.readLine()) != null){
      line = eho.convertAllUpperCaseToLowerCase(line);
      writer.write( line );
      writer.newLine();
    }
    writer.close();
    br.close();
  }
}

Open in new window

0
 
nkiggsCommented:
Include out.newLine(); to write a new line after every line read from in.
	BufferedWriter out  = new BufferedWriter(new FileWriter("out.txt"));
	BufferedReader in  = null;
	try {
	        in = new BufferedReader(new FileReader("input.txt"));
	        String str;	        
	        while ((str = in.readLine()) != null) {
				out.write(line.toLowerCase());
				out.newLine();
			}
	}
	catch (IOException ez) {
		ez.printStackTrace();
	}
	in.close();	
	out.close();

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now