Hmm. Why can't I ouput this integer?

Soldato
Joined
12 Sep 2003
Posts
11,249
Location
Newcastle, UK
:o

It looks to be real simple but everytime I run my code, I don't get the output on the screen.

String s = buf.toString();
out.println(s);


Works fine, and I get my number on the screen. Now I want to convert this to an int so that I can perform some calculations on it, so I did,

int i = Integer.parseInt(s);
out.println(i);


... but sadly, nothing. :(

Any ideas please? Many Thanks!
 
Cheers for that, it still outputs nothing though. :(

I have found this code on the net which I tried,

Code:
int i
try 
{
  i = Integer.parseInt(s);
  out.println(i);
} catch (NumberFormatException e)
{
  out.println("String is not a number!");
}

... and it prints out the error message. Hmm, I don't understand. The only data the String holds is numbers? i.e. 00003459 or 000003222, etc, etc.

Why can't I convert it? :(

Thanks!
 
Are you programming in Java? It should work :/

Try this using valueOf() instead:
Code:
class Test 
{
	public static void main(String[] args) 
	{
		String s;
		int i;
		s = "1";
		i = Integer.valueOf(s);
		//i = Integer.parseInt(s);
		System.out.println(i);
		
		
	}
}

It's J2ME mate, so pretty similar. ;)

I tried the above and when I declared my String value in the code myself, i.e. as "1", it outputted it fine. If I output direct from the StringBuffer as well it outputs the number on the screen.

So I then used the same code above and passed the string that I read out of the StringBuffer and again (to try and convert it to int), no output again. :confused:

Everywhere I look on the web people say just pass the String to .parseInt() and it should work....

Cheers again though!! HELP! Lol.
 
So yeah, this is what I tried...

Code:
	String s;
	int i;
	s = "1";
	i = Integer.valueOf(s);
           out.println(i);

This gave me 1. :) So I then changed it back so it took my buffer value, converts it to a String for what I needed...

Code:
	String s = buf.toString();
	int i;
	i = Integer.valueOf(s);
           out.println(i);

... and no output. Just will not convert it to an integer. Can quite happily just do,

Code:
out.println(s);

but as far as I know, how would I then perform arithmetic calculations on a string? It needs to be an int yeah?

:confused:
 
Hi guys

Thanks for the replies again, I found out my problem just the other day so thought I'd share it with you all. It turns out the reason I was having so much bother with the string in the buffer was that the value I was reading in had a "\t" before the value, i.e. white space.

I removed this (I must have just put it in by mistake) and it can output fine now.

:D

Thanks again!!
 
Back
Top Bottom