Reputation: 1
The question seems confusing. The program does this: It asks the user for input in char form and then stores it in 2 variables. There are 2 Strings which I've already created. One contains the letters a-g and the other for the letters h-o. These are basically 2 routes which a user can go, but the user can't put for example "a" as start and "h" as destination since they are from a different String. He/She has to have the start and destination in the same String. If the start is and destination aren't in the same String, there will be an error.
This method I have looks as follows:
private void neueRoute() {
Scanner in = new Scanner(System.in);
String bereich1 = "aAbBcCdDeEfFgG";
String bereich2 = "hHiIjJkKlLmMnNoO";
char start = ' ';
char ziel = ' ';
int a = 0;
int b = 0;
int c = 0;
int d = 0;
System.out.println("Themenbereich 1: Bahn A - G: ");
System.out.println("\nA\nB\nC\nD\nE\nF\nG\n");
System.out.println("Themenbereich 2: Bahn H - O: ");
System.out.println("\nH\nI\nJ\nK\nL\nM\nN\nO");
while((a == 0 && b == 0) || (c == 0 && d ==0)){
System.out.println("Put in a starting point please: ");
start = in.next().charAt(0);
while(start != 'a' && start != 'A' && start != 'b' && start != 'B' &&
start != 'c' && start != 'C' && start != 'd' && start != 'D' &&
start != 'e' && start != 'E' && start != 'f' && start != 'F' &&
start != 'g' && start != 'G' && start != 'h' && start != 'H' &&
start != 'i' && start != 'I' && start != 'j' && start != 'J' &&
start != 'k' && start != 'K' && start != 'l' && start != 'L' &&
start != 'm' && start != 'M' && start != 'n' && start != 'N' &&
start != 'o' && start != 'O') {
System.out.println("Put in a correct starting point please: ");
start = in.next().charAt(0);
}
System.out.println("Put in a destination point please: ");
ziel = in.next().charAt(0);
while(ziel != 'a' && ziel != 'A' && ziel != 'b' && ziel != 'B' &&
ziel != 'c' && ziel != 'C' && ziel != 'd' && ziel != 'D' &&
ziel != 'e' && ziel != 'E' && ziel != 'f' && ziel != 'F' &&
ziel != 'g' && ziel != 'G' && ziel != 'h' && ziel != 'H' &&
ziel != 'i' && ziel != 'I' && ziel != 'j' && ziel != 'J' &&
ziel != 'k' && ziel != 'K' && ziel != 'l' && ziel != 'L' &&
ziel != 'm' && ziel != 'M' && ziel != 'n' && ziel != 'N' &&
ziel != 'o' && ziel != 'O') {
System.out.println("Put in a correct destination point please: ");
ziel = in.next().charAt(0);
}
a = bereich1.indexOf(start);
b = bereich2.indexOf(ziel);
c = bereich2.indexOf(start);
d = bereich1.indexOf(ziel);
}
}
I tried using .indexOf() today, which I have here with this while loop:
while((a == 0 && b == 0) || (c == 0 && d ==0)){
And these variables:
a = bereich1.indexOf(start);
b = bereich2.indexOf(ziel);
c = bereich2.indexOf(start);
d = bereich1.indexOf(ziel);
I just can't get it to work and it's been working my mind for a couple of days. Any help or pinpointing in the right direction is greatly appreciated. Please let me know if there is anything insufficient given by me or if you need any more information.
Thank you.
Upvotes: 0
Views: 81
Reputation: 416
Use regex! Don't iterate on a string to identify patterns.
Example:
Pattern pattern = Pattern.compile("<your string/char>");
boolean containsInFrisrt = pattern.matcher(str1).matches();
boolean containsInSeconds = pattern.matcher(str2).matches();
Upvotes: 0
Reputation: 41223
To find out whether a single character is in a range, you don't need a regular expression, nor a long list of checks. You can compare char
s as if they were numbers (which they are, really).
char start = input.charAt(0);
if( start >= 'a' && start <= 'o' ) {
// do stuff
}
To make it case-insensitive, convert it to a case before comparing:
char start = Character.toLowerCase(input.charAt(0));
I suggest spending a couple of hours reading through the Javadocs for general-purpose classes like String
, Character
, Math
and so on, in the spirit of spotting methods that "might come in handy", and getting a general feel for the sort of methods that you can expect to find if you look.
Upvotes: 0
Reputation: 3279
You're using bereich1.indexOf(start) == 0
, which is equivalent to bereich1.startsWith(""+start)
, i.e. you're checking not for whether a string contains another string, but for whether it starts with another string.
What you actually want to use is bereich1.contains(""+start)
, so your while loop would look like this:
while (bereich1.contains(""+start) && bereich2.contains(""+ziel)
|| bereich2.contains(""+start) && bereich1.contains(""+ziel)) {
PS: for checking whether a character is between 'a' and 'o', you can use if ('a' <= ziel && ziel <= 'o' || 'A' <= ziel && ziel <= 'O')
instead of your huge if-statement.
Upvotes: 0
Reputation: 1087
You can use the contains()
function to check if the either of the strings contain the input. From there you can determine which string the user started with and then check against that one to make sure they enter a destination in the same string.
private void neueRoute() {
Scanner in = new Scanner(System.in);
String bereich1 = "aAbBcCdDeEfFgG";
String bereich2 = "hHiIjJkKlLmMnNoO";
char start = ' ';
char ziel = ' ';
System.out.println("Themenbereich 1: Bahn A - G: ");
System.out.println("\nA\nB\nC\nD\nE\nF\nG\n");
System.out.println("Themenbereich 2: Bahn H - O: ");
System.out.println("\nH\nI\nJ\nK\nL\nM\nN\nO");
System.out.println("Put in a starting point please: ");
start = in.next().charAt(0);
// make sure the starting character exists in either string
while(!bereich1.contains(start) && !bereich2.contains(start)) {
System.out.println("Put in a correct starting point please: ");
start = in.next().charAt(0);
}
// determine which string the starting character exists in
String chosen = bereich1.contains(start) ? bereich1 : bereich2;
System.out.println("Put in a destination point please: ");
ziel = in.next().charAt(0);
// make sure the ending character exists in the chosen string
while(!chosen.contains(ziel)) {
System.out.println("Put in a correct destination point please: ");
ziel = in.next().charAt(0);
}
}
Upvotes: 0
Reputation: 54148
You can replace :
while(start != 'a' && start != 'A' && start != 'b' && start != 'B' &&
start != 'c' && start != 'C' && start != 'd' && start != 'D' &&
start != 'e' && start != 'E' && start != 'f' && start != 'F' &&
start != 'g' && start != 'G' && start != 'h' && start != 'H' &&
start != 'i' && start != 'I' && start != 'j' && start != 'J' &&
start != 'k' && start != 'K' && start != 'l' && start != 'L' &&
start != 'm' && start != 'M' && start != 'n' && start != 'N' &&
start != 'o' && start != 'O') {
System.out.println("Put in a correct starting point please: ");
start = in.next().charAt(0);
}
By :
//matches a letter between a and o in lower case or not
while (!start.matches("\\b[a-oA-O]\\b")) {
System.out.println("Put in a correct starting point please: ");
start = in.next().charAt(0);
}
Upvotes: 1