• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is a String in an integer possible?  RSS feed

 
william chaple
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, well ive been extremely frustrated and I need some help please





this is my code, as you can see, I have if the min is less than 10, display the string "0" next to it so it will be something like

blank is zero and min is one

so it will display as 01 but after min reaches 10 and above, the 0 goes away. Problem I have is, you just cant add "blank" to int checker because checker is an int and blank is a string. So what must I do in order for it to display the 0 under checker?
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not declare checker as a String and convert min to a string using String.valueOf() method after the conditional test in the ternary operator...
 
Ashley Lester
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want a simple formatted time in the format hh:mm, have you looked at the SimpleDateFormat class?

http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
 
william chaple
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ashley, I am actuallyg oing to give that a try; also, I am trying to build up my logic, I would like to know how the who int to string and string to int works

So,

after

I should add and convert "min" variable to a string using String.valueof()? and that will allow me to add the 0 in the ternary operator?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your checker variable is an int. It will never hold a leading zero. It holds an integer value, not formatted data.

 
william chaple
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so do I need to convert checker or min into a string or both into a string? can someone offer me a clue on what method to use? parse, valueof, ect
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are lots of ways you could do it...But the best thing for you to do is explain - in English - exactly what you want to do. Don't think about java, Strings, ints, or programming terms. Explain it as if you are talking to a young child what needs to be done. Literally write it out, step by step. Then revise the steps, making them more detailed and more discrete.

When you've done that, post it, and we can talk about how you convert it into code.


 
Ashley Lester
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
william chaple wrote:Ok so do I need to convert checker or min into a string or both into a string? can someone offer me a clue on what method to use? parse, valueof, ect


The issue here is what happens when you mix String and int together.

Try this:



You're assigning the primitive value 1 to a String "0". Remember, code goes left to right, so the String "0" becomes an object. So, the String understands to append the primitive value 1 to "0" because Java is being kind and autoboxing it for you.

If you do this:



It won't work.

The reason for this is that there isn't a String object existing yet. You're trying to assign a primitive integer into a String object, which it doesn't understand.

That is the same behaviour as what's happening in your code.

So, you either need to:

Option 1: Change how you're constructing the time and use a class like SimpleDateFormat. This will solve the problem.
Option 2: Make checker a String (an int will drop the leading zero) and make sure that you're only assigning Strings to it. This will also solve the problem.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!