Using StringTokenizer to do This [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need to use StringTokenizer to search through a string, and if they find this two words in the string, they will print "Yes.", if not, it'll print "No."
These two words are "eat", and "yet", and the string is "Did you eat yet?"
If it finds both words in that string it's suppose to print out yes, and if not, it's suppose to print out no. I have no idea how to do this. If you do, then please help.

StringTokenizer st = new StringTokenizer("Did you eat yet?");
This is how you initialize a StringTokenizer. Then your implementation should use the .hasMoreTokens() method to step through the tokens and check if they are equal to the words you are searching for.
Not sure why would want to use this method however, as..
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.
From the documentation at http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html

As others have suggested, using StringTokenizer is discouraged and in this case is over complicating the procedure in the first place.
Getting familiar with the Java String class is the right start. Here we find it's possible to determine if a string contains() the target string with yourString.contains(yourSubString).
You can combine this with another call to contains() in a conditional with...
if (yourString.contains(someSubString) &&
yourString.contains(someOtherSubString)) { ... }

Related

ReplaceAll but each replacement is replaced by the current number of occurrence java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 14 days ago.
Improve this question
I would like to use replaceAll to replace the every instance of a certain string but replace it with the current occurrence of that string. For example if my string looked like
'String str = "I really like bananas. bananas bananas bananas!"'
Could I use replaceAll to get to:
I really like 1. 2 3 4!
I'm hoping there is a way to use a lambda function to do this
Thanks
One sollution could be to use Pattern and AtomicInteger.
public String replaceStringWithMatchCount(String input, String stringToCount) {
AtomicInteger counter = new AtomicInteger(1);
return Pattern.compile(stringToCount).matcher(input)
.replaceAll((m) -> String.valueOf(counter.getAndIncrement()));
}
Another alternative would be...not to use lambda:
String inputString = "I really like bananas. bananas bananas bananas!";
int i = 1;
while (inputString.toLowerCase().contains("bananas")) {
inputString = inputString.replaceFirst("(?i)(bananas)", Integer.toString(i++));
}
System.out.println(inputString);
The (?i) within the expression used for the String#replaceFirst() method means to Ignore Letter Case.
When run, the Console output will display:
I really like 1. 2 3 4!

Java regular expression is validating the date in ddMMMyyy but not validating yyyy-MM-dd [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
I want to validate different kinds of strings which are of different format like
10JUN2022, 2Mx1D, 4M, 1D, TEN, ONE|TEN etc.. and I have written regular expression for that '''^([0-9A-WYZa-wyz ]+)([xX|]([0-9A-WYZa-wyz ]+))?$''' and it's working fine but I also need to validate one more string 2022-06-10, but the expression is failing.
When it comes to regex, don't try to get overly clever. Just solve the basic problem. If that takes multiple regex patterns, so be it. It's much easier to maintain and read.
I would use this for the first regex: [0-3]?\d\w{3}(1|2)\d{3}
and this for the second regex: (1|2)\d{3}(-\d{2}){2}
or combine them if you must: ([0-3]?\d\w{3}(1|2)\d{3})|((1|2)\d{3}(-\d{2}){2})

Creating a simple calculator using strings in Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to learn Java. My current assignment is to build a simple four function calculator..... this would be easy given if/else and/ or switch statements, but I'm supposed to build this using methods.
The original input has to be put in as a single string, so, in my mind, I'm going to have to take the single string and create substrings, then somehow convert these substrings into double values, while deleting whatever whitespace could possibly be between characters. My current idea is to somehow identify the "+,-,*, or /" within the string and divide into substrings before and after these values, using the appropriate defined method for whichever operator to do the calculations....
The problem is that I can't see a good way to divide these up into substrings or how to convert the numbers involved into double values. Anyone got any advice for me? Keep in mind, what we have gone through is pretty limited and I feel like I'm missing something REALLY simple out there.
You can split a string based on a particular character using str.split("\\+"), for example. You can convert the split pieces of the string to doubles by using Double.parseDouble(str);

trying to obtain a first name in the path after first back slash [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to display the first folder name from the path.
/mnt/sdcard/Videos/lk.jpeg, I want to display mnt string. in java
/mnt/sdcard/Videos/lk.jpeg--> **mnt**
You can split on / and use [1] element from result array.
You can either use regular expressions or you can use String.split(). Note that the split() result should be checked for live usage (e.g. if it has at least two elements).
String desired = "/mnt/sdcard/foo".split("/")[1];
String str = "/mnt/sdcard/Videos/lk.jpeg";
System.out.println(str.split("/")[1]);
Try this out. This is a poor question. But maybe the asker can be a newbie.

How to check if File "mm" exists in uppercase like "mM" or "Mm" in linux [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
so how may I check that? There are 36'000 files and on every event call I would need to check it, there could be plenty of these situations like: "nukasa" or "Nukasa" or "nUkasa", how may I detect all of these if I have one file name like: "NUKASA" or "nukasa" by event calling.
It's work with File not String. I just get String and then I need to work with File to check if in folder exists same file names just in Uppercase or Lowercase.
Please refer below the snippet I want to show as an example:
File sampleFile = new File("Nukasa");
String valueToCheck = "NUKASA";
if(sampleFile.getName().equalsIgnoreCase(valueToCheck))
{
//Logic you want to code goes here
}
Alternatively, you can use file.getName().toUpperCase().equals(valueToCheck) (if you assign upper case string to the variable valueToCheck). Same applies to toLowerCase() method also
CAUTION: This approach works fine as long as the Locale is of English language. For other languages, it won't work as expected. So, equalsIgnoreCase() is the best way. Credits to the person who suggested this
For case insensitive string comparison, use the String method str1.equalsIgnoreCase(str2).

Categories