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) {
... ...
...
}
}
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.
Would any one be able to explain what this code does?
if ((x >= 'A' && x <= 'Z') || (x >= 'a' && x <= 'z')){}
where x is an int.
However I am not entirely sure how it works, would any one be able to provide me with an explanation? If anyone needs more details please comment below rather than down voting my question.
You can initialize an int with a char type because, the code of char can be represented with an int so for example :
char x = 'A';
int i = x;
System.out.println((int)x);//this will print 65
System.out.println(i);//this will print 65
The code of char A is 65 so for that you can compare a char with int in your case : if ((x >= 'A' && x <= 'Z') || (x >= 'a' && x <= 'z')){} you can also take a look here Java - char, int conversions
This code compare x to the ASCII code of A, Z... You can check there values in the ASCII table.
A verbal expression of your if-statement could be:
"If x value is the ASCII code of a letter (uppercase or not)."
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
Relating to this question: ROT-13 function in java?
What would be the code to decode rot13 in java?
Do I simply reverse the signs?
Below is the code for encoding a String into rot13:
public class Rot13 {
public static void main(String[] args) {
String s = args[0];
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c >= 'a' && c <= 'm') c += 13;
else if (c >= 'A' && c <= 'M') c += 13;
else if (c >= 'n' && c <= 'z') c -= 13;
else if (c >= 'N' && c <= 'Z') c -= 13;
System.out.print(c);
}
System.out.println();
}
}
You don't reverse the signs. The decoding method is identical to the encoding method.
For example : 'a' is encoded to 'n'. If you "encode" the 'n', it is decoded back to 'a'.
Because there are 26 letters (2×13) in the basic Latin alphabet, ROT13 is its own inverse; that is, to undo ROT13, the same algorithm is applied, so the same action can be used for encoding and decoding. The algorithm provides virtually no cryptographic security, and is often cited as a canonical example of weak encryption
The wikipedia article explains it pretty well
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...