hiiii frns...


integer can hold only upto 8 bits...where as a string can hold more than 8 bits...so to compare them...is der any function???

like my code is like this

for(int i=0;i<= stringgg("12345678901234567890"); i++)

1. Java integers are 32 bits.
2. Java Strings are 16 bits per character

3. To compare them or use the String in arithmetic expressions, you first need to convert the string to integer with something like int i = new Integer("12345");

An int is 4 bytes which is 32 bits (31 of which is value and the 32nd is a pos/neg toggle). A long is 8 bytes (63 bits data and 1 pos/neg toggle) so use long and parse that String to a long using Long.parseLong(string). If a long is also not large enough use the BigDecimal class. See the API docs.

commented: Very well written comment. +7

An integer in Java is a 32-bit number. If you need anything larger than that, I suggest you look at the Math.BigInteger class. Documentation for a BigInteger can be found here.

Looks like James, masijade and I all posted at the same time, and I forgot about the long type :$ Use that as masijade said.

and since you're working with String-objects, make sure they're valid numeric values before you try to compare them :)

Ach, yeah, and as darkagn say, BigInteger, not BigDecimal (unless you're going to have decimals, in which case you'd have been using float and double instead of int and long, anyway).

Yes, long is necessary if your values can exceed 2^31 (although I suspect your example loop was just an example; you don't really want to execute a loop that many times!).
If your String isn't valid for converting to integer (or long) you'll get a NumberFormatException thrown.

thanks for the fast responses!!!!

but is their any other methods other than big integers...

What do u need exactly?

r u talking ab integers of more than 10 bytes ..like 12345678910228377466464678262737867865786826868326???

Leave your lame chat-speak at the door. Read the forum rules on this.

Are you talking about integers of more than 10 bytes ..like 12345678910228377466464678262737867865786826868326???

@Ezzaral
Are you happy ???

I guess yes, but he would have been much happier if you would have dropped the multiple character typing (???) habit of yours too. That too is a symbol of chat/IM speak.

Are you talking about integers of more than 10 bytes ..like 12345678910228377466464678262737867865786826868326???

@Ezzaral
Are you happy ???

Yes, much better. That wasn't so hard, was it?

Thank you

r u talking ab integers of more than 10 bytes ..like 12345678910228377466464678262737867865786826868326???

yea i need for more than 10 byete as what u said...i.e more than or equal to 500 digits...

i.e my string is like this .....String (1234455....upto >=500 digits)

now i want compare that wid an integer in a for loop as already said

for(i=0;i< String (1234455....upto >=500 digits);i++)

I would like to ask you the reason that you are comparing it to an integer.

I would like to ask you the reason that you are comparing it to an integer.

am writing a code for division for more than 512 bits...so in that i need to compare the value 'i' upto the value of the String.

so for that reason am asking u is their any special methods ??

So is it that you are just interested in finding out whether the string contains all digits and not any other characters ? If yes you can use regex to do that, if not detail out the process.

So is it that you are just interested in finding out whether the string contains all digits and not any other characters ? If yes you can use regex to do that, if not detail out the process.

yea the string contains only integer values....what is regex???and how to use it??

Here is the tutorial for Regular Expressions (regex is short for them) you will get to know a lot about them and how to use them on the link.

To just provide a brief about what a regex is and how they would be useful in such operations, take the current example where you just want to check wheter a string is an integer, in other words you want to check whether it contains just digits or other characters too.
So you could define a regex with a pattern such as [\\d*] Here '\\d' means digits (0-9), and * means any number of occurrences of them. After such a pattern has been defined, you just check whether the string satisfies this pattern or not, any string satisfying this pattern would certainly contain only digits and hence would classify to be an integer.

I'm still not sure what you are asking, are you trying to determine if each position in a given string is a digit (numeric)? If so then what was stated above will do just fine. If not what is the purpose of comparing a gigantor number such as xyz...512 characters to an integer.

In your example loop you would simply loop up to the size of the the string and not necessarily the position.

IE: if you need to compare each position in a string to determine if it is in fact a digit you would want something that loops up to the string.length and then use the [] accessors to determine if a specific point is a digit IE: is string a digit.

If you need a mathematical comparison, and the numbers are too large to store in a long, then no, there is no other (uncomplicated) way than with BigInteger. Now, if you think you must do it some other way, than convert the int that you are to compare it to, to a String, than check the lengths of the String. The longer one is the larger. If they are same, start taking one character at time from the front of each String and comparing them (Strings compareTo is good enough for this). As soon as you find one that is larger than the other, you've found the larger number. Now, is it wise to do it this way? No. It is wise to use BigInteger, as already said.

But, as asked once already, what is it that you are actually trying to acheive? If you are attempting to loop some 1000000000000 times, than be prepared to wait, no matter what method you use. And, if you are trying to loop that many times, than the real question is why? What are you doing that you feel you need to loop that long?

convert i to String and use compareTo() function

here is how it works

public class StrCmp {
    public static void main(String args[]) {
        String s1 = new String("1234567891011");
        String s2 = new String("1234567891012");
        System.out.println(s1.compareTo(s2));
    }
}

convert i to String and use compareTo() function

here is how it works

public class StrCmp {
public static void main(String args[]) {
String s1 = new String("1234567891011");
String s2 = new String("1234567891012");
System.out.println(s1.compareTo(s2));
}
}

Try it with

public class StrCmp {
  public static void main(String args[]) {
    String s1 = new String("1234567891011");
    String s2 = new String("234567891012");
    System.out.println(s1.compareTo(s2));
  }
}

You might be surprised by the outcome.

Anyways it gives the same outcome which is "-1"

Anyways it gives the same outcome which is "-1"

Which is saying that s1 is less than s2. Well, is it? ;-)

Yes you are right.

@lipun4 : Read what the docs for compareTo() have to say to understand the point masijade makes here cleverly.

Like I said, though, "-1" says that s1 (which is the lesser number the first time, but the greater number the second time) is less than s2, whereby s1 is significantly greater than s2 the second time, not less than s2 as "-1" indicates.

What I am saying, is that you cannot String compare the entire String at once. That comparison is done alphabetically, not numerically. For single characters, alphabetically and numerically are the same, but not for multicharacter Strings. (E.G. alphabetically 10 is less than 2).

commented: Both your posts previous and this one underline a point which o/w can be missed. Good. +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.