Use toUpperCase() or toLowerCase() to standardise your string before testing it.
Use toUpperCase() or toLowerCase() to standardise your string before testing it.
One option is to convert both of them to either lowercase or uppercase:
"Session".toLowerCase().startsWith("sEsSi".toLowerCase());
This is wrong. See: https://stackoverflow.com/a/15518878/14731
Another option is to use String#regionMatches() method, which takes a boolean argument stating whether to do case-sensitive matching or not. You can use it like this:
String haystack = "Session";
String needle = "sEsSi";
System.out.println(haystack.regionMatches(true, 0, needle, 0, needle.length())); // true
It checks whether the region of needle from index 0 till length 5 is present in haystack starting from index 0 till length 5 or not. The first argument is true, means it will do case-insensitive matching.
And if only you are a big fan of Regex, you can do something like this:
System.out.println(haystack.matches("(?i)" + Pattern.quote(needle) + ".*"));
(?i) embedded flag is for ignore case matching.
This smells like homework … … … And it stinks of the prof making you use startsWith() and endsWith() instead of checking for “topright” and “topleft” directly. Good prof!
OK – THINK about what you need to do:
-
Accept user input
-
Parse user input, ignoring case sensitivity
- Check for “help”
- Check for “stop”
- Check if it starts with “top”
- Check if it ends with “left”
- Check if it ends with “right”
- Check if the first 3 characters are “bot”
- Check if it ends with “left”
- Check if it ends with “right”
-
User input may be mixed case, so we need to ignore case
Here’s the tools you have:
-
You know how to check equality with case insensitivity: orInput.equalsIgnoreCase(“stop”)
-
You know how to get the beginning and ending parts of String objects: orInput.startsWith() and orInput.endsWith()
-
You should know how to convert a String to all Upper or all Lowercase: orInput.toUpperCase() and orInput.toLowerCase();
-
You should understand that equalsIgnoreCase() compares two String objects and that startsWith() and endsWith() return Boolean values - so they can’t be used to compare against a String. You have to find a way to make that work if you MUST use these methods
-
And you should understand how Object methods are “additive” within context: You can take a String, apply a method that returns an Integer, apply a second method to the resulting Integer, etc. This allows you to do things like: String X = orInput.concat(rawInput).substring(2,orInput.length()) - so X is a String that is built by taking orInput, adding rawInput, starting at the 3rd character, and taking as many characters as the length of orInput. In your example, if the user typed “topright”, the value of X would be “prightto”. Useless to you as is, but it shows you how you can perform method on method
You have everything here you need to make this work.
I’d want to provide the user some discretion in how they type instructions, such as top right, top-right, TOP RIGHT, upper left, and so on. To that aim, at the last if (, check whether cutInput begins with “top” or “up” AND check if cutInput concludes with “left” or “right,” all while being case-insensitive. Is this even possible?
public static void rightSel(Scanner scanner,char t)
{
/*if (!stopping)*/System.out.print(": ");
if (scanner.hasNextLine())
{
String orInput = scanner.nextLine;
if (orInput.equalsIgnoreCase("help")
{
System.out.println("The following commands are available:");
System.out.println(" 'help' : displays this menu");
System.out.println(" 'stop' : stops the program");
System.out.println(" 'topleft' : makes right triangle alligned left and to the top");
System.out.println(" 'topright' : makes right triangle alligned right and to the top");
System.out.println(" 'botright' : makes right triangle alligned right and to the bottom");
System.out.println(" 'botleft' : makes right triangle alligned left and to the bottom");
System.out.println("To continue, enter one of the above commands.");
}//help menu
else if (orInput.equalsIgnoreCase("stop")
{
System.out.println("Stopping the program...");
stopping = true;
}//stop command
else
{
String rawInput = orInput;
String cutInput = rawInput.trim();
if (
The final aim is for the user to be able to select one of four triangle orientations from a single line of input just like the example here . This was the best solution I could think of, although I’m still new to programming in general and may be overcomplicating things. If I am, and it turns out that there is a better approach, please let me know.