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.
Gotcha! You put an semi colon after your while statement which causes your program to halt.
I’ve removed it and noticed that your code goes into infinite loop because
getChoiceStringmethod calls itself unnecessarily. Therefore I’ve edited that as well (commented out your recursion call), and it seems working now! Try the following:EDIT: Also in your getRequiredString method, use
R = sc.next();instead ofR = sc.nextLine();in order to make your program work properly. Check the following: