Convert an int to a String

Hi,
when i try to do the following its an error
int totalmarks;
String Totalmarks;
Totalmarks = totalmarks.toString();
could any one tell me how to do it correctly
Thank you

A lot of alternatives, but you can't do new String(int..).
1. Integer.toString(totalmarks)
2. String.valueOf(totalmarks) will call Integer.toString(totalmarks)
3. ""+totalmarks will use the StringBuffer and append "" and totalmarks which again will call String.valueOf(totalmarks) which again will call Integer.toString(totalmarks)

Similar Messages

  • How can I convert an int to a string?

    Hi
    How can I convert an int to a string?
    /ad87geao

    Here is some the code:
    public class GUI
        extends Applet {
      public GUI() { 
        lastValue = 5;
        String temp = Integer.toString(lastValue);
        System.out.println(temp);
        showText(temp);
      private void showText(final String text) {
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            tArea2.setText(text + "\n");
    }

  • How do I convert an int to a String?

    How do I convert an int to a String?

    You can also use any of these methods if you need to get more complicated:
    Integer.toString(int i)
    Integer.toBinaryString(int i)
    Integer.toHexString(int i)
    Integer.toOctalString(int i)
    Integer.toString(int i, int radix)

  • Converting an int to a string

    i want to know how to convert an int to a string .
    I have tried toString() but it says can't dereference an int.
    any ideas ????
    thanks

    What I mean by the object being null is, say for example, you have the following method:
       public String combine(Object o1, Object o2){
          return o1.toString() + o2.toString();
       }This method will throw a NullPointerException if either or both o1 and/or o2 are null. If you use it like so:
       public String combine(Object o1, Object o2){
          return String.valueOf(o1) + String.valueOf(o2);
       }This will always work. (I know someone out there would say that this would return a String like "nullnull" if both are null, and so forth and so on, but hey, you get my drift.) I'm also not saying the you can't not check if either o1 or o2 is null before proceeding, so the following also works:
       public String combine(Object o1, Object o2){
          String s = null;    // I'm using this instead of StringBuffer for
                              // simplicity's sake so don't get this wrong
          if (o1 != null){
              s = o1.toString();
          if (o2 != null){
              s += o2.toString();
          return s;
       }As you can see, there's no right or wrong way in programming as long as you achieve the result. The only thing that would matter is how clean your code is, how efficient your code is, and how maintainable your code is.

  • How to convert an int array to string array?

    Hi,
    Can anyone please help me with this question?
    int number={4,6,45,3,2,77};
    I like to convert this into a list and sort
    List mylist = Arrays.asList(number);
    Collections.sort(mylist);
    But first I need to convert the int array to String array.
    Please advise. Thanks.

    If you want to convert your int array to a String array, you have no choice but doing a conversion element by element in a for loop.
    However, if the sort method doesn't behave as desired, you can use the one with 2 parameters, the second parameter being a comparator class.
    Check the javadoc for more information : http://java.sun.com/j2se/1.3/docs/api/java/util/Collections.html
    /Stephane

  • How to convert an int variable into String type

    hi everybody
    i want to know how to convert an interger variable into string variable
    i have to implement a code which goes like this
    Chioce ch;
    for(int i=0;i<32;i++)
    // here i need a code to convert the int variable i into a string variable
    ch.add(String variable);
    how do i convert that int variable i into a String type variable??
    can anyone help me?

    Different methods:
    int a;
    string s=a+"";or
    String.valueOf(int) is the better option because Int.toString() generated an intermediate object to get the endresult
    Ema

  • How to convert an int to a String

    Hi, just wanting to know is anyone could help me with convertin a primitive int into a String, currently I have
    String mod = Integer.valueOf(n);
    but with this i keep getting an error which says, - cannot resolve symbol - method valueOf(int)
    Can anyone please help me. thanks.

    it should be
    String mod = Integer.toString(n);

  • Converting type INT to type String...possible?

    Hi
    I basically have a stored string that I want to add numbers to, like adding a phone numbers digits to eachother or a calculators numbers to eachother in seperate instances. Is this possible?
    So say I ask the user to input a number (single), can this then be added to the string field and then perhaps looped (dont tell me how to do that - I will try that myself) to create a string of 10 or more numbers?
    Thanks for any help

    Thanks but I have to start with a param of INT before
    adding it to the string, and I cant seem to do this
    with casting (String) FIELDNAME - I get the
    Inconvertible types error[url http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html#toString(int)]Integer.toString()

  • Trying to convert a int back to a textfield - help

    Okay this is what I want. I want to type a number in a TextField and double it.... So lets say I type in 10, it will output 20.. But I cant seem to get it to work. The integer won't change it back to a textfield usable text thing .. any help? And yes, i'll admit, its for school, not really for marks, but just to learn.. Theres like 10 of them. And I've been trying to get it to work for awhile, and I ain't asking for coding, or cheating. I'm just asking for help :)
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Myname extends Applet implements ActionListener
         Button pressMe = new Button("Enter a number");
         TextField answer = new TextField(10);
         Font bigfont = new Font("TimesRoman", Font.ITALIC,24);
         public void init()
              add(pressMe);
              add(answer);
              answer.requestFocus();
              pressMe.addActionListener(this);
         public void actionPerformed(ActionEvent thisEvent)
              int doubleamount = (new Integer(answer.getText())).intValue();
              int doubleamount2 = doubleamount + doubleamount;
              answer.setText(doubleamonut2);
              invalidate();
              validate();
    Thats what I have

    You need a String.valueOf (doubleamount2)to convert your int to a String, and you don't need
    the revalidate ().
    Kind regards,
    LeviYeah, Levi is correct. :-)

  • 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;

  • 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()

  • How to convert a "text variable" to String with plugin

    Hello,
    I am currently developing a InDesign (CS5) plugin, where I need to manipulate text variable.
    From the plugin I want to convert the "text variable" to string (in principle I should use the method "VariableToString (..)" of "ITextVariable")"
    My problem is, how to find the text varial from its name (I think I should use the method "FindLocationsUsed(..)" of "ITextVariableSuite") and then convert it to String
    I don't know how to use interfaces ITextVariable and ITextVariableSuite
    Plugin implemented in C++ language
    thank you

         InputStreamReader in=new InputStreamReader(fis);
          StringWriter out=new StringWriter();
          char[] buffer=new char[8192];
          int sizeRead;
          while ( ( sizeRead=in.read(buffer, 0, 8192) ) != -1 )
            out.write(buffer, 0, sizeRead);
         String content=out.toString();

  • Convert from int to char

    Hi,
    How can I convert from an int to char?

    The challange is to convert from int to String without using any API or automatic conversionWoah, what a challenge ! Why ?
    Anyway, what about a recursive call and switch statement. Something likepublic String intToString(int n) {
        if(n<0) {
            return "-"+intToString(-n);
        if(n<10) {
            return getStringDigit(n);
        else {
            return intToString(n/10) + getStringDigit(n%10);
    private String getStringDigit(int n) {
        switch(n) {
            case 0:
                return "0";
            // etc.
    }

  • Converting encoding of a Stream/String

    Hi,
    I am wondering if there is a way we can convert encoding of a Stream/String. For example, I have an Stream/String having iso-8859-1 encoding and I want to convert it to utf-8 encoding. Is there any way to do that conversion?
    I figured out a way, but that is expensive to do. I build a JDOM document out of that InpuStream, and use Outputter to output the document into an OutputStream/String setting the encoding. The resultant OutputStream/String has the new encoding. Here is what I do for this:
    Document doc = getDocumentFromStream(inputStream);
    XMLOutputter outputter = new XMLOutputter();
    //default encoding in JDOM is "UTF-8", if no encoding is specified, encoding
    //is not set and thus defaults to "UTF-8"
    if(StringUtils.isNotEmpty(encoding)){          
         outputter.setEncoding(encoding);
    outputter.setTextTrim(true);
    outputter.output(doc, out);
    I am wondering if there is any way we can do it without using JDOM thing, which is expensive.
    I will appreciate any help on this issue.
    Thanks in advance,
    Pramodh.

    Hi,
    Thanks for the reply!
    The reason, I want to convert the encoding of a Stream is: Our application supports UTF-8 very well. So, incoming data in non-UTF-8 encoding may most probably fail. There are characters like �, TM, copyright, etc which are supported by UTF and also ISO-8859-1. So, if I change the incoming ISO-8859-1 Stream into UTF-8, then the application will like resultant UTF-8 data.
    I tried what u suuggested, but stil can't get it work. Following is what i did:
    reader = new InputStreamReader(inStream, "ISO-8859-1");
    writer = new OutputStreamWriter(outStream, "UTF-8");
    int n;               
    while ((n = reader.read(c)) > 0) {
    writer.write(c, 0, n);
    Any other suggestions?
    Thanks.

  • Converting a long to a string  (PLEASE HELP)

    I am trying to convert a long to a string so that I can put it into a vector. I have this:
    long fileSize = 0;
    String fileModDate = null;
    Vector fileList = new Vector();
         ~~~~~~~
    File [] files = myDir.listFiles();
    for (int i = 0; i<files.length; i++){
    fileSize = files.length();
    String s = fileSize.toString();
    //do the same for the mod date
    fileModDate = files[i].lastModified();
    When i try this, i get an error that long can not be dereferenced. Can anyone please help?

    There are two ways to solve this problem. You are trying to convert a primative type to a string without using its wrapper function (Long). So if you created a long variable as a (Long) object you could call its toString() method and this would work...
    The other way is to simply use the static method in the String class valueOf(long) as follows:
    String longString = String.valueOf(i);Hope this helps.
    Mark

Maybe you are looking for

  • How to bring a objects on a Canvas to the foreground

    I have a list objects on a canvas. How do I manipulate the objects as to make one of them "ontop" of the other (i.e. bring it to the foreground). Thanks for your help

  • Videos are not transfering.

    I have tried taking everything off manual in my settings but none of my videos are transfering to my ipod! It says that my ipod has finished updating but when I look at the video section on my ipod nothing shows up. I know they are configured correct

  • Using Newly Installed Font in Applications

    I recently installed Concielian font. It is in the Library and in User. It appears in the list. However, I cannot access it in any application including Text Editor. Please help.

  • Modify the errors messages

    Plz tell me could I modify the error messages file from English into National Language. (Such as modify the hotkey use Oracle Terminal)

  • How do I temporarily disable the pop-up function?

    How do I temporarily disable the pop-up function on my Mac desktop?