Search code examples
javadelimiter

Is the text book wrong about delimiter usage


On page 66 of An Introduction to Object-Oriented Programming with Java, C Thomas Wu introduces a delimiter. To my understanding by default it divides string up using whitespace.

So to divide the string into tokens via the return button, the example uses:

String lineSeparator = System.getProperty("line.separator");
   scanner.useDelimiter(lineSeparator);

which to me is get the value of return from the system, assign it to lineSeparator and the tell scanner to use lineSeparator as the delimiter.

The problem is when I copy it word for word, I get an error.

import javax.swing.*;
import java.util.*;
import java.text.*;


class Ch2Sample1 {
    public static void main(String [] args){
      
    Scanner scanner = new Scanner(System.in);
    String lineSeparator = System.getProperty("line.separator");
    Scanner.useDelimiter(lineSeparator);
    
    String quote;
    System.out.println("enter last name");
    quote = scanner.next();
    System.out.println(quote);
    
    
}}

enter image description here

Have I made a mistake, or has the book made an error?


Solution

  • The problem is that you are trying to use a instance method off of the type as if it were a static method. Change Scanner.useDelimiter(lineSeparator); to scanner.useDelimiter(lineSeparator);.