I have here two problems needed to be solved. First off: I have here my program, wherein I want that whatever the user inputs will be saved on a string, and to be placed in an array.
my codes:
String words;
String replaced_words;
Scanner s = new Scanner (System.in);
System.out.println("Enter a line of words basing on an event, verse, place or a name of a person.");
words = s.nextLine();
System.out.println("Word accepted!");
//using char array method
char [] c = words.toCharArray();
Now, I used the replace method for the string such as this:
String replace = words.replace(' ', '-');
But the problem is, this occurs as an error. By replacing the ‘ ‘ character into words such as:
String replace = words.replace(words, '-')
although I want to limit the length of the hyphens depending on the numbers of characters on the words.
The output of this program should be the following:
· Create a copy of this string to an array where each letter is replaced with a hyphen (-) and each space is replaced with an underscore (_). Symbols and numbers will remain shown.
The problems I’m getting is that it doesn’t replace them with a hyphen and underscore. It just displays the inputted text I did just typed via scanner.
To achieve your goal you can use:
if you want spaces between the underscores (to distinguish them from each other) you can use