Java - String Class, Scanner Class -
how read sentence (string) line of input, , print whether represents declarative sentence (i.e., ending in period), interrogatory (ending in question mark), or exclamation (ending in exclamation point) or not sentence (anything else)?
import java.util.*; public class stringdemo { public static void main(string[] args){ scanner console = new scanner(system.in); // enter string: system.out.println("enter string: "); string dog = console.nextline(); dog = dog.touppercase(); system.out.println(dog + " has " + dog.length() + " letters , starts " + dog.substring(0, 1)); // enter string: system.out.println("enter string: "); string cat = console.nextline(); cat = cat.touppercase(); system.out.println(cat + " has " + cat.length() + " letters , starts " + cat.substring(0, 1)); // check contents here if(dog.equals(cat)) system.out.println("input strings matching "); else system.out.println("input strings not matching "); } }
need use .equals() method check content equality below.
import java.util.*; public class stringdemo { public static void main(string[] args) { scanner console = new scanner(system.in); // enter string: system.out.println("enter string: "); string dog = console.nextline(); dog = dog.touppercase(); system.out.println(dog + " has " + dog.length() + " letters , starts " + dog.substring(0, 1)); // enter string: system.out.println("enter string: "); string cat = console.nextline(); cat = cat.touppercase(); system.out.println(cat + " has " + cat.length() + " letters , starts " + cat.substring(0, 1)); // check contents here if(dog.endswith("?")) system.out.println("input strings ends in question mark "); } }
Comments
Post a Comment