I've got a simple java assignment. I need to determine if a string starts with the letter A through I. I know i have to use string.startsWith(); but I don't want to write, if(string.startsWith("a")); all the way to I, it seems in efficient. Should I be using a loop of some sort?
You don't need regular expressions for this.
Try this, assuming you want uppercase only:
char c = string.charAt(0);
if (c >= 'A' && c <= 'I') { ... }
If you do want a regex solution however, you can use this (ideone):
if (string.matches("^[A-I].*$")) { ... }
if ( string.charAt(0) >= 'A' && string.charAt(0) <= 'I' )
{
}
should do it
How about this for brevity?
if (0 <= "ABCDEFGHI".indexOf(string.charAt(0))) {
// string starts with a character between 'A' and 'I' inclusive
}
Try
string.charAt(0) >= 'a' && string.charAt(0) <= 'j'
char c=string.toLowerCase().charAt(0);
if( c >= 'a' && c <= 'i' )
...
This makes it easy to extract it as a method:
public static boolean startsBetween(String s, char lowest, char highest) {
char c=s.charAt(0);
c=Character.toLowerCase(c); //thx refp
return c >= lowest && c <= highest;
}
which is HIGHLY preferred to any inline solution. For the win, tag it as final so java inlines it for you and gives you better performance than a coded-inline solution as well.
if ( string.toUpperCase().charAt(0) >= 'A' && string.toUpperCase().charAt(0) <= 'I' )
should be the easiest version...
Related
basically, Ive been asked to write a program that checks how many times a number is apparent in a string and print it out. This is what i have
BufferedReader input = new BufferedReader (new InputStreamReader (System.in));
System.out.println("Please enter your string");
String s = input.readLine();
/*System.out.println("Please enter the chracter you are looking for");
char c = (char)input.read();*/
char one = '1';
char two = '2';
char three = '3';
char four = '4';
char five = '5';
char six = '6';
char seven = '7';
char eight = '8';
char nine = '9';
char zero= '0';
int counter = 0;
for( int i=0; i<s.length(); i++ ) {
if( s.charAt(i) == one || s.charAt(i) == two || s.charAt(i) == three || s.charAt(i) == four ||
s.charAt(i) == five || s.charAt(i) == six || s.charAt(i) == seven
|| s.charAt(i) == eight || s.charAt(i) == nine || s.charAt(i) == zero ) {
counter++;
}
}
is there a faster, better way to do this? I tried another way but for this error
Error: The operator || is undefined for the argument type(s) boolean, char
Instead of declaring digits by yourself in the code, you can take a look at Character.isDigit() method in Java. This will make the code much cleaner. There's no other faster way of doing this.
If you want to count occurrence of each digit, one simple way to do that would be to use Java Maps. You can read basic tutorial about Maps from here.
this works in c#
foreach (char c in str)
{
if (c >= '0' && c <= '9')
counter++;
}
You can use the decimal value of the character (as defined in the ASCII table)
String s = "abc123def456";
int cpt = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
cpt++;
}
}
System.out.println(cpt); // 6
You can also use the Character::isDigit method
if (Character.isDigit(s.charAt(i))) {
cpt++;
}
EDIT :
If you are using Java 8+ you can turn the String in an stream of characters, apply a filter to keep the digits, and then count the number of elements in it.
long nbDigits = s.chars()
.filter(Character::isDigit) // If the character is a digit (= the isDigit() method returns true) it's kept in the stream
.count();
System.out.println(nbDigits); // 6
is there a faster, better way to do this
Your approach is absolute correct and almost maximum fast!. You could make it mo readable.
I think that general algorithm is the same for all languages with O(n):
Loop the array and increment counter when find a number character.
Your approach is absolute correct and almost maximum fast!. (note: I really think that speed between two comparisons and nine is very-very small and we should not care about it) All you can do is just write it with as less lines of code as possible. You can do following corrections:
char is integer in JVM and ASCII code for 0-9 is 0x30-0x39, so you can move from == to ch >= '0' && ch <= '9'.
Character class contains special method to check it: Character.isDigit(ch).
For java 8 you can use Streams instead of manual for...each.
Not using streams (plain old java) I think this approach provides maximum speed and lees memory objects
public int countDigits(String str) {
int count = 0;
for(int i = 0; i < str.length(); i++)
if(Character.isDigit(str.charAt(i)))
count++;
return count;
}
Using streams (from java 8). Looks good, works a little bit slower that previous example, and creates some additional objects in memory.
public int countDigits(String str) {
// could be a little bit slower, because additional objects are created inside
return (int)str.chars().filter(Character::isDigit).count();
}
P.S. If you want to show your skill, plain old java variant is more preferable. In working code, both variant are equal.
P.P.S. actually String.toCharArray() or str.chars() are look more elegant and even a little bit less performed that str.charAr(int), because they create additional objects in memory, but str.charAr(int) works directly with internal arrays. But I did not faced with problem with any approaches in real application.
So I'm trying to write this in a short way:
char letter;
while ( letter!='A' && letter!='B' && letter!= 'C... letter!= 'a'
&& letter !='b' && letter!=c)
Basically if the user does not input a letter between A and C, a while loop will run until A,B,C,a,b,c is inputted.
It should be in the form of
while(letter<'a' && letter > 'c')
but it didn't work apparently because if I inputted F, is it greater than 'C', but it is less than 'c', since char uses ACSII.
There are many ways to do this check.
char letter;
while (!(letter >= 'A' && letter <= 'C') && !(letter >= 'a' && letter <= 'c'))
Or you can use Character.toUpperCase or toLowerCase on the letter first, to remove half of the conditions.
Or, if the range of letters is small or non-contiguous, you could do:
char letter;
while ("ABCabc".indexOf(letter) == -1)
There are more ways of course.
Set letter to lower case and then check:
letter = Character.toLowerCase(letter);
while (letter < 'a' && letter > 'c') {
// ...
}
This way, even if the user enters an upper case letter, the check will work.
You need two conditions and a bit of de Morgan:
while(!((letter >= 'A' && letter <= 'C') || (letter >= 'a' && letter <= 'c')))
or good old regex
char input = 'B';
Pattern p = Pattern.compile("a|b|c", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher("" + input);
if (m.find())
{
System.out.println("found");
}
You can check with ASCII code of characters such as 65 for 'A' and 97 for 'a'. So you could do if (letter > 65) to check if letter greater than 'A'. Hope this helps.
Updates :
For checking if letter is only either of A,B,C,a,b,c, use this check : if (letter >= 65 && letter <= 67) || (letter >= 97 && letter <= 99) .
Please mark as answer if this helps.
I never learnt C#.
Here I got in C# & I need to use this method into java.
I tried with different combination & permutation. But I didn't get success into this.
String word = "#############";
foreach(var c in from ch in word
where ch >= 'B' &&
ch <= 'F' &&
sleng < 4
select ch)
Can anybody I have idea how to covert above code into java?
I done it .Main start need to convert into toCharArray()
And it's not possible to use query as c# into java.
You need to use if else statement for this.
for (char ch : word.toCharArray()) {
if (ch >= 'B' && ch <= 'F' && soundexString.length() < 4) {
... ...
...
}
}
I need this program to keep on asking for cardChosen1, until the user gives an answer of something between 'a' and 'h'. I know I could just put
while (cardChosen1 == 'a' && cardChosen1 == 'b' && cardChosen1 == 'c' &&...
Etc. But I would prefer there to be a more concise way. I want it not only block letters, but also symbols and numbers
I've tried the following:
while (cardChosen1 > 'a' && cardChosen1 < 'h')
{
System.out.println ("Hey");
cardChosen1 = e.getKeyChar ();
}
But it doesn't work.
You're almost there. The problem is that you loop while you do get an answer between 'a' and 'h', and you wanted to loop as long as you don't get such an answer. Just slap on a negation operator (!), and you should be fine:
while (!(cardChosen1 > 'a' && cardChosen1 < 'h')) {
Better yet, you can simplify this statement with some boolean algebra:
while (cardChosen1 <= 'a' || cardChosen1 > 'h')) {
this condition is universally false
while (cardChosen1 == 'a' && cardChosen1 == 'b' && cardChosen1 == 'c' &&...
this condition will work but I would prefer you to create a Set<Character> and look up if it exists in Set by contains()
while (cardChosen1 > 'a' && cardChosen1 < 'h')
First of all, the right form of what you typed above as the suggested answer has to be like this:
while (cardChosen1 == 'a' || cardChosen1 == 'b' || cardChosen1 == 'c' || ... || cardChosen1 == 'h')
because you need a character to be "a" or "b" or "c" ... but what you entered wants the character to be all of characters from 'a' to 'h' which is impossible.
Now for your concise way. You are close to the answer and with some minor changes it will work. But it's more readable (in Java) to use Explicit Type Cast (of course it's optional). Like this:
while ((int) cardChosen1 >= (int) 'a' || (int) cardChosen1 <= (int) 'h')
Also notice about >= and <= signs as you accept the answer if it's 'a' or 'h' itself.
And finally you must convert && to || as I explained above.
You should check for ASCII codes.
for example if you want to check a to h range try
(int)cardChosen1 > 97 || (int)cardChosen1 < 122
Why when comparing a char against another it must be taken also from a string? For example;
This does not work
while(i < t.length() && zeroCount < 5) {
if(t.charAt(i) == 0){
zeroCount++;
}
i++;
}
Nor does this
char zero = 0;
while(i < t.length() && zeroCount < 5) {
if(t.charAt(i) == zero){
zeroCount++;
}
i++;
}
The only way I managed to get it working is like this...
String zeros = "0000000000";
while(i < t.length() && zeroCount < 5) {
if(t.charAt(i) == zeros.charAt(i)){
zeroCount++;
}
i++;
}
Can anyone explain if am doing something wrong, or if it is just not acceptable to do it like the top 2 examples. If so, why?
You're confusing
char zero = 0;
with
char zero = '0';
The former is the null-character (ASCII value of zero), whereas the latter is the character representing the digit zero.
This confusion is a rather unfortunate hang-over from C, with char variables being treated as numbers as well as characters.
You are looking for the character '0'? Then compare to '0', not 0.
You're comparing against Unicode value 0 (aka U+0000, the "null" character) - which is not the same as the Unicode character representing the digit 0.
Use '0' instead of 0:
while(i < t.length() && zeroCount < 5) {
if(t.charAt(i) == '0'){
zeroCount++;
}
i++;
}
Use '0' instead of 0.
The simple answer is that the value 0 is not the same as the character '0' which has an ASCII code of 48 (IIRC).
You should compare it with the char value charAt(i) == '0' or subtract the char before comparison charAt(i) - '0' == 0
These other answers have it right, but there’s one very important thing you should know. You should never use chatAt! You should only use codePointAt.
Similarly, you mustn’t blindly use i++ to bump through a string. You need to see whether s.codePointAt(i) > Character.MAX_VALUE to know whether to give an extra i++ kicker.
For example, to print out all the codepoints in a String s in standard "U+" notation:
private static void say_U_contents(String s) {
System.out.print("U+");
for (int i = 0; i < s.length(); i++) {
System.out.printf("%X", s.codePointAt(i));
if (s.codePointAt(i) > Character.MAX_VALUE) { i++; } // UG!
if (i+1 < s.length()) { System.out.printf("."); }
}
}
That way you can output like U+61.DF, U+3C3, and U+1F4A9.1F4A9 for the corresponding strings. That last one looks like "\uD83D\uDCA9\uD83D\uDCA9", which is simply insane.