java problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: java problem

  1. #1
    KateP Guest

    java problem


    Hello; i'm trying to build a java calculator application; I keep getting errors
    when i compile; this is the error:

    ---------------------------------------------------------------------

    C:\javap>javac Calc.java
    Calc.java:46: variable result might not have been initialized
    System.out.println("Answer= " + result);
    ^
    1 error

    ---------------------------------------------------------------------

    and this is the code; mind you i AM a beginner, so any help AT ALL would
    be great.

    --------------------------------------------------------------------

    public class Calc
    {

    public static void main(String[] args)
    {

    String another = "y";

    while(another.equalsIgnoreCase("y"))
    {
    System.out.println("Enter the first number: ");
    int first = Keyboard.readInt();

    System.out.println("Enter operator (+, -, * or /): ");
    int op = Keyboard.readInt();

    System.out.println("Enter second number: ");
    int second = Keyboard.readInt();

    int result;

    switch(op)
    {
    case '+':
    result = first + second;
    break;

    case '-':
    result = first - second;
    break;

    case '*':
    result = first * second;
    break;

    case '/':
    result = first / second;
    break;

    default:
    System.out.println("Wrong operator; use +, -, *, or / functions");

    };
    System.out.println("Answer= " + result);
    System.out.println("Another calculation? (y/n) ");
    another = Keyboard.readString();


    }

    }

    }

    -----------------------------------------------------------------

    basically what it's meant to do is ask the user for a number, then an operator,
    then a number than give an answer. if the user wants to make a another calculation,
    s/he pressed "y" and it goes through again.

    the problem i have (apart from the error ) is that i dont know how to allow
    for multiple calculations:

    eg; 4-3+34=blah or 56*6-12=blah

    please help me!



  2. #2
    JavaBeg Guest

    Re: java problem


    The varialble "result" has to be initialised to zero or some other integer
    value
    int result = 0;

    Hope this helps
    ============================


    "KateP" <java.@127.0.0.1> wrote:
    >
    >Hello; i'm trying to build a java calculator application; I keep getting

    errors
    >when i compile; this is the error:
    >
    >---------------------------------------------------------------------
    >
    >C:\javap>javac Calc.java
    >Calc.java:46: variable result might not have been initialized
    >System.out.println("Answer= " + result);
    >^
    >1 error
    >
    >---------------------------------------------------------------------
    >
    >and this is the code; mind you i AM a beginner, so any help AT ALL would
    >be great.
    >
    >--------------------------------------------------------------------
    >
    >public class Calc
    >{
    >
    >public static void main(String[] args)
    >{
    >
    >String another = "y";
    >
    >while(another.equalsIgnoreCase("y"))
    >{
    >System.out.println("Enter the first number: ");
    >int first = Keyboard.readInt();
    >
    >System.out.println("Enter operator (+, -, * or /): ");
    >int op = Keyboard.readInt();
    >
    >System.out.println("Enter second number: ");
    >int second = Keyboard.readInt();
    >
    >int result;
    >
    >switch(op)
    >{
    >case '+':
    >result = first + second;
    >break;
    >
    >case '-':
    >result = first - second;
    >break;
    >
    >case '*':
    >result = first * second;
    >break;
    >
    >case '/':
    >result = first / second;
    >break;
    >
    >default:
    >System.out.println("Wrong operator; use +, -, *, or / functions");
    >
    >};
    >System.out.println("Answer= " + result);
    >System.out.println("Another calculation? (y/n) ");
    >another = Keyboard.readString();
    >
    >
    >}
    >
    >}
    >
    >}
    >
    >-----------------------------------------------------------------
    >
    >basically what it's meant to do is ask the user for a number, then an operator,
    >then a number than give an answer. if the user wants to make a another calculation,
    >s/he pressed "y" and it goes through again.
    >
    >the problem i have (apart from the error ) is that i dont know how to allow
    >for multiple calculations:
    >
    >eg; 4-3+34=blah or 56*6-12=blah
    >
    >please help me!
    >
    >



  3. #3
    Guest

    Re: java problem


    The first response is not fully correct. It does not "have" to be initialized
    to zero or any other value explicitly. It will automatically be initialized
    to zero when declared.

    The problem is that this is a local variable and you used it outside it's
    scope. If you want to use this variable at the point you are using it, you
    need to declare it as a global variable.




    "KateP" <java.@127.0.0.1> wrote:
    >
    >Hello; i'm trying to build a java calculator application; I keep getting

    errors
    >when i compile; this is the error:
    >
    >---------------------------------------------------------------------
    >
    >C:\javap>javac Calc.java
    >Calc.java:46: variable result might not have been initialized
    >System.out.println("Answer= " + result);
    >^
    >1 error
    >
    >---------------------------------------------------------------------
    >
    >and this is the code; mind you i AM a beginner, so any help AT ALL would
    >be great.
    >
    >--------------------------------------------------------------------
    >
    >public class Calc
    >{
    >
    >public static void main(String[] args)
    >{
    >
    >String another = "y";
    >
    >while(another.equalsIgnoreCase("y"))
    >{
    >System.out.println("Enter the first number: ");
    >int first = Keyboard.readInt();
    >
    >System.out.println("Enter operator (+, -, * or /): ");
    >int op = Keyboard.readInt();
    >
    >System.out.println("Enter second number: ");
    >int second = Keyboard.readInt();
    >
    >int result;
    >
    >switch(op)
    >{
    >case '+':
    >result = first + second;
    >break;
    >
    >case '-':
    >result = first - second;
    >break;
    >
    >case '*':
    >result = first * second;
    >break;
    >
    >case '/':
    >result = first / second;
    >break;
    >
    >default:
    >System.out.println("Wrong operator; use +, -, *, or / functions");
    >
    >};
    >System.out.println("Answer= " + result);
    >System.out.println("Another calculation? (y/n) ");
    >another = Keyboard.readString();
    >
    >
    >}
    >
    >}
    >
    >}
    >
    >-----------------------------------------------------------------
    >
    >basically what it's meant to do is ask the user for a number, then an operator,
    >then a number than give an answer. if the user wants to make a another calculation,
    >s/he pressed "y" and it goes through again.
    >
    >the problem i have (apart from the error ) is that i dont know how to allow
    >for multiple calculations:
    >
    >eg; 4-3+34=blah or 56*6-12=blah
    >
    >please help me!
    >
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center