Public Group

# Java - problem parsing String to numeric type

This topic is 3807 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm having trouble with Java right now. I'm working on a coursework assignment which is a simulation (not emulation) of the SSEM Manchester Baby from 1949. Basically, I've got a string which contains 32 1s and 0s as a binary representation of an instruction line inside the SSEM, and I need to load this into the accumulator when a specific instruction is run. The accumulator is an int, so I'm wanting to convert the string of 1s and 0s into an int so I can simply add it on. Problem is, I'm getting java.lang.NumberFormatException whenever I try to do anything at all. Here's the code:
// LDN - Instruction 2, binary: 010
// Copy contents of specified line, negated, into the accumulator
public static void ldn(store babyStore,accumulator babyAccumulator, PICI babyPICI)
{
// Use CI and a variable to seek along the line
System.out.println("LDN instruction called");
String temp = "";
for (int x = 0; x < 32; x++)
{
if(!(babyStore.store[babyPICI.returnCiCount()][x]))
{
temp+="0";
}
else
{
temp+="1";
}
}

// We need to turn this line in the store into negated binary
int decValue = Integer.parseInt(temp);
//System.out.println(temp);
//System.out.println("decValue: "+decValue);
//babyAccumulator.updateAccumulator(temp);
}


And here's the error:
Exception in thread "main" java.lang.NumberFormatException: For input string: "01000000000000100000000000000000"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at instruction.ldn(instruction.java:80)
at instruction.determineInstruction(instruction.java:22)
at ManchesterBaby.executeInstructions(ManchesterBaby.java:82)
at ManchesterBaby.main(ManchesterBaby.java:47)

I've tried Long.parseLong but that didn't work either. I'm trying to convert the string of 1s and 0s into a numeric type so I can then convert it into an int the standard way. babyStore.store[][] is a 32x32 array of booleans (the easiest way of representing 1 and 0 within memory). There are 32 lines each with 32-bits on them. I've also got to negate the line once I've got it and add the negated value onto the value in the accumulator, but I can't even get that far. Any ideas? TIA, ukd.

##### Share on other sites
What you probably really want to do is turn that binary number into an int. Your trying to turn that into a decimal number which is *way* to big. In int has 32 bits of precision with sign counting as one bit, so 31 bits of size is up to +2,000,000,000.

Integer.parseInt() wont convert it from binary for you. You must do it yourslef.

If you need help with that just say something and ill tell you.

##### Share on other sites
int integer = Integer.parseInt(binaryString, 2);

##### Share on other sites
hmm i didnt know about that. Thats useful to know actually. Thanks!

##### Share on other sites
Just browsing the API for a few seconds and you would have discovered the method ;)

##### Share on other sites
@OP: parseInt needs to be told what base your number is in. For obvious reasons, it assumes by default that your string represents a base 10 number, but you are giving it a base 2 number. A "number format exception" means your number is in the wrong format, oddly enough. Fortunately, you can simply provide the base as an optional second parameter to the call. You can find out this kind of thing by simply reading the documentation.

However, there is no good reason to create a string out of the bits just to convert it back to a number. Just put the bits into the appropriate places of the int variable. Hint: >> is a "shift" operator, which moves all the bits forward (i.e. to more significant places) by the specified distance, leaving zero bits on the right. | is a bitwise OR. Consider what happens if you shift the number one place to the right, and then OR it with either 0 or 1.

##### Share on other sites
Quote:
 Original post by ZahlmanHint: >> is a "shift" operator, which moves all the bits forward (i.e. to more significant places) by the specified distance, leaving zero bits on the right.
Unless I'm much mistaken, Java conforms to the classic conventions: "<<" is shift left (towards more significant bits) and ">>" is shift right (towards less significant bits).
Typo? [smile]

1. 1
2. 2
Rutin
23
3. 3
JoeJ
20
4. 4
5. 5

• 29
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631740
• Total Posts
3001962
×