user3476500 user3476500 - 18 days ago 5
Java Question

Changing characters in a string (Path) Java

Hello i'm working on program in java, i have a string (a path) in which i want to change the "\" with the "/".

This my program, but i keep having the same path, no changes.

package Test;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Filechooser {

public static void main(String[] args) {

JFileChooser fc=new JFileChooser();
fc.setApproveButtonText("Open");
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
fc.addChoosableFileFilter(new FileNameExtensionFilter("text Documents", "txt"));
fc.setAcceptAllFileFilterUsed(true);
int returnVal=fc.showSaveDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
String path= fc.getSelectedFile().getAbsolutePath();
System.out.println(path);
String temp = path.replaceAll("\"", "/");
System.out.println(temp);
}

}

Answer

Try using:

String temp = path.replace("\\", "/");

replaceAll(str, str) takes a regex, not a simple char/string, so you can't use replaceAll like that.

\\ means \ in Java, while \" means ", so be careful with that :)