Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8877121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:23:10+00:00 2026-06-14T19:23:10+00:00

import java.util.Scanner; public class InteractiveRectangle { public static void main(String[] args) { do {

  • 0
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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:23:11+00:00Added an answer on June 14, 2026 at 7:23 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import java.util.Scanner; public class Zad01 { public static void main(String[] args) { int []
import java.util.Scanner; public class CourseSplitter { public static void main(String args[]){ Scanner keyboard =
package GC; import java.util.Scanner; public class main { public static void main(String args[]) {
import java.util.Scanner; public class Main extends Hashmap{ public static void main(String[] args) { Hashmap
import java.util.Scanner; public class EP55Out { public static void main(String[] args) { Scanner in
import java.util.Scanner; public class RockPaperScissors { public static void main (String[] args) { Scanner
import java.util.scanner; import javax.swing.JOptionPane; public class FirstHomeJavaApplet{ public static void main(String[] args){ int num1=2;
import java.util.Scanner; import java.lang.String; public class Test { public static void main(String[] args) {
import java.util.Scanner; public class Question3 { public static void main(String[] args) { String input;
import java.util.Random; import java.util.Scanner; public class Game { public static void main(String[] args) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.