Hmm. Why can't I ouput this integer?

Soldato
Joined
12 Sep 2003
Posts
11,220
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);
		
		
	}
}
 
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:
 
It works for me:

Code:
public class Test {

    public static void main(String[] args) {

        StringBuffer sb = new StringBuffer("1");
          
        String s = sb.toString();
         int i;
	i = Integer.valueOf(s);

        System.out.println(i);
       
        
    }

}

Code:
Output: 1
 
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:

You are confident that buf contains what you are expecting?
 
I can't see why that doesn't work, tried it just and it seems to run fine, I'm not using J2ME though which could be the problem.

You are confident that buf contains what you are expecting?

Yeah, remember that the toString() method can be called on everything so is buf actually holding what you expect? The best way to test it to put a print statement to check it.

Code:
String s = buf.toString();
out.print("buf.toString() = "+s);
 
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!!
 
Out of interest is there any Java method that will display special characters hidden in a string such as '\r' '\t' '\n' ?
 
Back
Top Bottom