How can you compare strings in android with greater than - java

I was wondering if there is a way to compare strings in android with greater than or >.
Lets say I have this:
String numbers = number.getText().toString();
if (numbers.equals("9")){
output.setText("50");}
so if you enter 9 in the number EditText field the output TextView will display 50.
I have quite a few different numbers that will then = a different number but what can I do if I want 10,11,12,13,etc to = 100?
Is there a way to do this by using something like this?
if (numbers.equals("9"++))
or is there some kind of wildcard in android like
if (numbers.equals("1"+"*"))
i know if i replace the * with zero it will be 10 so if this is possible I could make one for 1, one for 2, one for 3, etc. and this would still save me so much code.
If this is not possible let me know if you have any ideas.
Thanks guys!

You'll need to convert the String to a number first. Something like this:
int number = Integer.parseInt(number.getText().toString());
if (number > 9)
{
output.setText("50");
}
If the String is not guaranteed to be a valid integer you'll have to handle NumberFormatException too.

Is there a reason you can't use
Integer.valueOf("9");

Related

Using String.format to remove particular digits from an integer

Say we have a number
int number = 1234
and I want to use formatting to extract the final two digits as a string 34. How can one accomplish that?
I attempted:
String extraction = String.format("%d", number)
but this simply copies the entire number. I'm new to the syntax used by the formatter and can't seem to figure out the syntax that can go inside the characters of a number (or a String, for that matter) and pull them out. I've found a solution using charAt methods but am particularly curious about whether it's possible to accomplish it using formatting techniques.
Going via a String is inefficient and unnecessary to extract the last two digits of an integer.
Simply:
int lastTwoDigits = number % 100;
If you do want to go via a String, you can use:
String s = Integer.toString(number);
s = s.substring(s.length() - 2);
int lastTwoDigits = Integer.parseInt(s);
(note that this handles -ve numbers slightly differently to the first suggestion).

What datatype to use for 40 digit integers in java

For example
first number = 123456.....40 digits
second number = 123456.....40digits
Then I need to store the number
third = first * second;
after that I need to print third and again I need to perform operation like
fourth = third * third;
and print fourth. So how can I handle that much long integers which data type I need to use?
Use BigInteger class in java.math, then use BigInteger.multiply to multiply them together.
Check here for more on how to use it:
https://www.tutorialspoint.com/java/math/biginteger_multiply.htm
See this question its similar Arbitrary-precision arithmetic Explanation
the answer explain it quite good.
The basic is that you work with smaller parts. Just remember how you learned to work with big numbers in school (2-3 grade) you wrote down two numbers and
2351
*12
-----
4702
2351
------
28212
You just do small operations and store them somewhere you can put them in string or better in some array of integers. Where for example
number 123456789123456789 can be
number[0] = 6789
number[1] = 2345
number[3] = 7891
number[4] = 2345
number[5] = 1
String numberToShow = "";
for(int i = 0; i
There are some links for computer arighmetics
https://en.wikipedia.org/wiki/Category:Computer_arithmetic
and for adders
https://en.wikipedia.org/wiki/Category:Adders_(electronics)
In your computer you have basically also just some adders which can work only with some size of numbers and if you need to work with bigger you need to split it in smaller parts.
Some of this parts can be done parallel, so you can speed up your algorithm. These are usually more sophisticated.
But the basic principe is similar to working with big numbers on your primary school.

How to find remainder?

I want to find a remainder of very long numbers .I am writing a program for this and as I cannot find the remainder directly due to the fact that they are large numbers (in c) .How can I do this?the limit for the number from which I have to divide the bigger number to find remainder is 500.i.e 1 to 500
I thought of dividing the number like this:
1234567=1*10^6+2*10^5+...
1234567%x=1modx*10^6modx+2modx*10^5modx...
I need a better way than this.
Hint:
Use a linked list. Store the number as a group of numbers dynamically.
For eg:
112233445566778899001122 => 11223344 55667788 99001122
Now consider the individual unit and start from left to right. Find the reminder and manipulate it to add to the next group and go on.
Now implementation is very easy :)
Edit:
112233445566778899001122/6 => 11223344 55667788 99001122/6
11223344/6 =>2
2*100000000 + 55667788 = 255667788
255667788/6 => 0
0*100000000 + 99001122 = 99001122
99001122/6=>0
So the reminder is 0.
Remember, the individual unit after manipulation should be under the maximum range int can support.
If your question regards using very long or large numbers try using something a long long. The problem could be that the data type that you are using is too small to hold the values that you require.
You could try using a bignum library like GMP or another kind of ugly way in comparison would be to use arrays or lists, somewhat similar to this.
Other than that, the modulo operation % will calculate the remainder for you.

Getting each different numbers separately and examining them in java

I am new to java and i want to make a program where i want to get the integers the user has input, and look at what different numbers they are. and move them around as well as add stuff between them.
For example:
Enter number
User input: 123456
after that i want to see what the first number is, which is 1 or what the third number is which is 3. i want to be able to get those different information. and play around with them, for example add the sixth and the first number and print it out for the user.
Which technique in java allows me to do that, what is the name so i could study it more.
if there is a video online teaching that, it would also be great.
Thanks.
ps. i know the basics, i dont need to know how to do the second part playing with the numbers i just dont know how to tell what the first or second number in the lists of number is.
Eg: 123456
Convert the int to string keep the int also
1) String.valueOf(number)
2) Integer.toString(number)
get the length of the string using String.length();
the if you want the 3rd number
Use the int value for the following operation
Eg: 123456 (3rd number 3) Since you have the length of the string you know what will be the divisor
divide using 10 * 10 * 10 this will 123456/1000 = 123
Then % it with 10 always you can put these within a methos to get any position
Try the below code :You are taking an integer input and converting it into a string
public class InputNumberProgram {
public static void main(String[] args) {
Integer i = 5678;
// Convert your input to a string
String s = Integer.toString(i);
System.out.println("Length of input:" + s.length()); System.out.println(Integer.parseInt(s.valueOf(s.charAt(0)))+Integer.parseInt(s.valueOf(s.charAt(1))));
}
}
If you read it as a String and do what you want with String methods
For example you have String number. To get i-th symbol you can use number.charAt(i)

small java problem

Sorry if my question sounds dumb. But some time small things create big problem for you and take your whole time to solve it. But thanks to stackoverflow where i can get GURU advices. :)
So here is my problem. i search for a word in a string and put 0 where that word occur.
For example : search word is DOG and i have string "never ever let dog bite you" so the string
would be 000100 . Now when I try to convert this string into INT it produce result 100 :( which is bad. I also can not use int array i can only use string as i am concatinating it, also using somewhere else too in program.
Now i am sure you are wondering why i want to convert it into INT. So here my answer. I am using 3 words from each string to make this kind of binary string. So lets say i used three search queries like ( dog, dog, ever ) so all three strings would be
000100
000100
010000
Then I want to SUM them it should produce result like this "010200" while it produce result "10200" which is wrong. :(
Thanks in advance
Of course the int representation won't retain leading zeros. But you can easily convert back to a String after summing and pad the zeros on the left yourself - just store the maximum length of any string (assuming they can have different lengths). Or if you wanted to get even fancier you could use NumberFormat, but you might find this to be overkill for your needs.
Also, be careful - you will get some unexpected results with this code if any word appears in 10 or more strings.
Looks like you might want to investigate java.util.BitSet.
You could prefix your value with a '1', that would preserve your leading 0's. You can then take that prefix into account you do your sum in the end.
That all is assuming you work through your 10 overflow issue that was mentioned in another comment.
Could you store it as a character array instead? Your using an int, which is fine, but your really not wanting an int - you want each position in the int to represent words in a string, and you turn them on or off (1 or 0). Seems like storing them in a character array would make more sense.

Categories

Resources