Levon Tamrazov
Levon Tamrazov

Reputation: 556

cannot find symbol error when creating an input object in java

I am new to java and netbeans. I am trying to write a program that requires user input. This is my code:

public class Arrays {

  public static void main(String[] args){
  }

  private double[] readNumbers(){
      final Input in = new Input();
      System.out.print("How many numbers will you enter?: ");
      final int count = in.nextInt();
      final double[] list = new double[count];
      for (int i = 0; i < count; ++i){
          System.out.print("Enter next number: ");
          list[i] = in.nextDouble();
      }
      return list;
  }
}

In line final Input in - new Input(); Netbeans underlines Input saying that it cannot find symbol. However I practically copied this code from the textbook so I don't understand what the problem is. I thought maybe I needed to import java.io, but that did not solve the problem. Really sorry if this is a stupid question, but any help would be really appreciated.

Thank you!

Upvotes: 2

Views: 1598

Answers (3)

See Jay
See Jay

Reputation: 11

Your code attempts to create an instance of class Input, but you don't include code for class Input. Resolve that (try the previous page in the book!) and your code will probably work.

Upvotes: 1

Jimmy
Jimmy

Reputation: 2619

Looks like your text book has some class definition that you forgot to import to your project. If you like to , change your code like,

final Input in = new Input();

to

final Scanner in = new Scanner(System.in);

If you don't want to change your code, then look few pages up and down the pages from where you got this code, you should see Class named Input , some what similar to this :

class Input{

    public int nextInt(){
         Scanner sc=new Scanner(System.in);
         return sc.nextInt();
    }

    public double nextDouble(){
         Scanner sc=new Scanner(System.in);
         return sc.nextDouble();
    }

}

Which is basically, an extra unnecessary work. Include that in your project, and it should run fine.

Upvotes: 2

Reimeus
Reimeus

Reputation: 159784

Input may be a wrapper class for java.util.Scanner:

You could replace:

final Input in = new Input();

with

final Scanner in = new Scanner(System.in);

Upvotes: 0

Related Questions