Results 1 to 13 of 13
  1. #1
    matpj is offline Member
    Join Date
    Sep 2008
    Posts
    42
    Rep Power
    0

    Default trying to convert an interger to a string

    Hi all,

    I have an integer used as a counter in my WHILE loop.
    I need to convert this to a string

    I have tried defining a string called 'counter';
    my integer is called 'count'

    I have tried counter.toString(count)
    but this does not work as the toString is underlined and it pops up the message "cannot find Symbol: method toString(int)"

    can anybody advise?

    thanks,
    Matt

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    if you "integer" is Integer, you can use counter.toString()
    if you "integer" is int, you can use String.valueOf(count) or Integer.toString(count)
    Last edited by mtyoung; 01-14-2009 at 11:29 AM.

  3. #3
    matpj is offline Member
    Join Date
    Sep 2008
    Posts
    42
    Rep Power
    0

    Default

    Quote Originally Posted by mtyoung View Post
    if you "integer" is Integer, you can use counter.toString()
    if you "integer" is int, you can use String.valueOf(count) or Integer.toString(count)
    Thanks for that.
    I'm having problems where it complains that the variable may not have been initialized.


    here is my code.
    I declare the 'String counter;' at the beginning.
    and the 'Int count;'

    i'm trying counter.valueOf(count) and it is returning the "variable may not have been initialized error"

    Java Code:
      public static void main(String args[]) throws SQLException, IOException {
    
            DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
            int count = 0;
            String counter;
            String serverName = "francisca";
            int port = 1521;
            String user = "niku";
            String password = "niku";
            String SID = "nikuuat";
            String URL = "jdbc:oracle:thin:@" + serverName + ":" + port + ":" + SID;
    
            Connection conn = DriverManager.getConnection(URL, user, password);
            String SQL = "SELECT DSTI_PROJ_REF, id FROM NIKU.ODF_CA_PROJECT where dsti_proj_act = 'ADM'";
            String newLine = System.getProperty("line.separator");
    
    
            Statement stat = conn.createStatement();
            ResultSet rs = stat.executeQuery(SQL);
            FileWriter fout = new FileWriter("c:\\test.txt"); //Create new file stream
            
            while (rs.next()) { //cycle through result set
                 count++;
                 counter.valueOf(count);
    
                 fout.write(
                          padLeft(counter, 8) +
                          padRight(rs.getString(1), 11) +
                          rs.getInt(2) + newLine
                          
                          );
    
            }
    
            fout.close();  //close file stream
            System.out.println("Complete");
            stat.close();
            conn.close();
    
      }
    am I making a really stupid mistake?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    To begin with, String#valueOf is a static method. Static methods are to be invoked on the class, not an instance of the class.

    Secondly, the method returns a String. Your code does not attempt to assign this return value to a variable.

    And your error is caused because the method-local variable "counter" is declared and not initialized.

    IMHO the easiest way to obtain a String from an int is simply to use the language feature that allows concatenation.
    Java Code:
    String counter = "" + count;
    db

  5. #5
    PeterFeng is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default

    Since this is asked, can I do this?

    int num2Txt=2
    num2Txt= " "
    System.out.println("showing number or space:" +num2Txt")

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    rather than ask, you should try it yourself first and find out if you can do this.

  7. #7
    PeterFeng is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default

    tried, didn't work.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Good. So now you know that
    1) you can't assign a String to an int variable, and
    2) how to answer some basic questions on your own.

    Keep on truckin'

  9. #9
    matpj is offline Member
    Join Date
    Sep 2008
    Posts
    42
    Rep Power
    0

    Default

    perfect, that works well thanks.

  10. #10
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Better to use Integer.toString(int), as fits the function of Integer.

    It's a good practice to initialize your variables, even if you set them to null.

    You can invoke a static method from an instance variable, but it is not good style, and it can lead to problems.

    As noted above, the compiler will automatically invoke toString() when you use the + operator to concatenate String and other objects.

    In your program, you really don't need counter. Just use
    Java Code:
                 fout.write(
                          padLeft([B]Integer.toString(count)[/B], 8) +
                          padRight(rs.getString(1), 11) +
                          rs.getInt(2) + newLine
                          );

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    > As noted above, the compiler will automatically invoke toString() when you use the + operator to concatenate String and other objects.
    AFAIK it invokes String.valueOf(...)

    db

  12. #12
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by Darryl.Burke View Post
    > As noted above, the compiler will automatically invoke toString() when you use the + operator to concatenate String and other objects.
    AFAIK it invokes String.valueOf(...)

    db
    Correct. It invokes String.valueOf(), which in turn, usually invokes the provided Objects toString().

    So, in a roundabout sort of way, your both right. ;-)

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

Similar Threads

  1. How to convert a string into a BigInteger
    By valery in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 01:32 PM
  2. how to convert String number to int
    By gabriel in forum New To Java
    Replies: 5
    Last Post: 08-02-2009, 03:46 PM
  3. convert a really big string (len 39) to bigdecimal
    By coolsig in forum Advanced Java
    Replies: 6
    Last Post: 06-14-2008, 02:48 AM
  4. convert string to a double?
    By javaMike in forum Advanced Java
    Replies: 2
    Last Post: 11-27-2007, 03:10 AM
  5. convert string to float
    By miss_dot in forum NetBeans
    Replies: 1
    Last Post: 11-14-2007, 11:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •