david
david

Reputation: 9

How to use the value of variable from if/else tree as value of variable for a while loop afterwards

Here is my program code as I have it written so far. It's saying I haven't initialized answer. I want to use the value of answer after the if/else tree executes to convert the number into asterisks.

import java.io.*;
import java.util.*;

public class Project5 {
    public static void main(String[] args){
        System.out.println("Please enter a formula of this form:operator,operand,operand(ex. add,2,2):");
        // wanted to change things but was not sure how far I was allowed to go so I used commas instead of spaces
        //was trying to split with a delimiter but kept getting an error [Ljava.lang.String;@55e83f9
        Scanner input = new Scanner(System.in);
        String formula = input.nextLine();
        System.out.println(formula);
        String oper = formula.substring(0,3);
        System.out.println(oper);
        String fnum = formula.substring(4,5);
        System.out.println(fnum);
        String snum = formula.substring(6,7);
        System.out.println(snum);
        double freal = Integer.parseInt(fnum);
        System.out.println(freal);
        double sreal = Integer.parseInt(snum);
        double answer;
        if (oper.equalsIgnoreCase("add") == true){
            answer = freal+sreal;
            System.out.println(answer);
        }
        else if(oper.equalsIgnoreCase("subtract") == true){
                 answer = freal-sreal;
            System.out.println(answer);
        }
        else if(oper.equalsIgnoreCase("multiply") == true){
                 answer = freal*sreal;
            System.out.println(answer);
        }
        else if(oper.equalsIgnoreCase("divide") == true){
                 answer = freal/sreal;
            System.out.println(answer);
        }
        else
            System.out.println("not valid.");

        while(answer > 0){
            System.out.print("*");
            answer--;
    }
}}

Upvotes: 0

Views: 170

Answers (3)

user749242
user749242

Reputation:

I think the only thing is that you have to initialize answer: double answer = 0.0;

Upvotes: 0

Chris Thompson
Chris Thompson

Reputation: 35598

Because in the final else branch, you don't assign a value to answer. If that is the tree that's executed, then when you get to the while loop, it will be undefined. You can fix this by initially initializing answer to some value or by making sure every branch of the if/else tree assigns answer a value.

Edit To address your question, change

double answer;

To

double answer = 0.0;

That will fix the problem.

Upvotes: 2

andyb
andyb

Reputation: 43823

The wording of the error you are getting is a good start in working out what the problem is:

Project5.java:41: variable answer might not have been initialized

So you should initialize the variable answer

Edit: To be even clearer, change the line that says double answer; to double answer = 0.0; thereby initializing the answer variable. That is what the compiler is complaining about and is the only change you need to make in order for the code to compile and execute. By the way, you will have more errors to deal with after this because although you are allowing the operator to be 3 characters, you are trying to compare this with a longer word in some cases. So add works but subtract does not.

Upvotes: 0

Related Questions