Converting primitive data types causing NumberFormatException
Hello all,
Hmm, for some reason I cant convert the value of a string to byte or any other type of primitive data type. I tried to use valueOf(), decode(), and parseByte(), but none would work.
IDE: Eclipse
Code java:
package OTherTests;
public class JPM {
public void stringToByte(byte b, String s){
b = Byte.valueOf(s);
}
public static void main(String[] args){
//Init variables
String string2 = "Alem";
byte byte2 = 0;
JPM run = new JPM();
//convert vales
run.stringToByte(byte2, string2);
}
}
Exception in thread "main" java.lang.NumberFormatException: For input string: "Alem"
at java.lang.NumberFormatException.forInputString(Unk nown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Byte.parseByte(Unknown Source)
at java.lang.Byte.valueOf(Unknown Source)
at java.lang.Byte.valueOf(Unknown Source)
at OTherTests.JPM.stringToByte(JPM.java:6)
at OTherTests.JPM.main(JPM.java:17)
Thanks.
-Mel
Re: Converting primitive data types causing NumberFormatException
What is the byte equivalent for "Alem"? That doesn't make any sense. Those methods are suppose to take a string like "5" and convert it to the byte value 5.
Re: Converting primitive data types causing NumberFormatException
Re: Converting primitive data types causing NumberFormatException
Can you show some examples of the input and what you want the output to be for your code?
Re: Converting primitive data types causing NumberFormatException
Looking at the other examples they're converting a single char to other data types, not trying to parse a string object to a primitive value. In Java char's are an integer-like type who's values can be mapped to a character table to gain physical meaning. For example, the character 'A' is mapped to the integer value 0x41 (65).
You can extract a single character from a string using the charAt() method, then performing a type cast to the data type you want.
Re: Converting primitive data types causing NumberFormatException
Quote:
Originally Posted by
helloworld922
Looking at the other examples they're converting a single char to other data types, not trying to parse a string object to a primitive value. In Java char's are an integer-like type who's values can be mapped to a character table to gain physical meaning. For example, the character 'A' is mapped to the integer value 0x41 (65).
You can extract a single character from a string using the charAt() method, then performing a type cast to the data type you want.
Ah, thanks, I thought it also applied to Strings.
Re: Converting primitive data types causing NumberFormatException
Can you show some examples of the input and what you want the output to be for your code?
Re: Converting primitive data types causing NumberFormatException
Quote:
Originally Posted by
Norm
Can you show some examples of the input and what you want the output to be for your code?
There will be no input, output should be something like: 77 is a java.lang.Byte. It isn't something to be used in other project, it is just an assignment to see what the members of a programming group are capable of doing.
Re: Converting primitive data types causing NumberFormatException
I don't understand what the code is supposed to do if it does not have input from some source.
The input to the method in your first post was a String: "Alem"
What output did you expect with that input?
Can you explain what your problem or question is?
Re: Converting primitive data types causing NumberFormatException
Quote:
Originally Posted by
Norm
I don't understand what the code is supposed to do if it does not have input from some source.
The input to the method in your first post was a String: "Alem"
What output did you expect with that input?
Can you explain what your problem or question is?
Sorry I thought you meant user input. I am supposed to convert a String to primitive data types and print the value of the data type plus the name of the class using the getClass().getName() method.
Sorry for being unclear before.
-Mel
Re: Converting primitive data types causing NumberFormatException
Have you solved all your problems now?
Re: Converting primitive data types causing NumberFormatException
Yes, here is the finial code:
Code java:
/*Author: Melawe(Alem)
* Date of completion: 09/Dec/2011
* Code made for: JClass's first assignment, a Java Programming group.
* Link:[url]http://jclass.freeforums.org]Jclass.freeforums.org[/url]
*
*/
public class JavaPrimitivesMel{
/*make a method to display the value of a primitive
* data type, cast it and print its value and the classes full name.
*/
public static void printClassName(Object obj){
System.out.println(obj + " is a " + obj.getClass().getName());
}
public static void main(String[] args){
String convertMe = "Alem";
byte testByte = (byte) convertMe.charAt(0);
short testShort = (short) convertMe.charAt(1);
int testInt = convertMe.charAt(2);
long testLong = convertMe.charAt(3);
double testDouble = convertMe.charAt(2);
float testFloat = convertMe.charAt(1);
char testChar = convertMe.charAt(0);
JavaPrimitivesMel.printClassName(testByte);
JavaPrimitivesMel.printClassName(testShort);
JavaPrimitivesMel.printClassName(testInt);
JavaPrimitivesMel.printClassName(testLong);
JavaPrimitivesMel.printClassName(testDouble);
JavaPrimitivesMel.printClassName(testFloat);
JavaPrimitivesMel.printClassName(testChar);
}
}
[consol]run:
65 is a java.lang.Byte
108 is a java.lang.Short
101 is a java.lang.Integer
109 is a java.lang.Long
101.0 is a java.lang.Double
108.0 is a java.lang.Float
A is a java.lang.Character
BUILD SUCCESSFUL (total time: 2 seconds)[/console]
Thanks all, it is much appreciated! :D
-Mel