Two Laptop Bag*
The moose likes Beginning Java and the fly likes integer to string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "integer to string" Watch "integer to string" New topic
Author

integer to string

tayy abdelqader
Greenhorn

Joined: Dec 11, 2005
Posts: 20
how could i convert an integer to a string ?

is it possible?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

The static method String.valueOf(int) is one way:

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


[Jess in Action][AskingGoodQuestions]
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Another way is
int anInt = 5;
String aString = "" + anInt;
tayy abdelqader
Greenhorn

Joined: Dec 11, 2005
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 (1) for addition");
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");

}

int choice=Integer.parseInt(JOptionPane.showInputDialog("Enter your choice here"));


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

switch (choice)

{

case 0:

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

if ((Choose.equals("1"))||(Choose.equals("+"))||(Choose.equals("add")))
{
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;

Choose=JOptionPane.showInputDialog("Enter your choice here");


}

}
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

Joined: Aug 29, 2003
Posts: 130
Hi,

Can you post the compilation error u're getting?


Work Hard, Expect The Worst...<br /> <br />Bimal R. Patel<br />(SCJP 1.2, SCWCD 1.4)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

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";
tayy abdelqader
Greenhorn

Joined: Dec 11, 2005
Posts: 20
Calculator.java:35: cannot resolve symbol
symbol : variable Choose
location: class Calculator
if ((Choose.equals("1"))||(Choose.equals("+"))||(Choose.equals("
add")))
^
Calculator.java:35: cannot resolve symbol
symbol : variable Choose
location: class Calculator
if ((Choose.equals("1"))||(Choose.equals("+"))||(Choose.equals("
add")))
^
Calculator.java:35: cannot resolve symbol
symbol : variable Choose
location: class Calculator
if ((Choose.equals("1"))||(Choose.equals("+"))||(Choose.equals("
add")))
^
Calculator.java:61: cannot resolve symbol
symbol : variable sum
location: class Calculator
System.out.println(sum);
^
tayy abdelqader
Greenhorn

Joined: Dec 11, 2005
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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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.
tayy abdelqader
Greenhorn

Joined: Dec 11, 2005
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

Joined: Jan 17, 2006
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.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
tayy abdelqader
Greenhorn

Joined: Dec 11, 2005
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

Joined: Jan 03, 2006
Posts: 490
Did you declare sum?


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: integer to string