Java noob here. My instructor told me specifically to "instantiate the scanner INSIDE the constructor". The problem is, i am not seeing a constructor in our ScannerLab class. Nor am i seeing any inheritance. I have a field named scan which is of type java.util.Scanner that i need to use. How do i instantiate the scanner inside the constructor?
code:
public class ScannerLab {
private java.util.Scanner scan;
public void echoStrings() {
String word;
// create a new storage array
String[] myList = new String[5];
// set for loop
for(int i = 0; i < 5; i ++) {
// prompt for the value
System.out.print("Enter word " + i + ": ");
// get the input value
word = scan.next();
// echo the input value
System.out.println("You entered " + word);
// store the input value into the array
myList[i] = word;
}
String line = "";
// loop through the array and concatenate the values
// put a space between the words
System.out.println("The words you entered are: " + line);
System.out.println("list is" + myList);
}
public void echoIntsAndTotal() {
int inputValue;
// declare an array to hold the 5 values
for(int i = 0; i < 5; i ++) {
// prompt for the value
System.out.print("Enter integer value " + i + ": ");
// get the input value
inputValue = 23;
// echo the input value
System.out.println("You entered " + inputValue);
// store the input value into the array
}
int total = 0;
// loop through the array and add the values
System.out.println("The total of your values is " + total);
}
public static void main(String[] args) {
ScannerLab lab;
lab = new ScannerLab();
lab.echoStrings();
// lab.echoIntsAndTotal();
}
}
i have tried: setting the scan field as a reference variable to:
private java.util.Scanner scan = new java.util.Scanner(System.in);
then in my application method i used:
public static void main(String[] args) {
ScannerLab lab;
lab = new ScannerLab(scan);
didnt work. The only way it would compile and run is if i switch my field to:
private static java.util.Scanner scan = new java.util.Scanner(System.in);
but he wont allow us to use static fields, and that is still not being instantiated inside the constructor.
where is the constructor, and how do i instantiate a new scanner in it? Thank you