Results 1 to 6 of 6
Thread: string to negative int problem
- 10-31-2009, 04:50 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 3
- Rep Power
- 0
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
- 10-31-2009, 05:08 PM #2
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
- 11-01-2009, 01:17 AM #3
Member
- Join Date
- Oct 2009
- Posts
- 3
- Rep Power
- 0
- 11-01-2009, 01:40 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
- 11-01-2009, 03:42 PM #5
Member
- Join Date
- Oct 2009
- Posts
- 3
- Rep Power
- 0
The program must go to each char in sequenceJava 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 ); } }Last edited by Fubarable; 11-01-2009 at 05:46 PM. Reason: code tags added to improve code readability
-
Code tags added to your post to improve readability of your code.
Similar Threads
-
I don't want negative money [BUG]
By anthonym2121 in forum New To JavaReplies: 1Last Post: 04-07-2009, 08:06 AM -
Negative elements
By swikar.java in forum New To JavaReplies: 6Last Post: 12-15-2008, 04:48 AM -
[SOLVED] Array of first negative number
By random0munky in forum New To JavaReplies: 9Last Post: 12-08-2008, 11:17 PM -
String Problem
By gnomewise in forum New To JavaReplies: 1Last Post: 10-19-2008, 12:37 PM -
string replace problem
By soni in forum Advanced JavaReplies: 8Last Post: 07-06-2008, 01:21 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks