VictorN
VictorN

Reputation: 3

How to use delimiter?

I'm trying to compile the following code, but I keep getting the cannot find symbol error. The assignment requires the use of getInput method which requires no argument and return nothing as well, its main funcion is to read the input and display it. So I was trying to change the scope of the scanner variable input and inputString so that whenever getInput is called, I dont have to pass them in to it.

import java.util.Scanner;

public class ReadKeyboardInput
{   
   public static Scanner input = new Scanner(System.in);
   public static String inputString;
   public static void main(String[] args)
   {     

      input.useDelimeter("\\n");

      System.out.print("Enter an integer: ");
      getInput();
      System.out.print("Enter a float: ");
      getInput();
      System.out.print("Enter a string: ");
      getInput();      
   }
    public static void getInput()
    {                 
      inputString = input.next();
      System.out.println("You have entered: " + inputString + ".");
    }
}

Or if I bring the input.useDelimeter("\n"); outside of the main scope, then I get another 2 errors 'identifier' expected and illegal start of type for this particular line. This altered code looks like above, except for :

   public static Scanner input = new Scanner(System.in);
   public static input.useDelimeter("\\n");
   public static String inputString;
   public static void main(String[] args)
   { ....  

Upvotes: 1

Views: 2798

Answers (3)

itsfarseen
itsfarseen

Reputation: 1178

Instead of input.useDelimeter("\\n"); write input.useDelimiter("\\n");.
That should work for you.

Upvotes: 0

James Cronen
James Cronen

Reputation: 5763

You spelled Delimiter wrong, maybe? A delimeter would be something that measures the corned beef before it goes on your sandwich.

Upvotes: 3

tbodt
tbodt

Reputation: 16987

Your code segment doesn't seem to have a problem.


As for the suggested modification, you obviously misunderstand what it means to say public static whatever. You can only use that to declare variables and methods with class scope. You can't do anything else. If that's what you really want to do, put in a static initilalization block. Just put this:

static {
    input.useDelimiter("\\n");
}

Upvotes: 0

Related Questions