• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • 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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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