• Post Reply Bookmark Topic Watch Topic
  • New Topic

Change a negative number to a positive number?  RSS feed

 
Jeff Boynton
Greenhorn
Posts: 18
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I am 4 weeks into my Intro to Java course and I am having a bit of trouble with my code. I need to make a program that will take a user inputted number, space the numbers out, then add them to a total sum. What I am having a hard time with is when I enter a negative number. I can't figure out what I need to do to have my program ignore the "-" in the string. Any help is appreciated.

 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest reading through the API documentation, although it can be tough to know where to begin looking. I suggest trying the Character class, particularly the isDigit method; you might find that useful.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are you trying to change a negative number to a positive, or are you trying to ignore certain characters?

You may want to look at the Math class and the "abs" function. You have an int. it should be easy to make it positive, and THEN convert it to a String...

Of course, you can also do this without the string at all. It's not hard to get the ones digit value, then use integer division to whittle the number down...
 
Jeff Boynton
Greenhorn
Posts: 18
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it!



Thank you very much for the help!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!