Reputation: 13
import java.util.Scanner;
public class InteractiveRectangle
{
public static void main(String[] args)
{
do
{
int length = readInteger ("For Length ");
System.out.println();
int width = readInteger ("For Width ");
printRectangleDetails(length,width);// existing code goes here
}
while (keepGoing());
System.out.println("Goodbye, friend");
}
/**
* returns the details of the rectangle
* @param height the height of the rectangle
* @param width the width of the rectangle
*/
public static void printRectangleDetails (int length, int width)
{
System.out.println ("This is the length of the rectangle " + length);
System.out.println ("This is the width of the rectangle " + width);
System.out.println (("This is the perimeter of the rectangle " + (length + width)));
System.out.println (("This is the area of the rectangle " + (length * width)));
}
/**
* Read in an integer and return its value
* @param the prompt to be shown to the user
*/
public static int readInteger(String prompt)
{
System.out.println (prompt);
Scanner scan = new Scanner(System.in);
System.out.println("Enter an integer");
while (!scan.hasNextInt()) // while non-integers are present
{
scan.next();
System.out.println ("Bad input. Enter an integer.");
}
int input = scan.nextInt();
return input;
}
/**
* Read a positive integer and return its value
* @param the prompt to be shown to the user
*/
public static int readPositiveInteger(String prompt)
{
System.out.println (prompt);
Scanner scan = new Scanner(System.in);
System.out.println("Enter an integer");
boolean positive = false;
while (scan.hasNextInt() && positive == false)
{
int input = scan.nextInt();
if (input > 0)
{
positive = true;
{
return input;
}
}
else
{
System.out.println ("Bad input enter an integer.");
positive = false;
scan.nextLine();
}
}
return 0;
}
/**
* Ask the user whether or not to spawn another rectangle
* and returns the result as a boolean
*/
public static boolean keepGoing()
{
Scanner scan = new Scanner(System.in);
boolean inputRead = false;
boolean result = false;
System.out.println ("Do you want to process another rectangle?");
scan.next();
String input = scan.next();
if (input == "y")
{
inputRead = true;
result = true;
}
else if (input == "n")
{
inputRead = true;
result = false;
}
else
{
System.out.println("Bad input please try again!");
scan.nextLine();
}
return result;
}
}
I want the program to ask the user if they want to spawn another rectangle, and keep going until the user answers this question with “n”. Atm when I run the program, the rectangle only spawns once, so I think there's a problem with my keepGoing method. Any help would be appreciated, Thanks!
Upvotes: 1
Views: 945
Reputation: 213223
Yes there are few problems: -
First you are comparing strings using ==
operator, which will always be false. Use equals
method: -
if (input.equals("y")) // or, if (input.equalsIgnoreCase("y"))
Secondly, you should not use scan.next()
method, the way you are using. Your first scan.next
should be assigned to input
, as your 2nd scan.next
contains the newline: -
System.out.println ("Do you want to process another rectangle?");
// scan.next(); // Should not be here.
String input = scan.next();
scan.next(); // Change the order
Or, just use scan.nextLine()
: -
System.out.println ("Do you want to process another rectangle?");
String input = scan.nextLine();
Third, in your else
part, you can just invoke your keepGoing
method again, rather than reading input there: -
else
{
System.out.println("Bad input please try again!");
return keepGoing();
}
Also, in your if-else
, rather than setting the boolean values to the variables, you can directly return from there.
So, in all, you can change your if-else if-else
to: -
if (input.equals("y")) {
return true;
}
else if (input.equals("n")) {
return false;
}
else
{
System.out.println("Bad input please try again!");
return keepGoing();
}
And then, you don't need those boolean
variables: - inputRead
and result
. Just remove them. And remove the return
statement from the end of your method
. As it will be unreachable code
now.
Upvotes: 2
Reputation: 16234
This code:
if (input.equals("y"))
{
inputRead = true;
result = true;
}
else if (input.equals("n"))
{
inputRead = true;
result = false;
}
Should solve the problem. Remember, objects in Java are compared by references, not by values.
Upvotes: 1
Reputation: 46408
You are checking if two Strings are equal using ==
operator. Always, check String equality using equals method.
if (input == "y")
should be
if (input.equals("y"))
and in the rest of the places as well,
==
operator checks if two String references point to the same String Object. equals method determines if two String objects are meaningfully equal.
Upvotes: 1
Reputation: 5183
always compare strings with .equals()
if (input == "y")
replace to
if (input.equals("y"))
Upvotes: 1
Reputation: 37813
if (input == "y")
always compare Strings with equals()
you need
if ("y".equals(input))
or
if ("y".equalsIgnoreCase(input)) // will also allow Y
change the other checks accordingly.
Upvotes: 4