navyretired
asked on
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{
}
}
}
}
}
}
> PrintWriter out = new PrintWriter(new BufferedWriter (new FileWriter("out.txt")));
move that *outside* your loop
ie. before the loop starts
move that *outside* your loop
ie. before the loop starts
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.convertAllUpperCaseToL owerCase(l ine);
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{
}
}
}
PrintWriter out = new PrintWriter(new BufferedWriter (new FileWriter("out.txt")));
while ((line = br.readLine()) != null){
if (line.length() < 1){
break;
}
line = eho.convertAllUpperCaseToL
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{
}
}
}
ASKER
Now it ony does the first line
> out.close();
sorry, and move the close() to after everything is done
sorry, and move the close() to after everything is done
ASKER
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();
}
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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 >>>>
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 >>>>
ASKER
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();
}
}
thats a problem in your logic, you output the whole line each time you check a char
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!
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();
}
}
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();
ASKER