Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# integer to string

Greenhorn
Posts: 20
how could i convert an integer to a string ?

is it possible?

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24211
35
The static method String.valueOf(int) is one way:

int anInt = 5;
String aString = String.valueOf(anInt);

wise owen
Ranch Hand
Posts: 2023
Another way is
int anInt = 5;
String aString = "" + anInt;

Greenhorn
Posts: 20
it does not work

may be i have aproblem with my program

here is the program

_______________________

import javax.swing.*;

public class Calculator

{
public static void main(String[]args)
{
while(true)
{

System.out.println("Enter (2) for substraction");
System.out.println("Enter (3) for multiplication");
System.out.println("Enter (4) for division");
System.out.println("Enter (5) for mod");
System.out.println("Enter (6) for binary");
System.out.println("Enter (7) for exit");

}

if (choice==7)
{
System.exit(0);
}

switch (choice)

{

case 0:

choice=1;
String Choose= String.valueOf(choice);

{
choice=1;
String Choose= String.valueOf(choice);
double value=0;
double value1=Double.parseDouble(JOptionPane.showInputDialog("Enter the nomber you want to calculate"));
double value2=Double.parseDouble(JOptionPane.showInputDialog("Enter the nomber you want to calculate"));
value=value1+value2;
double sum=0;
double temp=0;

while (!Choose.equals("="))

{
temp=value;
sum+=temp;

}

}
else

{
System.out.println(sum);
System.exit(0);

}

}

}
}

[ February 04, 2006: Message edited by: tayy abdelqader ]
[ February 04, 2006: Message edited by: tayy abdelqader ]

Bimal Patel
Ranch Hand
Posts: 130
Hi,

Can you post the compilation error u're getting?

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24211
35
Well, there are all sorts of interesting things going on here, but converting between Strings and ints isn't really the major issue. The worst problem is that the initial "while (true)" loop encloses only the printing of the menu -- so the menu will be printed over and over, non-stop, until you kill the program.

When you do use String.valueOf(), it's in the following two lines

choice = 1;
String Choose = String.valueOf(choice);

which is really just exactly the same as

String Choose = "1";

Greenhorn
Posts: 20
Calculator.java:35: cannot resolve symbol
symbol : variable Choose
location: class Calculator
if ((Choose.equals("1"))||(Choose.equals("+"))||(Choose.equals("
^
Calculator.java:35: cannot resolve symbol
symbol : variable Choose
location: class Calculator
if ((Choose.equals("1"))||(Choose.equals("+"))||(Choose.equals("
^
Calculator.java:35: cannot resolve symbol
symbol : variable Choose
location: class Calculator
if ((Choose.equals("1"))||(Choose.equals("+"))||(Choose.equals("
^
Calculator.java:61: cannot resolve symbol
symbol : variable sum
location: class Calculator
System.out.println(sum);
^

Greenhorn
Posts: 20
thanx Ernest Friedman-Hill

i think the while(true) just declare the 7 lines u can say it is as a help menu

i hope i am right

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24211
35
The error messages are complaining about "Choose" because you're using it in the condition of the "if" statement several lines before you actually declare it, inside the loop. You can't use a variable before you define it.

Greenhorn
Posts: 20
i do not think so

because it is in the if statement and the loob also in the if statement
so no problem here

i think !!!

Garrett Rowe
Ranch Hand
Posts: 1296
Ernest is right. Right here:

You are using the variable Choose two lines before you declare it. You have to move the declaration outside the if statement if you want to use it in the if conditional clause.

Greenhorn
Posts: 20
thanx u are right

any how i stil have the following problem

Calculator.java:61: cannot resolve symbol
symbol : variable sum
location: class Calculator
System.out.println(sum);

Rusty Shackleford
Ranch Hand
Posts: 490
Did you declare sum?