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();
In that last line of code
int i = c.
I get a squiggly line under i that tells me I am not using the local variable so I’m not sure what exactly fixes that in this situation since I’m trying to use it in another class. Do I need to create an object?
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.
package myUtils.util;
import java.util.Scanner;
public class Console
{
Scanner sc = new Scanner(System.in);
public void print(String s)
{
System.out.println();
}
public void println(String s)
{
System.out.println();
}
public void println()
{
System.out.println();
}
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 the int
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())
{
//if user chooses menu option less than 1 the program will print an error message
d = sc.nextInt();
if (d < min)
{
System.out.println("Error! Please select menu option 1, 2, or 3");
}
//if the user chooses a menu option greater than 3 the program will print an error
else if (d > max)
{
System.out.println("Error! Please select menu option 1, 2, or 3");
}
//if the option is between 1 and 3 the menu option is valid
else
isValid = true;
}
else
System.out.println("Error! Invalid number value");
sc.nextLine();
}
// return the int
return d;
}
public String getRequiredString(String prompt)
{
return prompt;
}
public String getChoiceString(String prompt, String s1, String s2)
{
return s2;
}
public int getInt(String prompt)
{
return 0;
}
}
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?
This isn’t an import problem. If you’re importing something wrong, your program won’t compile in the first place, or at a bare minimum won’t launch.
As far as actually fixing your problem, I’d recommend looking at the two lines you think are associated with the outermost
elseingetIntWithinRangeand considering which code is actually associated with theelseand which code isn’t. The main issue is that anifor anelseis only associated with one line unless you surround the multiple lines with curly braces. So, you should modify the lastelseofgetIntWithinRangeto look likeEDIT: In response to the updated question
Right now, the code that you have written doesn’t use
iinmain. That includes not passing it to any other method or to any constructor. Sinceiis a local variable, it’s not accessible outsidemain, so Eclipse warns you. This “squiggly red line” warning from Eclipse won’t stop your code from compiling and running just fine, but it is intended to help you find bugs in your own code.On a broader note, there are a lot of bugs that arise because of unused locals, and Eclipse wants to help you prevent those bugs. Consider the example of wanting to initialize all the elements of a two-dimensional array to some user-supplied number (the variable names are shorter than I would use in a real program, but they still get the point across):
Eclipse’s flagging of unused locals helps you find these kinds of bugs more easily.