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 7 years ago.
Improve this question
If I had a numberical idno value, how would I find the ones >=5000 and < 5000? Would i use idno.substring(7,11) and then check the conditions
if >=5000 print Male
if <5000 print Female
How would I perform this?
Check idno % 10000 to get the value of the last four digits.
Given that your ID number is stored as an integer:
int idno = ...; // some ID number
You can use this comparison:
if (idno % 10000 >= 5000) {
System.out.println("Male");
} else {
System.out.println("Female");
}
The if statement is a branching operator that allows your code to branch to one block or another based on the result of the comparison. This is different from a loop.
A loop is code that will execute repeatedly until a condition is met. For example, if you had an array of integer ID numbers:
int[] idArray = { .... , .... , .... }; // some array of IDs
You could loop through them like this:
for (int idno: idArray) {
if (idno % 10000 >= 5000) {
System.out.println(idno+" is Male");
} else {
System.out.println(idno+" is Female");
}
}
First convert your String to integer using
int idno1=Integer.parseInt(idno);
Then simple check idno1<=5000 or idno1>5000
Related
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 6 months ago.
Improve this question
I need to count the number of times a letter's present in a String.
For example:
str = "/data/name/data/name"
How do we get the number of / in this string?
val count = str.count { it == '/' }
To be honest, I am not sure whether you need an answer in java or kotlin (your tags include both), so if you need an answer in java:
String input = "/data/name/data/name";
char search = '/';
long count = input.chars().filter(ch -> ch == search).count();
(and if you need a kotlin version, just take a look at #Ivo's answer)
I think you can count with this way,
val str = "/data/name/data/name"
var count = 0
str.forEach {
if(it == '/'){
count++
}
}
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 7 years ago.
Improve this question
how can we figure-out a number is even or odd, with out using division(/) or percentile (%) symbols.
This one of the interview asked to me. Asked me to write program using java.
We should not use arithmetic symbols like division(/) , percentile (%).
Without using division or modulo, the only thing that comes to mind is checking if the last digit is in the set [ 1, 3, 5, 7, 9 ], like so:
public static boolean isEven(int testNumber) {
String strI = Integer.toString(testNumber);
String lastCharacter = strI.substring(strI.length() - 1);
return ("13579".indexOf(lastCharacter) == -1);
}
That would produce:
System.out.println ( isEven( 10) ); // true
System.out.println ( isEven( 11) ); // false
System.out.println ( isEven( 999) ); // false
Good enough?
Shift right and then shift left the number, if it remains the same number is even, otherwise it is odd.
boolean isOdd = (yourInteger&1)==1;
That is from the link provided by Christopher How do I check if an integer is even or odd?
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 7 years ago.
Improve this question
OK lets say the secret pin is:
2236 (resident evil 2 reference)
if a person type 2200
I get a match count of 2;
But:
If the person type: 2000 or 0200, or 0020, or 0002
results says that it still matches 2 numbers.
It should only match one of the 2 number but I still get a match count of 2.
I'm currently using :
if(currentPin.indexOf(secretPin.charAt(i)) != -1)
on a loop.
UPDATE:
Assuming all the the necessary variables are declared and initialized and all scopes are closed
heres the code that does the matching
for (int i = 0; i < secretPin.length(); i++)
{
if (currentPin.indexOf(secretPin.charAt(i)) != -1)
{
System.out.println("Found");
match++;
}
}
//output:
2236 //SECRET NUMBER display
0002 //USER GUESS
Found
Found
MATCH: 2 // should only match 1 if USER GUESS only contain one number 2
Assuming the number of digits is always the same between secret pin and current pin, this simple algorithm would do the trick to retrieve the number of match :
public static void main(String[] args) {
String secretPin = "2236";
String currentPin = "6322";
int match = 0;
for (int i = 0; i < currentPin.length(); i++) {
if (secretPin.contains(String.valueOf(currentPin.charAt(i)))) {
secretPin = secretPin.replaceFirst(String.valueOf(currentPin.charAt(i)), "");
match++;
}
}
System.out.println(match);
}
"if(currentPin.indexOf(secretPin.charAt(i) != -1)" is getting a character, checks if it's != -1, and passes the boolean result to indexOf. You also have a bracket mismatch. As given this will not compile, let alone produce inaccurate output. Please paste the code you say you are able to run if you would like it reviewed.
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
A beautiful number is a number containing only one type of digit, such as these: 0, 4, 44, 55555, 3333.
I would like to write an android app that finds out if a number is beautiful or not.
public static boolean isBeautiful(int n) {
char targetDigit = String.valueOf(n).toCharArray()[0];
for (char currentDigit : String.valueOf(n).toCharArray()) {
if (targetDigit != currentDigit) {
return false;
}
}
return true;
}
Bonus (sorry for the late addition of the bonus, but here it is), here's how to get all beautifuls up to x:
public static List<Integer> getBeautifuls(int upTo) {
List<Integer> beautifuls = new ArrayList<>();
int next = 0;
while (upTo >= next) {
beautifuls.add(next);
if (String.valueOf(next).toCharArray()[0] == '9') {
next = Integer.valueOf(new String(new char[String.valueOf(next).length() + 1]).replace("\0", "1"));
} else {
next = next + Integer.valueOf(new String(new char[String.valueOf(next).length()]).replace("\0", "1"));
}
}
return beautifuls;
}
Repeted string algorithm based on user102008's excellent answer on this question: Simple way to repeat a String in java
Note: Using Strings to create ints is a bit hack-ish and not so fast.
Try to check how many digits the number has, for example the number has 5 digits(between 10000-99999 and >= 11111)
if(num % 11111 == 0){
//it is beautiful number
}else{
//it is not beautiful
}
This is only an example for 5 digit numbers. You can develop an algorithm for checking other num of digits. I don't know is there any algorithm created before.
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 9 years ago.
Improve this question
I am trying to convert the tenth digit of an array to a number (10) if it is the value 'x'.
I have tried...
if (array[9] === 'x') {
'x' === 10;
};
Thanks
Try this, assuming that array is a char[]:
if (array[9] == 'x') {
array[9] = 10;
}
By the way, the code you posted is not valid for Java. This is not an operator: ===, we must use = for assignment and the trailing ; after the last } is unnecessary.
I edited your code already.
if (array[9] == 'x') //Line 1
{
array[9] = 10; // Line 2
}
On Line 1 you said ===. Java uses == to check if two primitive values are equal in value.
On Line 2 you used three equal signs again. If you want to reassign a variable, you will need to use ONE equal sign.