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

Convert To Lowercase

I need to read a file and convert all uppercase to lowercase. This is for a class that I have to turn in tomorrow so any help you can give will be appriciated. I have tried a number of things but I cannot get it to work. Thanks for any help you can offer.

I/O program that reads an input file, converts all the uppercase characters into lowercase without changing any other characters, and writes the characters to an output file.

I worked out the below code however I just found out it needs to read an input file and create an output file not the kbi as I currently 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;
    System.out.print("Please input string to convert\n\n");
    System.out.print("");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    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;
          	
          System.out.print(c);
        }
        else if (c == ' ' || c == '\t'){ 
          if (gotSpace){
            continue; 
          }
          else{
            }
        } 
      } 
      	System.out.print('\n');
    } 
    } 
}

Open in new window

0
navyretired
Asked:
navyretired
  • 4
  • 3
1 Solution
 
objectsCommented:
>     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

replace that with

    BufferedReader br = new BufferedReader(new FileReader("input.txt"));


Also just letting you know there is a toLowercase() method in the String class

0
 
navyretiredAuthor Commented:
Thanks for the help. How do I select the file to convert?

I am getting
Exception in thread "main" java.io.FileNotFoundException: input.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:106)
    at java.io.FileInputStream.<init>(FileInputStream.java:66)
    at java.io.FileReader.<init>(FileReader.java:41)
    at ConverttoLowercase.main(ConverttoLowercase.java:35)
0
 
objectsCommented:
you need to specify the path to where it is
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
navyretiredAuthor Commented:
Never Mind I got it for the input file.
0
 
navyretiredAuthor Commented:
Is there something I can do to save it to an output file?
0
 
objectsCommented:
create an output writer

PrintWriter out = new PrintWriter(new FileWriter("out.txt"));

and write output there instead of System.out

0
 
navyretiredAuthor Commented:
Great thanks a lot for your help.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

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