I am a trying to create a program that takes input from the user (assuming 3 words) and outputs the first word capitalised, the second in lower case and the third word only the first two characters. I don't understand why the third one is printed whole plus the first two characters and not just the first 2 characters. Thanks so much! So far I have this:
import java.util.Scanner;
public class Words {
public static void main (String[] args){
Scanner keyboard = new Scanner(System.in);
String line = keyboard.nextLine();
String word1 = line.substring(0, line.indexOf(" ")).toUpperCase();
String word2 = line.substring(line.indexOf(" ") +1).toLowerCase();
String word3 = line.substring(line.lastIndexOf(" ")+1).substring(0,
,2);
System.out.println(word1 + " " + word2 + " " + word3);
}
}
Output:
JAVA is fun fu