Results 1 to 8 of 8
  1. #1
    LeeB96 is offline Member
    Join Date
    Oct 2016
    Posts
    6
    Rep Power
    0

    Default How to convert int to String?

    Here is the code:

    public static void ifStatement() throws Exception {

    String Withdraw;
    String Deposit;
    String Inquire;
    String Quit;
    String usersOption;
    int a = 1;
    Integer.toString(a);

    Scanner userInput = new Scanner(System.in);

    System.out.println("What do you want to do?\n1.Withdraw\n2.Deposit\n3.Inquire\n4.Quit\nEnt er your number");

    usersOption = userInput.nextLine();

    if (usersOption == a) {
    System.out.println("****************************** ***********\nWithdrawl\n************************** ***************");

    }
    }}

    So I thought I had declared int "a" as a string in the line Integer.toString(a); but when I try making the userOption equal to a in my if statement it still says that a is an int and it cannot be equal to the string userOption. I'm just wondering if anybody knows where I've gone wrong, thanks in advance! :D

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: How to convert int to String?

    Integer.toString() is a method. So you need to use it
    like this:
    Java Code:
    String s = Integer.toString(a);
    Regards,
    Jim
    Last edited by jim829; 10-24-2016 at 09:24 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    LeeB96 is offline Member
    Join Date
    Oct 2016
    Posts
    6
    Rep Power
    0

    Default Re: How to convert int to String?

    Thanks for the reply, I changed the code so that it is like how you have suggested and I am still encountering the same error unfortunately. Here is the change that I have made:

    public static void ifStatement() throws Exception {

    String Withdraw;
    String Deposit;
    String Inquire;
    String Quit;
    String usersOption;
    int a = 1;
    String s = Integer.toString(a);

    Scanner userInput = new Scanner(System.in);

    System.out.println("What do you want to do?\n1.Withdraw\n2.Deposit\n3.Inquire\n4.Quit\nEnt er your number");

    usersOption = userInput.nextLine();

    if (usersOption == a{
    System.out.println("****************************** ***********\nWithdrawl\n************************** ***************");

    }
    }}

    Any suggestions would be much appreciated

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How to convert int to String?

    You need to use the equals() method to compare two Strings, not the == operator.

    Please edit your post and wrap the code in code tags.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: How to convert int to String?

    Also, the variable 'a' is an int.
    You should be comparing the String.

    In addition, since you only expect numbers to be input, couldn't you just use nextInt() instead? That would mean you would be comparing ints and save on the conversion.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    jack12 is offline Member
    Join Date
    Oct 2016
    Posts
    29
    Rep Power
    0

    Default Re: How to convert int to String?

    The Integer class has a static method that returns a String object representing the specified int parameter. Using this is an efficient solution.

    Syntax
    public static String toString(int i)
    The argument i is converted and returned as a string instance. If the number is negative, the sign will be preserved.

    Example
    int number = -782;
    String numberAsString = Integer.toString(number);

    The code is equivalent to:
    String numberAsString = "-782";

    If you will try and search for solutions, this is one of the most popular ways of converting an int to String.

  7. #7
    jack12 is offline Member
    Join Date
    Oct 2016
    Posts
    29
    Rep Power
    0

    Default Re: How to convert int to String?

    The Integer class has a static method that returns a String object representing the specified int parameter. Using this is an efficient solution.

    Syntax
    public static String toString(int i)
    The argument i is converted and returned as a string instance. If the number is negative, the sign will be preserved.

    Example
    int number = -782;
    String numberAsString = Integer.toString(number);

    The code is equivalent to:
    String numberAsString = "-782";

    If you will try and search for solutions, this is one of the most popular ways of converting an int to String.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: How to convert int to String?

    That's what the OP is doing.
    The problem lies elsewhere in their code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 4
    Last Post: 09-26-2013, 11:35 AM
  2. Convert Int[] to String[]
    By JP34 in forum New To Java
    Replies: 2
    Last Post: 01-05-2013, 09:35 AM
  3. Convert String to int
    By vsriram92 in forum New To Java
    Replies: 8
    Last Post: 06-27-2012, 06:23 PM
  4. Replies: 3
    Last Post: 02-10-2012, 06:26 PM
  5. convert obj to string
    By isme in forum New To Java
    Replies: 11
    Last Post: 06-14-2010, 10:54 AM

Tags for this Thread

Posting Permissions

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