Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Converting primitive data types causing NumberFormatException

  1. #1
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Question 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

    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


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  3. #3
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Converting primitive data types causing NumberFormatException

    Would you mind looking at the assignment? It is here:Jclass.freeforums.org • View topic - Java Primitives Assignment.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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?

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  6. #6
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Converting primitive data types causing NumberFormatException

    Quote Originally Posted by helloworld922 View Post
    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.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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?

  8. #8
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Converting primitive data types causing NumberFormatException

    Quote Originally Posted by Norm View Post
    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.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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?

  10. #10
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Converting primitive data types causing NumberFormatException

    Quote Originally Posted by Norm View Post
    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

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Converting primitive data types causing NumberFormatException

    Have you solved all your problems now?

  12. #12
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Converting primitive data types causing NumberFormatException

    Yes, here is the finial code:

    /*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!
    -Mel

Similar Threads

  1. Tutorial: Primitive Data Types
    By newbie in forum Java Programming Tutorials
    Replies: 3
    Last Post: July 5th, 2012, 11:56 PM
  2. [SOLVED] Instance data member vs Local variables (primitive/object reference)
    By chronoz13 in forum Java Theory & Questions
    Replies: 1
    Last Post: September 23rd, 2011, 12:42 AM
  3. Primitive data type
    By stuart harper in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 17th, 2011, 12:14 PM
  4. Double & Float primitive types
    By Zeek in forum Java Theory & Questions
    Replies: 10
    Last Post: September 12th, 2011, 07:31 AM
  5. Replies: 4
    Last Post: September 5th, 2010, 10:29 AM