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

  • SEARCH
  • Home
  • 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 7743057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:26:06+00:00 2026-06-01T09:26:06+00:00

I’m learning about packages. I have two classes that are in different packages and

  • 0

I’m learning about packages. I have two classes that are in different packages and I seem to be having an issue with my output.

Here is the class with the main:
My two issues are with the bottom two sections “required string test” and “String choice test”

import myUtils.util.Console;

public class ConsoleTestApp
{
public static void main(String args[])
{
    // create the Console object
    Console c = new Console();

    // display a welcome message
    c.println("Welcome to the Console Tester application");
    c.println();

    // int
    c.println("Int Test");
    int i = c.getIntWithinRange("Enter an integer between -100 and 100: ", -101, 101);
    c.println();

    // double
    c.println("Double Test");
    double d = c.getDoubleWithinRange("Enter any number between -100 and 100: ", -101, 101);
    c.println();

    // required string
    c.println("Required String Test");
    String s1 = c.getRequiredString("Enter your email address: ");
    c.println();

    // string choice
    c.println("String Choice Test");
    String s2 = c.getChoiceString("Select one (x/y): ", "x", "y");
    c.println();
}
}

here is the code for the class they are accessing. (this is the Console class that was imported at the top of the main)

package myUtils.util;
import java.util.Scanner;

public class Console 
{           
Scanner sc = new Scanner(System.in);

/******************************************************/
public void println()
{       

}   

/******************************************************/
public void print(String s)
{
    System.out.println(s);
}

/******************************************************/
public void println(String s)
{
    System.out.println(s);      
}

/*****************************************************/
public int getIntWithinRange (String prompt, int min, int max)
{
    int i = 0;
    boolean isValid = false;
    while (isValid == false)
    {
        System.out.println(prompt);
        if (sc.hasNextInt())
        {               
            i = sc.nextInt();
                if (i < min)
                {                       System.out.println
                    ("Error! Please enter an integer greater than -100");
                }                   
                else if (i > max)
                {
                    System.out.println ("Error! Please enter an integer less than 100");
                }
                else
                    isValid = true;
        }
        else
        {
            System.out.println ("Error! Invalid number value");
            sc.nextLine();
        }   
    }
    return i;
}

/*****************************************************/
public double getDoubleWithinRange (String prompt, double min, double max)
{
    int d = 0 ;
    boolean isValid = false;
    while (isValid == false)
    {
        System.out.println(prompt);
        if (sc.hasNextInt())
        {               
            d = sc.nextInt();
                if (d < min)
                {
                    System.out.println ("Error! Please enter a number greater than -100");
                }
                else if (d > max)
                {
                    System.out.println ("Error! Please enter a number less than 100");
                }
                else
                    isValid = true;
        }
        else 
        {
            System.out.println("Error! Invalid number value");
            sc.nextLine();
        }
    }
    return d;
}

/*****************************************************/
public String getRequiredString(String prompt)
{
    String R = "";
    boolean isValid = false;
    while (isValid == false)
    {
        System.out.print(prompt);
        R = sc.nextLine();
        if (R.equals(""))
        {
            System.out.println("Error! This entry is required. Try again.");
        }
        else
        {
            isValid = true;
        }
    }
    return R;
}   

public String getChoiceString (String prompt, String s1, String s2)
{   
    String s = "";
    boolean isvalid = false;
    while (isvalid == false);
    {           
        s = this.getChoiceString(prompt, s1, s2);
        System.out.println(s);
        s = sc.nextLine();
        if (!s.equalsIgnoreCase(s1) && !s.equalsIgnoreCase(s2))
        {
            System.out.println("Error! Entry must be '"+
                s1 +"', '"+ s2 +"', or '."+  "Try again.");     
        }
        else
        {
            isvalid = true;
        }
    }
    return s;   
}       

public int getInt(String prompt)
{
    return 0;   
}
}

The issue I am having with the getRequiredString is I have a line of code that says

    R = sc.nextLine();

This is causing my output to print my error message because it thinks the user didn’t enter anything. When I change it to a simple

    R = sc.next();

The code doesn’t let the user enter anything. I’m unsure what to do to fix this.

The issue I am having with my getChoiceString is the program isn’t printing anything. It seems that section of code won’t print my prompt or any of the other elements.

I’m new and this is homework so any hints, clues, or help walking me through this is appreciated.

  • 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-01T09:26:07+00:00Added an answer on June 1, 2026 at 9:26 am

    Gotcha! You put an semi colon after your while statement which causes your program to halt.

    while (isvalid == false);  <---
    {
       ....
    }
    

    I’ve removed it and noticed that your code goes into infinite loop because getChoiceString method calls itself unnecessarily. Therefore I’ve edited that as well (commented out your recursion call), and it seems working now! Try the following:

    public String getChoiceString(String prompt, String s1, String s2)
    {
    
        String s = "";
        boolean isvalid = false;
        while (true)
        {
            if(isvalid == true)
            {
                break;
            }
            //s = this.getChoiceString(prompt, s1, s2);
            //System.out.println(s);
            System.out.println("Enter your String: ");
            s = sc.nextLine();
            if (!s.equalsIgnoreCase(s1) && !s.equalsIgnoreCase(s2))
            {
                System.out.println("Error! Entry must be '"+
                    s1 +"', '"+ s2 +"', or '."+  "Try again.");
    
    
            }
            else
            {
                isvalid = true;
            }
        }
        return s;
    
    }
    

    EDIT: Also in your getRequiredString method, use R = sc.next(); instead of R = sc.nextLine(); in order to make your program work properly. Check the following:

    System.out.print(prompt);
    R = sc.next();
    System.out.println("Email address: " + R.toString());
    if (R.equals(""))
    {
        System.out.println("Error! This entry is required. Try again.");
    }
    else
    {
        isValid = true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.