So I'm looking for a regex that will match anything that ISN'T [a-zA-Z] (s no numbers, no specials, no underscores, no nothing but English letters), at any point in a string, regardless of whether there's any alpha character in said string.
Here's my (relevant, I omitted output bits) code, which, frankly, might have its own problems, but getting the regex right (or at least confirmed to match a character that isn't a letter) is the problem that needs to be solved so I can find any other problems.
I tried The Google and StackExchange, but didn't find anything that actually works.
It's supposed to take a variable-length input, then find the strings that are a certain length out of that input.
Yes, I suppose there are better ways to implement this than Scanner...Hmh, in fact, I should just (learn and) do an EasyReader or whatever the heck it is...
Here's the code anyway!
ArrayList<String> inputList = new ArrayList(0);
Scanner console = new Scanner(System.in);
StringBuilder input = new StringBuilder(0);
boolean isInputValid = true;
while(isInputValid){
System.out.println("Please input a word. Any characters that are not capital or lowercase letters will end the input period.");
input.append(console.next());
//If it doesn't match letters? Something about negative lookaheads...
if(!input.toString().matches("(?![a-zA-Z]$).*")){
//if size is less than one...
if(inputList.size() < 1){
System.out.println("Please input a word before terminating the input period.");
}
//This SHOULD trigger on
else{
break;
}
}
if(input.toString().matches("(?![a-zA-Z]$).*")){
inputList.add(input.toString());
input.setLength(0);
System.out.println("Word added.");
}
}
E: Ooooor it could be that I forgot to negate that regexp? Trying that.
E2:Nope, now it's just erroring all the time, which makes that a logic flaw. Gleh.
E3:Oh, I need that in the other conditional, too.
E4:I just cannot figure this out for the life of me. Uuugh.
E5: REGULAR EXPRESSIONS AAAAAAAAA
E6: What the flying skeletal carp is WRONG with this thing?!
This is broken too.
Length is a field.
while(true){
try{
length = console.nextInt();
if(length < 1){
throw new IllegalArgumentException();
}
break;
}
catch(IllegalArgumentException | InputMismatchException ex){
System.out.println("Please input a number greater than zero and less than 2^31.");
}
}
Not to mention the bit where my code is apparently arbitrarily deciding WHEN to switch to the bit where it does this (following from the prior), it will immediately go into an infinite loop with the catch block.
I'm half-tempted to just throw the thing out and start from scratch.
Fixed that issue at least. Though not the seemingly arbitrary switching.
E(IDK): What. The. FLYING SKELETAL CARP.
Please input a word. Any characters that are not capital or lowercase letters will end the input period.
r
Please input a word before terminating the input period.
...
Please input a word. Any characters that are not capital or lowercase letters will end the input period.
r
Word added.
...?!
Please input a word. Any characters that are not capital or lowercase letters will end the input period.
r
r
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
* TheBiggerFish cancels Write Code: Too Insane.
E:Updated code.
E: Fixed one thing. Second code block is now properly try-catching instead of looping through the catch block indefinitely.
boolean didGetPositiveInteger = false;
while(!didGetPositiveInteger){
try{
System.out.println("Please input the integer word length you would like to check.");
length = Integer.parseInt(console.next());
if(length < 1){
throw new IllegalArgumentException();
}
else didGetPositiveInteger = true;
}
catch(IllegalArgumentException ex){
System.out.println("Please input a number greater than zero and less than 2^31.");
}
}
The input is still messed up though.
It will, no matter what I put in, run
1)The inner if() statement, AND the bit that adds the word. Hopefully fixed by actually applying the "isn't a terminator" regex.
2)A normal loop through.
3)The portion where it asks for an integer length to check against.
Is there something in the logic in the first code block (at the top of this post) that would make it do that?
E(null): Yeah, I really am stumped.
Here's one last dump of the thing.
import java.util.ArrayList;
import java.util.Scanner;
public class StringLengthChecker {
static int length;
public static void main(String[] args) {
ArrayList<String> inputList = new ArrayList(0);
Scanner console = new Scanner(System.in);
StringBuilder input = new StringBuilder(0);
boolean isInputValid = true;
while(isInputValid){
System.out.println("Please input a word. Any characters that are not capital or lowercase letters will end the input period.");
input.append(console.next());
//If it doesn't match letters? Something about negative lookaheads...
if(input.toString().matches("(?![a-zA-Z]$).*")){
//if size is less than one and it's not a valid word.
if(inputList.size() < 1 && input.toString().matches("(?![a-zA-Z]$).*")){
System.out.println("Please input a word before terminating the input period.");
}
//This SHOULD trigger if it's false.
else{
input.setLength(0);
break;
}
}
if(input.toString().matches("(?![a-zA-Z]$).*")){
inputList.add(input.toString());
input.setLength(0);
System.out.println("Word added.");
}
}
boolean didGetPositiveInteger = false;
while(!didGetPositiveInteger){
try{
System.out.println("Please input the integer word length you would like to check.");
length = Integer.parseInt(console.next());
if(length < 1){
throw new IllegalArgumentException();
}
else didGetPositiveInteger = true;
}
catch(IllegalArgumentException ex){
System.out.println("Please input a number greater than zero and less than 2^31.");
}
}
System.out.println("Out of the strings in this input");
for(String pull : inputList){
System.out.println(pull);
}
System.out.println("These strings were " + length + " character(s) long.");
for(String output : sortInput(inputList)){
System.out.println(output);
}
}
public static ArrayList<String> sortInput(ArrayList<String> input){
ArrayList<String> output = new ArrayList();
for(String strToProcess : input){
if(strToProcess.length() == length){
output.add(strToProcess);
}
}
return output;
}
}
I have officially thrown in the towel for now, I think.