I have written a code which transforms all the new line characters to comma. If a character is '\n'
it will change it to ','
. But I am having problems entering my sample data. I think Netbeans takes spaces as new line characters. Pressing enter at the run section inputs the data I have written instead of creating a new line. What should I do ?
I am also open to suggestions about my code.
This is the code:
package newlinetocomma;
import java.util.Scanner;
/**
*
* @author sametsahin
*/
public class NewLineToComma {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a text: ");
String textWithNewLines = scanner.next();
char[] textWithNewLinesAsArray = textWithNewLines.toCharArray();
for (int i = 0; i < textWithNewLinesAsArray.length; i++) {
if (textWithNewLinesAsArray[i] == '\n') {
textWithNewLinesAsArray[i] = ',';
}
}
for (int i = 0; i < textWithNewLinesAsArray.length; i++) {
System.out.print(textWithNewLinesAsArray[i]);
}
}
}