How to convert contains to equals.ignorecase? - java

I am reading a text file into my program, and having the user search for a string. How can I make this so its case-insensitive? Here is a snippet of my code:
while (str1.hasNextLine())
{
String line = str1.nextLine();
line = line.replace(";", " ");
if(line.contains(Val))
{
System.out.println(line);
}
}
Val is the string variable. It is the string that the user entered, and the string that, if found in the text file, will print out on the line. But I need it to be case-insensitive. For some reason when I use equals.IgnoreCase it doesn't work.

In this scenario, make everything a unified case, and compare.
if (line.toLowerCase().contains(Val.toLowerCase())) {
// logic
}
There are limitations on what contains can do. It only checks CharSequences and does so in a case-sensitive fashion. By introducing a common case, this eliminates the case sensitivity issue.

Related

Extra java input validation for strings

I want to make this so that short inputs can still be detected, such as "Londo" and "Lon", but want to keep it small and use it without basically copying and pasting the code, any tips? thank you.
if (Menu.answer1.equals("London"))
{
if (location.equals("London")) {
System.out.print(location + " ");
System.out.print(date + " ");
System.out.print(degrees + "C ");
System.out.print(wind + "MPH ");
System.out.print(winddirection + " ");
System.out.print(weather + " ");
System.out.println("");
}
You can use startsWith()
String city = "London";
if (city.startsWith("Lon")) {
// do something
}
Also if you need to check some substring, you can use contains method:
Menu.answer1 = "London";
Menu.answer1.contains("ondo"); // true
If you want to check against a fixed set of alternatives, you may use a list of valid inputs using contains:
List<String> londonNames = Arrays.asList("London", "Londo", "Lon");
if (londonNames.contains(Menu.answer1)) {
...
}
You can use (case-insensitive) regex to do the same, e.g.:
(?)Lon[a-z]{0,3} where
(?) = case insensitivity
Lon = Initial 3 characters
[a-z]{0,3} = any number of alphabets between 0 and 3
Here's an example:
String regex = "(?)Lon[a-z]{0,3}";
System.out.println("London".matches(regex));
System.out.println("Lond".matches(regex));
System.out.println("Lon".matches(regex));
If the underlying problem is that the user can enter one of several names, and you want to allow abbreviations, then a fairly standard approach is to have a table of acceptable names.
Given the user input, loop through the table testing "does the table entry start with the string typed by the user?" (like one of the previous answers here). If yes, then you have a potential match.
Keep looking. If you get a second match then the user input was ambiguous and should be rejected.
As a bonus, you can collect all names that match, and then use them in an error message. ("Pick one of London, Lonfoo, Lonbar").
This approach has the advantage (compared to a long chain of if-then-else logic) of not requiring you to write more code when all you want to do is have more data.
It automatically allows the shortest unique abbreviation, and will adjust when a once-unique abbreviation is no longer unique because of newly-added names.

Extracting Substrings from a List in Java

If I have a parent string (let's call it output) that contains a list of variable assignments like so ...
status.availability-state available
status.enabled-state enabled
status.status-reason The pool is available
And I want to extract the values of each variable in that list given the variable names, ie the substring after the space following status.availability-state, status.enabled-state, and status.status-reason, such that I end up with three different variable assignments making each of the following String comparisons true ...
String availability = output.substring(TODO);
String enabled = output.substring(TODO);
String reason = output.substring(TODO);
availability.equals("available");
enabled.equals("enabled");
reason.equals("The pool is available");
What is the simplest way to do this? Should I even use substring for this?
This is a little tricky because you need to assign the value to a specific variable - you can't just have a map of keys to variables in Java.
I would consider doing this with a switch:
for (String line : output.split('\n')) {
String[] frags = line.split(' ', 2); // Split the line in 2 at the space.
switch (frags[0]) { // This is the "key" of the variable.
case "status.availability-state":
availability = frags[1]; // This assigns the "value" to the relevant variable.
break;
case "status.enabled-state":
enabled = frags[1];
break;
// ... etc
}
}
It's not very pretty, but you don't have too many options.
There seem to be two questions here -- how to parse the string, and how to assign to variables by name.
Tackle the string parsing one step at a time:
first write a program to read one line at a time and output each one in the body of a loop. String.split() or StringTokenizer are two options here.
next enhance this by writing a method to handle one line. The same tools are helpful here, to split on spaces.
You should now have a program that can print name: status.availability-state, value: available for each line of input.
Next, you're asking to programatically assign to variables based on the name of the parameter.
There is no legitimate way to look at a variable's name at runtime (OK, Java 8 reflection has ways, but it shouldn't be used without very good reason).
So, the best you can do is to use a switch or if statement:
switch(name) {
case status.availability-state:
availability = value;
break;
... etc.
}
However, whenever you use switch or if you should think about whether there's a better way.
Is there any reason you can't turn these variables into Map entries?
configMap.add(name,value);
Then to read it:
doSomethingWith(configMap.get("status.availability");
That's what maps are for. Use them.
This is a similar situation to the rookie mistake of using variables called person1, person2, person3... instead of using an array. Eventually they ask "How do I go from the number 25 to my variable person25?" -- and the answer is, you can't, but an array or list makes it easy. people[number] or people.get(number)
A valid alternative is to split the string by \n and add to a Map. Example:
String properties = "status.availability-state available\nstatus.enabled-state enabled\nstatus.status-reason The pool is available";
Map<String, String> map = Arrays.stream(properties.split("\n"))
.collect(Collectors.toMap(s -> s.split(" ")[0], s -> s.split(" ", 2)[1]));
System.out.println(map.get("status.status-reason"));
Should output The pool is available
This loop will match and extract the variables, and you can then assign them as you see fit:
Pattern regex = Pattern.compile("status\\.(.*?)-.*? ([a-z]+)");
Matcher matcher = regex.matcher(output);
while (matcher.find()) {
System.out.println(matcher.group(1) + "=" + matcher.group(2));
}
status\\. matches "status."
(.*?) matches any sequence of characters but isn't greedy, and captures them
-.* matches dash, any chars, space
([a-z]+) matches any string of lower-case letters, and captures them
Here's one way to do it:
Map<String, String> properties = getProperties(propertiesString);
availability = properties.get("availability-state");
enabled = properties.get("enabled-state");
reason = properties.get("status-reason");
// ...
public void getProperties(String input) {
Map<String, String> properties = new HashMap<>();
String[] lines = output.split("\n");
for (String line : lines) {
String[] parts = line.split(" ");
int keyStartIndex = parts[0].indexOf(".") + 1;
int spaceIndex = parts[1].indexOf(" ");
string key = parts[0].substring(keyStartIndex, spaceIndex);
properties.put(key, parts[1]);
}
return properties;
}
This seems to be a bit more straight-forward, in terms of the code that's setting these values, as each value is set to exactly the value from the map, rather than iterating over some list of strings and seeing if it contains a particular value and doing different things based on that.
This is designed with the primary use-case being that the string is created at runtime in memory. If the properties are created in an external file, this code would still work (after creating the desired String in memory), but it may be a better idea to use either a Properties file, or perhaps a Scanner.

Fastest way to parse txt file in Java

I have to parse a txt file for a tax calculator that has this form:
Name: Mary Jane
Age: 23
Status: Married
Receipts:
Id: 1
Place: Restaurant
Money Spent: 20
Id: 2
Place: Mall
Money Spent: 30
So, what i have done so far is:
public void read(File file) throws FileNotFoundException{
Scanner scanner = new Scanner(file);
String[] tokens = null;
while(scanner.hasNext()){
String line= scanner.nextLine();
tokens = line.split(":");
String lastToken = tokens[tokens.length - 1];
System.out.println(lastToken);
So, I want to access only the second column of this file (Mary Jane, 23, Married) to a class taxpayer(name, age, status) and the receipts' info to an Arraylist.
I thought of taking the last token and save it to an String array, but I can't do that because I can't save string to string array. Can someone help me? Thank you.
The fastest way, if your data is ASCII and you don't need charset conversion, is to use a BufferedInputStream and do all the parsing yourself -- find the line terminators, parse the numbers. Do NOT use a Reader, or create Strings, or create any objects per line, or use parseInt. Just use byte arrays and look at the bytes. It's a little messier, but pretend you're writing C code, and it will be faster.
Also give some thought to how compact the data structure you're creating is, and whether you can avoid creating an object per line there too by being clever.
Frankly, I think the "fastest" is a red herring. Unless you have millions of these files, it is unlikely that the speed of your code will be relevant.
And in fact, your basic approach to parsing (read line using Scanner, split line using String.split(...) seems pretty sound.
What you are missing is that the structure of your code needs to match the structure of the file. Here's a sketch of how I would do it.
If you are going to ignore the first field of each line, you need a method that:
reads a line, skipping empty lines
splits it, and
returns the second field.
If you are going to check that the first field contains the expected keyword, then modify the method to take a parameter, and check the field. (I'd recommend this version ...)
Then call the above method in the correct pattern; e.g.
call it 3 times to extract the name, age and marital status
call it 1 time to skip the "reciepts" line
use a while loop to call the method 3 times to read the 3 fields for each receipt.
First why do you need to invest time into the fastest possible solution? Is it because the input file is huge? I also do not understand how you want to store result of parsing? Consider new class with all fields you need to extract from file per person.
Few tips:
- Avoid unnecessary per-line memory allocations. line.split(":") in your code is example of this.
- Use buffered input.
- Minimize input/output operations.
If these are not enough for you try to read this article http://nadeausoftware.com/articles/2008/02/java_tip_how_read_files_quickly
Do you really need it to be as fast as possible? In situations like this, it's often fine to create a few objects and do a bit of garbage collection along the way in order to have more maintainable code.
I'd use two regular expressions myself (one for the taxpayer and another for the receipts loop).
My code would look something like:
public class ParsedFile {
private Taxpayer taxpayer;
private List<Receipt> receipts;
// getters and setters etc.
}
public class FileParser {
private static final Pattern TAXPAYER_PATTERN =
// this pattern includes capturing groups in brackets ()
Pattern.compile("Name: (.*?)\\s*Age: (.*?)\\s*Status: (.*?)\\s*Receipts:", Pattern.DOTALL);
public ParsedFile parse(File file) {
BufferedReader reader = new BufferedReader(new FileReader(file)));
String firstChunk = getNextChunk(reader);
Taxpayer taxpayer = parseTaxpayer(firstChunk);
List<Receipt> receipts = new ArrayList<Receipt>();
String chunk;
while ((chunk = getNextChunk(reader)) != null) {
receipts.add(parseReceipt(chunk));
}
return new ParsedFile(taxpayer, receipts);
}
private TaxPayer parseTaxPayer(String chunk) {
Matcher matcher = TAXPAYER_PATTERN.matcher(chunk);
if (!matcher.matches()) {
throw new Exception(chunk + " does not match " + TAXPAYER_PATTERN.pattern());
}
// this is where we use the capturing groups from the regular expression
return new TaxPayer(matcher.group(1), matcher.group(2), ...);
}
private Receipt parseReceipt(String chunk) {
// TODO implement
}
private String getNextChunk(BufferedReader reader) {
// keep reading lines until either a blank line or end of file
// return the chunk as a string
}
}

Java - How can I check for the next upcoming space in a string? (For IRC Bot)

Obviously, for an IRC bot, input is generated by a user typing a single string, usually with a command and a few arguments, each separated by a space. I am coding an IRC bot using Java and would like to parse arguments that might vary in character length and save them into multiple strings for use later. I would like to make a command that looks something like this:
bot.command argument1 argument2 argument3
and I want it to be so that if the message starts with bot.command, then user will store argument1, time will store argument2, and date will store argument3. The thing is, though, that argument1, argument2, and argument3 could vary in character length, so doing something like time = message.substring(33, message.length()) will stop reading at the end of the argument, but it won't always read the string where the argument3's text begins. I need to detect where the separator space is and start reading argument3 from one character after that. And I can't use If statements to determine what the arguments might be, because they could be anything. Here's a template, if the above paragraph isn't clear:
String message //= the IRC message and bot input
String user;
String time;
String date;
if (message.startsWith("bot.command")) {
user = message.substring(13, detect next space here and stop reading one character before);
time = message.substring(detect where previous space was and start one character after that, end before next space);
date = message.substring(detect where previous space was and start one character after that, message.length());
}
I hope that kind of illustrates what I'm trying to do. Thank you for your help!
I'd recommend using String.split, which breaks up a string into an array using a delimiter regular expression. In your case, you might do:
String[] args = message.split("\\s+");
if (args[0].equals("bot.command"))
{
user = args[1];
...
}
The reason I'm splitting on the pattern \s+ (matches one or more whitespace characters) instead of just a space is that this way the program won't crash if the arguments are separate by more than one space or by something like a tab.
Edit (Removed StringTokenizer example) -
Since StringTokenizer is apparently "legacy", I would use a Scanner like this -
String message = "BOT.command USER_X THIS_IS_A_TIME THIS_IS_A_DATE";
String user = null;
String time = null;
String date = null;
// Use toLowerCase - assuming it's case insensitive.
if (message.toLowerCase()
.startsWith("bot.command")) {
Scanner st = new Scanner(message);
if (st.hasNext()) {
st.next();
}
if (st.hasNext()) {
user = st.next();
}
if (st.hasNext()) {
time = st.next();
}
if (st.hasNext()) {
date = st.next();
}
}
System.out.printf(
"User = %s, Time = %s, Date = %s\n", user,
time, date);
Output is
User = USER_X, Time = THIS_IS_A_TIME, Date = THIS_IS_A_DATE
If you are taking user input where you are not sure about how many space separated input words will be entered, you can do the following:
String[] terms = message.split("\\s+");
for(String word1 : terms) {
// doSomething()
}
But in your case, you just need 3 arguments, hence the looping will be done 3 times in the for loop.

Working with stringtokenizers and numbers

Using the second StringTokenizer constructor, write a method that
returns either the first or second token in the input string based on
the token argument. You will need to set custom delimiters which are
enclosed in a string but not separated by commas.
okay, so this is what i have so far..... i need help making the code so that it returns either the first or the second token depending on the users input.... right now i only know how to return either the first or the second token. ive tried making a while or and if statement but it always says i cant convert int to string, ive even tried type casting but it wouldnt let me do that either.... what can i do to make it return whichever token the user inputs?
String parseEqn_p2(String input, int token) {
StringTokenizer st = new StringTokenizer(input, "+-/*%");
String first = st.nextToken();
String second = st.nextToken();
return first ;
""it always says i cant convert int to string," - Use String.valueOf(int)
You probably want something like this
if (first.eqauls(String.valueOf(token)) {
return first;
} else if (...) {
}
return null; // if not found
...
Note : you may also need to .trim() the token, depending on what the input is. If there are spaces, the tokenizer won't exclude them, so you would need to call .trim() on the String first before trying to compare. like
String first = st.nextToken().trim();
Also, it looks like you may want to use a character class, because what you're doing is looking using a single delimiter of all the characters, which isn't what you want. Try this instead
StringTokenizer st = new StringTokenizer(input, "[+-/*%]");
In for any case you wanted the method to return an int, then you would need to parse the token
int first = Integer.parseInt(st.nextToken());
Also, NOTE: Whoever gave you this assignment, ask them to read the documentation for StringTokenizer, where you'll find this excerpt
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
The following example illustrates how the String.split method can be used to break up a string into its basic tokens:
String[] result = "this is a test".split("\\s");
for (int x=0; x<result.length; x++)
System.out.println(result[x]);

Categories