-1

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

Chris Wilson
  • 135
  • 3
  • 16

2 Answers2

1

How about writing a constructor then?

// Constructor
public ScannerLab() {
    scan = new java.util.Scanner(System.in);
}

Other than that, this line

System.out.println("list is" + myList);

doesn't work. It will give you the class name and hashcode, since that is the default implementation.

Try something like this:

System.out.println("\nThe words you entered are:");
System.out.println(Arrays.toString(myList));

which will give you an output like:

The words you entered are:
[apple, banana, carrot, dududu, elephant]
almightyGOSU
  • 3,731
  • 6
  • 31
  • 41
1

If there is no constructor in your class yet, you have an implicit default constructor available (see this question too). You can choose to override it at any point:

public class ScannerLab {
  public ScannerLab() {
    scan = new java.util.Scanner(System.in);
  }

  ...
}

From https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html:

The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass.

cringe
  • 13,401
  • 15
  • 69
  • 102
  • implicit constructor. Thank you guys very much, and i will utilize the .toString method, thats why it was returning a hashcode. Thanks guys! – Chris Wilson Jul 28 '15 at 03:18