1

I'm currently working on a program and ran into an error while trying to execute a for loop. I want to declare a variable in the for loop, then break once that variable obtains a certain value, but it returns the error "cannot be resolved to a variable."

Here's my code

int i = -1;
for (; i == -1; i = index)     
{ 
    Scanner scan = new Scanner(System.in);
    System.out.println("Please enter your first and last name");
    String name = scan.nextLine();
    System.out.println("Please enter the cost of your car,"
                     + "\nthe down payment, annual interest rate,"
                     + "\nand the number of years the car is being"
                     + "\nfinanced, in that order.");
    DecimalFormat usd = new DecimalFormat("'$'0.00");
    double cost = scan.nextDouble();
    double rate = scan.nextDouble();
    int years = scan.nextInt();
    System.out.println(name + ","
                   +  "\nyour car costs " + usd.format(cost) + ","
                   +  "\nwith an interest rate of " + usd.format(rate) + ","
                   +  "\nand will be financed annually for " + years + " years."
                   +  "\nIs this correct?");
    String input = scan.nextLine();
    int index = (input.indexOf('y'));
}

I want to run the output segement of my program until the user inputs yes, then the loop breaks.

5 Answers 5

2

The variable index's scope is local to the block of the for loop, but not the for loop itself, so you can't say i = index in your for loop.

You don't need index anyway. Do this:

for (; i == -1;)

or even

while (i == -1)

and at the end...

    i = (input.indexOf('y'));
}

Incidentally, I'm not sure you want input.indexOf('y'); an input of "blatherskyte" will trigger this logic, not just "yes", because there's a y in the input.

Sign up to request clarification or add additional context in comments.

2 Comments

to add to the above answer also handle upper case Y,also what if the user types in "yay" or something like that,that should also be handled
@KaushikSivakumar Yes, the mechanism for breaking from the loop should be changed, for the "blatherskyte", "yay", and "Y" reasons.
1

Instead of using a for loop, you can do-while(it suits much better for this scenario.

boolean exitLoop= true;
do
{
    //your code here
    exitLoop=  input.equalsIgnoreCase("y");
} while(exitLoop);

Comments

0

for indefinite loop, i would prefer while.

boolean isYes = false;
while (!isYes){ 
Scanner scan = new Scanner(System.in);
System.out.println("Please enter your first and last name");
String name = scan.nextLine();
System.out.println("Please enter the cost of your car,"
                     + "\nthe down payment, annual interest rate,"
                     + "\nand the number of years the car is being"
                     + "\nfinanced, in that order.");
DecimalFormat usd = new DecimalFormat("'$'0.00");
double cost = scan.nextDouble();
double rate = scan.nextDouble();
int years = scan.nextInt();
System.out.println(name + ","
                   +  "\nyour car costs " + usd.format(cost) + ","
                   +  "\nwith an interest rate of " + usd.format(rate) + ","
                   +  "\nand will be financed annually for " + years + " years."
                   +  "\nIs this correct?");
String input = scan.nextLine();
isYes = input.equalsIgnoreCase("yes");
}

Comments

0

You cannot do this. If the variable is declared inside of the loop, then it is re-created every run. In order to be part of the condition for exiting the loop, it must be declared outside of it.

Alternatively, you could use the break keyworkd to end the loop:

// Should we exit?
if(input.indexOf('y') != -1)
    break;

Comments

0

Here you want to use a while loop. Usually you can decide which loop to use by saying your logic out loud to yourself, While this variable is(not) (value) do this.

For your problem, initialize the variable outside of the loop, and then set the value inside.

String userInput = null;
while(!userInput.equals("exit"){
  System.out.println("Type exit to quit");
  userInput = scan.nextLine();
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.