Solved

string to BigInteger array

Posted on 2004-10-07
18
2,077 Views
Last Modified: 2012-06-21
I have a string (contents) wich is a concatation of numbers (129012002150...) and I use every group of 3 to load a BigInteger array with them:

BigInteger[] B_array;
Int i;

i = 0;
for (int x=0; x < contents.length()-3; x+=3)
{
B_array[i] = BigInteger.valueOf(contents.substring(x,x+3)));
i = i +1;
}

and it doesn't work. I want to have in my B_array something like:
129
12
2
150
.
.
0
Comment
Question by:edi02
  • 12
  • 5
18 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12248743
BigInteger.valueOf() doesn't take a String
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12248750
Try

B_array[i] = new BigInteger(contents.substring(x,x+3));
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12248827
This works:

        String contents = "129012002150";
        BigInteger[] B_array = new BigInteger[contents.length()/3];      // <<<<< have to new these ones
        int i = 0;
        for (int x=0; x <= contents.length()-3; x+=3) {                        // x <= instead of x <
            B_array[i] = new BigInteger(contents.substring(x,x+3));
            i++;
        }
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12248845
Add

        for (int  j=0; j<B_array.length; j++)
            System.out.println( ((BigInteger)B_array[j]).floatValue());

To check the result
0
 

Author Comment

by:edi02
ID: 12248864
thanx
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12248961
You're welcome.
Kindly close this Q if you feel like your problem is solved.
0
 

Author Comment

by:edi02
ID: 12248979
May i ask u one more question ...

How do you transform from Integer to BigInteger?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12249031
       Integer i = new Integer(2000);
        BigInteger b = new BigInteger("" + i);
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12249054
Or

       BigInteger b = BigInteger.valueOf(i.intValue());
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:edi02
ID: 12249171
I tried that and got this error:

C:\Second\second.java:167: int cannot be dereferenced
p_calc[indic] = BigInteger.valueOf(p_found.intValue());                                          
Were i have at the start:

private static BigInteger[] p_calc = new BigInteger[100];
0
 

Author Comment

by:edi02
ID: 12249195
This worked:

p_calc[indic] = new BigInteger("" + p_found);
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12249330
>> int cannot be dereferenced

if p_found is an int then that should have been
      p_calc[indic] = BigInteger.valueOf(p_found);

The one you wrote would work id p_found was an Integer
0
 
LVL 37

Accepted Solution

by:
zzynx earned 125 total points
ID: 12249510
Still questions/problems?
0
 

Author Comment

by:edi02
ID: 12249872
Ahh hopefully the last one ... :-)

Trying to convert a String to a Char. I know exactly my Strings are all between 0 and 255. So im sure they will have each of them a Char representation.

0
 
LVL 37

Expert Comment

by:zzynx
ID: 12250089
Do you mean a String that contains from "0" to "255" ?
>> String to Char
You mean Character of char?

      String s = "123";
      char c = (char) Integer.parseInt(s);
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12250174
Since you posted another Q for that last question, please close this one.
0
 
LVL 5

Expert Comment

by:Naeemg
ID: 12250273
can this code help u out.

String myStr = "ThisData";
Char[] myChar = new Char[myStr.getLength()];

for(int i = 0; i < myStr.getLenght(); i++)
     myChar[i] = myStr.charAt(i);


Bye
Naeem Shehzad Ghuman
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12250367
Thanks.

(Better accept the real "answer" as answer for the people that read it afterwards)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
network + 7 81
create a gui in perl 3 72
Impossible to extract MSI from new JAVA releases 2 50
eclipse argument 14 53
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now