Results 1 to 6 of 6
  1. #1
    pendulum101 is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default string to negative int problem

    hey there first and foremost this is school related and if anyone has a problem with me asking for help then it is at their discretion whether or not to help me and if there is a problem with admin i can understand.

    As far as requests go i feel that this is very simplistic and would really only aid me in a small way but i'll let you be the judge of that

    my problem is that i needed to make a program that takes in a string of digits and output them as an integer. the problem that i am having is that i cannot figure out how to do this with negative numbers.

    i know that it relates to unicode but ... im stuck
    i convert the integer and using an IF i check if the first character is a '-' if it is i multiply by -1.

    when the input is -1 i get 29 as an output. if anyone has any input that they would like to give i'd be very appreciative

    Thanks in advance

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    Java Code:
    int value = 0;
    
    try
    {
       value = Integer.parseInt(str);
    }
    catch(Exception e)
    {
       System.out.println("This was not a number");
    }
    My Hobby Project: LegacyClone

  3. #3
    pendulum101 is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by mrmatt1111 View Post
    Java Code:
    int value = 0;
    
    try
    {
       value = Integer.parseInt(str);
    }
    catch(Exception e)
    {
       System.out.println("This was not a number");
    }
    its not as easy as that i have to convert every char in the string using a loop charAt() and then convert it accumulating the results oh and i can only use int.parseInt() to test it :( any other ideas?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by pendulum101 View Post
    when the input is -1 i get 29 as an output.
    Post a small self-contained compilable example that gives rise to this behaviour. (That way you avoid "helpful" advice like people saying that there must be an error on line 42...; )

  5. #5
    pendulum101 is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Post a small self-contained compilable example that gives rise to this behaviour. (That way you avoid "helpful" advice like people saying that there must be an error on line 42...; )
    Java Code:
    public class lab2 {
      public static void main( String[] args ) {
        String input =  ( args[0] );
        int position = 1;
        int exponent = input.length() -(int) 1;
        int power = 1;
        int output = 0;
        int negative = -1;
        for( ; exponent > 0 ; exponent-- ) {
          power = power * 10;
        }
        for( position = 0 ; position < input.length() ; position++ ) {
            int digit = (int) input.charAt(position) - (int)'0';
            digit = digit * power;  
            output = output + digit;
            power = power / 10;
          }
             
        System.out.println("input = \"" + input + "\"");
        System.out.println("output = " + output );
      } 
    }
    The program must go to each char in sequence
    Last edited by Fubarable; 11-01-2009 at 05:46 PM. Reason: code tags added to improve code readability

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. I don't want negative money [BUG]
    By anthonym2121 in forum New To Java
    Replies: 1
    Last Post: 04-07-2009, 08:06 AM
  2. Negative elements
    By swikar.java in forum New To Java
    Replies: 6
    Last Post: 12-15-2008, 04:48 AM
  3. [SOLVED] Array of first negative number
    By random0munky in forum New To Java
    Replies: 9
    Last Post: 12-08-2008, 11:17 PM
  4. String Problem
    By gnomewise in forum New To Java
    Replies: 1
    Last Post: 10-19-2008, 12:37 PM
  5. string replace problem
    By soni in forum Advanced Java
    Replies: 8
    Last Post: 07-06-2008, 01:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •