Converting int to String

How can we convert from int to String?
Ex:
int i = 5;
Then i need that 5 in String format....
Message was edited by:
kiran_panga

String.valueOf(i)
According to http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html
public static String valueOf(int i)
Returns the string representation of the int argument.

Similar Messages

  • Convert int to string!!!!

    Ok, Now I'm having problems converting an int to a string!!
    I need to be able to display the value of an integer value using the drawstring method of the Graphics class in an applet!
    If anyone could help that would be great...
    Also, while i'm on here, I could do with some help with the following...
    I need to take a comma seperated string of values, passed in from a web page, and split it into an array of integers... i.e "1,2,3,4,5" would become
    int myArray[0]=1
    int myArray[1]=2
    int myArray[2]=3
    etc etc
    I would imagine that there is a built in method somewhere to do this - I am so far very impressed with the functionality that Java seems to offer 'out-of-the-box' - it's just a matter of finding the information! I guess it can only come with experience, like any other computer language (yesterday was my first day with Java!)
    Regards,
    Mark Wills (UK)

    Thanks once again John!
    Mark
    Converting an int to a String can be done in many
    ways, (just preparing myself against the posts that'll
    come next), but this'll work: int i = 0; String s =
    ""+i;
    Breaking up your String will require the use of the
    java.util.StringTokenizer class. Very simple indeed
    and if you follow the javadoc you'll also find
    yourself exposed to a number of other interfaces /
    classes that'll come in useful later.
    Hope this helps.
    J

  • Converting int to string, adding to string then pointing to an url

    So I'm getting a null pointer exception here's a snippet of my code (theres plus signs on both sides of the a in my code):
    private Image[] tiles;
    public Map() {
    for(int a=0; a<9; a++){
    ImageIcon aa = new ImageIcon(this.getClass().getResource("mapsquares"+a+".png"));
    tiles[a] = aa.getImage();
    I've also tried:
    private Image[] tiles;
    public Map() {
    for(int a=0; a<9; a++){
    ImageIcon aa = new ImageIcon(this.getClass().getResource("mapsquares$s.png", String.valueOf(a)));
    tiles[a] = aa.getImage();
    whats wrong with either of these?
    Edited by: Wub on Feb 28, 2013 3:17 PM

    1.) You should use code tags when posting code.
    2.) You should read the stacktrace closely to find out which exact line number in your code threw the exception.
    My guess is that it's the line tiles[a] = aa.getImage(); because you didn't initialize the array tiles using the new operator. This is just a guess since you didn't post the full stacktrace. Also, NullpointerExceptions are one of the easiest to debug. Just adding print out lines at strategic values can show you which variables are initialized and which ones are not.

  • Can't convert int into String

    I'm trying to code a GUI that will give an employee id, name, salary, hire date, and vac days.
    public String getFieldValue(int fieldIndex) {
              switch (fieldIndex){
              case 0: return employeeID;
              case 1: return firstName;
              case 2: return lastName;
              case 3: return empIndicator;
              case 4: return salary;
              case 5: return hireDate;
              case 6: return vacDate;
              return null;
    My salary and vacDate are double and int. How do I display all the fields?
    Thanks

    I would use enums here, not magic numbers.
    Are you trying to extract the text held in JTextFields? If so, perhaps returning String is all you need. This you can parse via the wrapper classes (i.e., Integer.parseInt(...) and Double.parseDouble(...))

  • How to convert from int to string

    Can anyone help me on how to convert int to string?
    Thanks,
    Q

    int i = 3
    String S = i + ""
    i will be promoted to String automatically when the above expression is evaluated

  • Convert from int to String

    Hi, may i know how to convert from int to String??
    For instance, i've>>
    int i = 0;
    i++;
    String temp = i.toString();
    [Need to convert the int to String here. Am i doing the convertion correctly?? Pls correct me. Thanks!]

    Hi, may i know how to convert from int to String??
    For instance, i've>>
    int i = 0;
    i++;
    String temp = i.toString();
    [Need to convert the int to String here. Am i doing
    the convertion correctly?? Pls correct me. Thanks!]String temp = "" + i;

  • Can u tell me how to convert int value in to string

    hi to all, can u tell me how to convert int value in to string.

    hi to all, can u tell me how to convert int value in
    to string.Even this way:
    int number = 155;
    String mystring = ""+155;
    [\code]                                                                                                                                                                                                                                                                                               

  • Convert an int in string

    I wrote this code to convert an int in string but is failed.
    Someone might help me. thanks
    String UID="select max(right(CodiceUtente,4)) from Utenti";
         ResultSet rs=st.executeQuery(UID);
         int massimo=rs.getInt("CodiceUtente") + 1;
              //massimo deve essere trasformato in una stringa. Come si fa?
         String maxim =toString();
    my email is [email protected]

    Try this one: String maxim = toString(massimo).
    Find out more from the API (hope it accepts this long
    URL that should point directly to the right place) :
    http://www.ttu.ee/it/vorgutarkvara/wai4040/jdkdoc/jdk1.
    2.2/docs/api/java/lang/String.html#toString()

  • String to Int and Int to String

    How can I convert a string to Int & and an Int to String ?
    Say I've
    String abc="234"
    and I want the "int" value of "abc", how do I do it ?
    Pl. help.

    String.valueOf(int) takes an int and returns a string.
    Integer.parseInt(str) takes a string, returns an int.
    For all the others long, double, hex etc. RTFM :)

  • Conversion of int to String

    hi,
    Do someone know which way of converting the int to String is better? In term of performance and other issues in the handset:
    1. int a = 5;
    String b = a + "";
    2. int a = 5;
    String b = String.valueOf(a);
    Thank you!

    Hi,
    The second method is better. Because for the first method 3 String objects will be created.

  • Converting a given string to non-english language

    {color:#0000ff}Hi can anybody help me how to convert an entered string in textfield to french or spanish or to anyother non-english language?{color}

    Hi,
    I don't think you get a language translator package.
    What you can do is store the fraises, words in a database.
    //SQL Code
    CREATE TABLE [Language_Data]
      [ID]    INT NOT NULL IDENTITY PRIMARY KEY,
      [Lang]  VARCHAR(30) NOT NULL,                             //Lang English/French.....
      [Type]  CHAR(1) NOT NULL,                                 //is Fraise or Word
      [Words] VARCHAR(100) NOT NULL                             //Fraise or Word data
    GO
    CREATE TABLE [Translate]
      [ID]       INT NOT NULL IDENTITY PRIMARY KEY,
      [FK_Orig]  INT NOT NULL REFERENCES [Language_Data]([ID]), //ID of the original language
      [FK_Trans] INT NOT NULL REFERENCES [Language_Data]([ID])  //ID's for all known translations
    GO Create Stored procedures to add a new word/fraise to the [Language_Data] table,
    Create a Stored Procedure to add a translation to the [Translate] table
    Please note that to add a translation you will first insert into the [Language_Data] table then
    insert the original's ID and the translation ID to the [Translate] table Also make prevision for backwards translation

  • Converting objects into Strings

    How would someone convert an object of say (String, int, double, String) into a readable string. I tried the toString() method but all I get is something like this
    Student@1f12c4e

    ..I'm not sure I understand "how" to override. The whole point of this project is to use quicksort on a list of students, unfortunately all I get is the address whenever I use the .toStrings() method.
    Here's what I have, any help would be greatly appreciated-so very close
    import cs1.Keyboard;
    import java.io.*;
    import java.util.*;
    public class StudentTraverse
    public static void main(String[] args)
    String newName;
    int newSocial;
    double newGPAs;
    String newMajors;
    System.out.println("How many Students would you like to add");
    Student newStudent;
    StudentList12 WORK = new StudentList12();
    int total = Keyboard.readInt();
    for(int number = total; number > 0; number--)
    System.out.println("Name?");
    newName = Keyboard.readString();
    System.out.println("Social?");
    newSocial = Keyboard.readInt();
    System.out.println("GPA?");
    newGPAs = Keyboard.readDouble();
    System.out.println("Major?");
    newMajors = Keyboard.readString();
    newStudent = new Student(newName, newSocial, newGPAs, newMajors);
    System.out.println("Inserting: "+newStudent.toString());
    WORK.add(newStudent);
    for(total = 0; total < WORK.size(); total++)
    System.out.println("top" total": "+WORK.top(total).toString());
    try
    BufferedReader in = new BufferedReader(new FileReader("LIST.out"));
    while (in.ready())
    // Print file line to scree
    System.out.println (in.readLine());
    in.close();
    catch (Exception e)
    System.err.println("File input error");
    public class StudentNode
    public Student student;
    public StudentNode next;
    public StudentNode()
    next = null;
    student = null;
    public StudentNode(Student d, StudentNode n)
    student = d;
    next = n;
    public void setNext(StudentNode n)
    next = n;
    public void setData(Student d)
    data = d;
    public StudentNode getNext()
    return next;
    public Student getData()
    return data;
    public String toString()
    return ""+data;
    public StudentNode(Student newStudent)
    METHOD NAME: StudentNode
    AUTHOR:
    DATE OF CREATION: Nov 20, 2004
    DATE OF UPDATES: Nov 28, 2004
    PURPOSE: Acts as a node for the Student list
    ALGORITHM:Acts as node for the list
    INSTANCE VARIABLES: none
    student = newStudent;
    next = null;
    public class Student
    private String name;
    private int social;
    private double GPA;
    private String Major;
    public Student(String newName, int newSocial, double newGPAs, String newMajors)
    METHOD NAME: Student
    AUTHOR:
    DATE OF CREATION: Nov 20, 2004
    DATE OF UPDATES: Nov 28, 2004
    PURPOSE: The actual Student class, determines what is allowed in the array
    ALGORITHM:Declare what variables will be needed for the program
    INSTANCE VARIABLES: String name, int social, double GPA, String Major
    name = newName;
    social = newSocial;
    GPA = newGPAs;
    Major = newMajors;
    import java.io.*;
    import cs1.Keyboard;
    import java.io.BufferedWriter;
    import java.util.*;
    public class StudentList12
    private StudentNode list;
    static int i = 0;
    public StudentList12()
    METHOD NAME: StudentList12
    AUTHOR:
    DATE OF CREATION: Nov 20, 2004
    DATE OF UPDATES: Nov 28, 2004
    PURPOSE: Declares the Node
    ALGORITHM:Declare the Node
    INSTANCE VARIABLES: none
    list = null;
    public boolean isEmpty()
    return list == null;
    public int size()
    return i;
    public void add(Student newStudent)
    METHOD NAME: add
    AUTHOR:
    DATE OF CREATION: Nov 20, 2004
    DATE OF UPDATES: Nov 28, 2004
    PURPOSE: Let's users add objects to the array of objects
    ALGORITHM:Traverses the current list and adds object to the end
    INSTANCE VARIABLES: none
    list = new StudentNode(newStudent, list);
    i++;
    current = current.next;
    current.next = node;
    public Student remove()
    if(isEmpty())
    return null;
    StudentNode tmp = list;
    list = tmp.getNext();
    i--;
    return tmp.getData();
    public void insertEnd(Student newStudent)
    if(isEmpty())
    add(newStudent);
    else
    StudentNode t = list;
    while(t.getNext() != null)
    t=t.getNext();
    StudentNode tmp = new StudentNode(newStudent, t.getNext());
    t.setNext(tmp);
    i++;
    public Student removeEnd()
    if(isEmpty())
    return null;
    if(list.getNext() == null)
    return remove();
    StudentNode t = list;
    while(t.getNext().getNext() != null)
    t = t.getNext();
    Student newStudent = t.getNext().getData();
    t.setNext(t.getNext().getNext());
    i--;
    return newStudent;
    public Student top(int n)
    StudentNode t = list;
    for(int i = 0; i <n && t != null; i++)
    t = t.getNext();
    return t.getData();
    public void writeToFile(int n)
    int z = n;
    try
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("LIST.out")));
    for(int counter = z; counter >= 0; counter--)
    System.out.println(counter);
    out.close();
    catch(Exception e)
    System.err.println("Couldn't Write File");
    try
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("LIST.out")));
    out.write(list.toString());
    out.close();
    catch(Exception e)
    System.err.println("Couldn't Write File");
    }

  • Can't convert int to java.lang.Integer.

    Hi everyone.
    I'm working on an auction site at the moment and have problems gaining a bidID number. This is part of my code and my error.
    AuctionFacade aHse = (AuctionFacade) application.getAttribute("AuctionFacade");
    String strTraderName = (String)session.getAttribute("traderName");
    String strPassword = (String)session.getAttribute("password");
    String strIDNum = request.getParameter("auctionID");
    Integer intID = Integer.valueOf(strIDNum);
    Integer [] bids = aHse.getAllBids(intID);
    float fltYourBid = aHse.getBidPrice(bids.length);
    ^
    Can't convert int to java.lang.Integer.
    can anyone help please?

    So, does "aHse.getBidPrice" expect an int or an Integer as its parameter? And does it return an int, or an Integer, or what? The answer to those questions may lead to your solution -- look at what you are actually using as the parameter, for example.

  • Converting Array of string to an array of integers

    I have a problem converting a array of string to array of int's
    This is my code...
    String[] forminfo = request.getParameterValues("forsendur");
          int[] forminfoInt = Integer.parseInt(forminfo); This is the error message:
    Incompatible type for method. Can't convert java.lang.String[] to java.lang.String.
                          int[] forminfoInt = Integer.parseInt(forminfo);
                                                               ^can anyone help me with this?

    ParesIn methos returns a int buto not a int[]. You must iterate along the String array and perform the methos for each element setting the return value into the element of the int array
    Ej:
    int[] intArray = new int[stringArray.length]
    for(int i = 0; i < stringArray.length; i++)
    intArray[i] = Integer.parseInt(stringArray);

  • Ints to Strings

    Can someone please tell me how I can convert an 'int' into a 'String'??
    The problem i am having is that i want to use some 'int' values and display them in a GUI TextField but I cant coz they are ints and a TextField only accepts Strings.
    So i want to be able to use the setText( ) method to do this after the conversion.
    Thanx.
    [K]

    then you look up javadocs,
    Integer:
    valueOf
    public static Integer valueOf(String s)
    throws NumberFormatException
    Returns an Integer object holding the value of the specified String. The argument is interpreted as representing a signed decimal integer, exactly as if the argument were given to the parseInt(java.lang.String) method. The result is an Integer object that represents the integer value specified by the string.
    In other words, this method returns an Integer object equal to the value of:
    new Integer(Integer.parseInt(s))
    Parameters:
    s - the string to be parsed. Returns:
    an Integer object holding the value represented by the string argument. Throws:
    NumberFormatException - if the string cannot be parsed as an integer.
    intValue
    public int intValue()
    Returns the value of this Integer as an int.
    Specified by:
    intValue in class Number
    Returns:
    the numeric value represented by this object after conversion to type int.
    String:
    suggestions above.

Maybe you are looking for

  • Must-know BlackBerry Z10 and BlackBerry 10 tips and tricks

    Check out the following must-know BlackBerry Z10 and BlackBerry 10 tips and tricks to get the most out of your new BlackBerry 10 smartphone. 1) BlackBerry 10's Built-In Screen Shot Feature Capturing a screen shot on a BlackBerry 10 device is simple.

  • Audio switching using optical connection

    I am directly connecting to my 2005-era Denon receiver (AV3805, no HDMI) using an optical connection. With the ATV2 "Dolby Digital" setting to 'On', I can get Dolby Digital (5.1) from movies on iTunes, but I'm wondering if the Denon unit is capable o

  • Direct input data

    Hi , What are some sample Direct input data transfer programs? Thanks, Manjula

  • Windows 2012 Datacenter R2 - Network Connections Confusion

    I have recently created a 2012 Datacenter install with the goal to create virtualize test lab.  As part of the install two connections were created: 1. Labeled 'Ethernet' with Hyper-V Extensible Virtual Switch protocol enabled. 2 Labeled 'Network 2'

  • Bug report: import image failure

    When I try to import images (formatted as either .gif or .jpg) some fail to load and a blank outline of the appropriate dimensions appears. Images that fail to appear have been set to grayscale and/or RGB, and have a file size less than 200 KB.