Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with weird IO problem  RSS feed

 
Darren Gordon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am making a program that takes the first sentence from a text file, takes out select letters and prints the new sentence to a file. For some reason, my new sentence isn't concatenating without the letters.

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

public class Problem1
{
public static void main(String[] args) throws IOException
{
File inFile = new File("Problem1Input.txt");
Scanner sc = new Scanner(inFile);

File outFile = new File("Problem1Output.txt");
PrintWriter out = new PrintWriter(outFile);

String[] badChars = new String[] {"b", "f", "j", "k", "q", "u", "v", "x", "y", "z"};
String sentence = sc.nextLine();


for(int i = 0; i < sentence.length(); i++)
{
for(int j = 0; j < badChars.length; j++)
{
if(badChars[j].equals(sentence.substring(i, i + 1)))
{
sentence = sentence.substring(0, i) + sentence.substring(i + 1);
}
}
}

out.print(sentence);
out.close();
}
}


Ideas anyone? Thanks.
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darren,

What results are you getting that makes you think it's not working? The loops you're using are not the most efficient manner, but seem to work. I ran your program with this content:


I got the following as output:


Which does indeed conform to your list of "bad letters" being removed.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!