• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

integer to string

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how could i convert an integer to a string ?

is it possible?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The static method String.valueOf(int) is one way:

int anInt = 5;
String aString = String.valueOf(anInt);
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another way is
int anInt = 5;
String aString = "" + anInt;
 
tayy abdelqader
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can you post the compilation error u're getting?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !!!
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
tayy abdelqader
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you declare sum?
 
reply
    Bookmark Topic Watch Topic
  • New Topic