A few things to note here. First of all, you are never actually creating an object to accept input from the console. In Java, this task is often carried out using a Scanner
.
Scanner sc = new Scanner(System.in);
Next, typical Java code conventions (taking Google's guide for example) dictate that variable names should be in camelCase style, and should not contain underscore characters. A better name for string_1
would therefore be input
, or something similar.
System.out.print("Please enter a string: ");
String input = sc.nextLine(); // input from console
System.out.println("Entered string: " + input);
Finally, in your for-loop
, you want to increment the number that is being displayed to the user for the character location as the loop progresses. This is done by concatenating a String
that includes the loop variable i
. Since the loop is zero-indexed, and presumably you want the output to be interpreted by humans, it would be useful to add one to the index when displaying it.
for (int i = 0; i < input.length(); i++ ) {
// build a string using `i + 1` to display character index
System.out.println ("Character #" + (i + 1) + ": " + input.charAt(i));
}
It's also worth noting, that declaring the loop variable int i
within the loop definition is preferable, as it limits the scope of the variable (See: Declaring variables inside or outside of a loop).