Import? Initialize? what do to?
Posted
by
Jeremy B
on Stack Overflow
See other posts from Stack Overflow
or by Jeremy B
Published on 2012-03-28T23:26:47Z
Indexed on
2012/03/28
23:29 UTC
Read the original article
Hit count: 240
I'm working on homework and I'm close but I am having an issue. I just learned how to work with packages in eclipse so I have a class that is importing another class from a package (I think I said that right) The main prompts the user to enter an integer between -100 and 100 and I am having an issue with validating it. I know the issue is where I'm importing I'm just unsure the direction I need to go to fix it.
This is a section of my main code. (my issue starts with the last couple lines if you want to skip ahead)
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();
I have a class called Console that is located in another package that I believe I have properly imported. here is the code I am stuck on in my console class.
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())
{
//if user chooses menu option less than 1 the program will print an error message
i = sc.nextInt();
if (i < min)
{
System.out.println("Error! Please enter an int greater than -100");
}
else if (i > max)
{
System.out.println("Error! Please enter an int less than 100");
}
else
isValid = true;
}
else
System.out.println("Error! Invalid number value");
sc.nextLine();
}
// return the int
return i;
}
when I run this I keep getting my last print which is an invalid number value. am I not importing the code from the main method in the other console properly?
© Stack Overflow or respective owner