Ich habe das Problem, dass, wenn ich eine .txt-Datei in einen "konvertiere", alle Leerzeichen verschwinden.
Folgender Code:
Wo ist der Fehler?
Folgender Code:
Java:
public static String enterFileOrString(JFrame f, String message) throws FileNotFoundException
{
String[] stringOptions = {"entered String", ".txt-file"};
String s = "";
int i = JOptionPane.showOptionDialog(f, "Which kind of string do you want to use?", "select kind of string", 0, 1, null, stringOptions, null);
if(i == 0)
s = enterString(f, message);
else
{
message = message.replace("string", ".txt-file's path");
String path = JOptionPane.showInputDialog(f, message, "enter path", 1).replace("\\", "/");
Scanner scanner = new Scanner(new File(path));
while(scanner.hasNextLine())
s += scanner.next();
}
return s;
}
Java:
public StringSearcher() throws FileNotFoundException
{
String[] searchOptions = {"String", "regular expression"};
int searchOption = Utils.selectTool(f, "Select your search-method", "select search method", searchOptions);
switch(searchOption)
{
case 0:
int position = 0;
String output = "";
boolean replace;
String mainString = Utils.enterFileOrString(f, "Enter the string you want to search in!");
String subString = Utils.enterString(f, "Enter the string you want to search!");
for (int i = 0; (position = mainString.indexOf(subString, position)) > -1; i++ )
{
position += subString.length();
output += "[" + position + "-" + (position + subString.length()) + "]\n";
}
JOptionPane.showMessageDialog(f, output, "string searched", 1);
replace = Utils.yesNo(f, "Do you want to replace the sub-String?", "replace?");
if(replace)
{
position = 0;
String replacement = Utils.enterString(f, "Enter the String for the replacement!");
mainString = mainString.replace(subString, replacement);
JOptionPane.showMessageDialog(f, mainString, "String with replaced subString", 1, null);
}
break;
case 1:
mainString = Utils.enterString(f, "Enter the String you want to search in!");
output = "";
String regularExpression = Utils.selectRegularExpression();
List<MatchResult> results = new ArrayList<MatchResult>();
Matcher m = Pattern.compile(regularExpression).matcher(mainString);
while(m.find())
{
results.add(m.toMatchResult());
}
for ( MatchResult r : results)
{
output += "[" + r.start() + "-" + r.end() + "]\n";
}
JOptionPane.showMessageDialog(f, output, "regular expression searched", 1);
replace = Utils.yesNo(f, "Do you want to replace the regular expression?", "replace?");
if(replace)
{
StringBuffer outputRegex = new StringBuffer();
String replacement = Utils.enterString(f, "Enter the String for the replacement!");
m = Pattern.compile(regularExpression).matcher(mainString);
while(m.find())
{
m.appendReplacement(outputRegex, replacement);
}
m.appendTail(outputRegex);
JOptionPane.showMessageDialog(f, outputRegex, "String with replaced regular expression", 1, null);
}
break;
}
Main.starter();
}