cannot find symbol error when creating an input object in java - 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!

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.

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.

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);

Related

Import these scanners into another class?

Again I am very new to Java, and I have this code here:
package Final;
import java.util.Scanner;
public class Position {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner MyPos = new Scanner(System.in);
System.out.println(" Enter Position: ");
String Pos = MyPos.nextLine();
System.out.println("Position: "+Pos);
Scanner MyMains = new Scanner(System.in);
System.out.println("Enter Mains: ");
String Mains1 = MyMains.nextLine();
System.out.println("Mains: "+Mains1);
Scanner WScore = new Scanner(System.in);
System.out.println("Enter Ward Score: ");
String score = WScore.nextLine();
System.out.println(" Average Ward Score is:"+score);
}
}
I was curious if there was a way to possibly import all 3 of my scanners to another class without moving all the actual code from this class? The class I'm trying to move it to is a class called "Player.java". There aren't any problems with my code, but additional input is always appreciate to help me understand and improve my code!
You don't need a new scanner for each variable. What you do need to do is close() a scanner when you are done using it.
You are thinking of the Scanner itself being associated with a variable - when in fact it is associated with System.in...
So, every time you want another variable, just do:
Scanner sc = new Scanner(System.in);
String firstThing = sc.nextLine();
String secondThing = sc.nextLine();
sc.close(); // Do this when you're done storing input from System.in!
Once you have declared your scanner. I'm not sure why you would want to pass the scanner to another class. What you can do easily is pass the variables that you grab from your scanner into a player object.
Just do Player p1 = new Player(stringYouScanned); assuming that you have your constructor in your Player class accept a String.
Or, look up getter and setter methods. Then you can do something like: `p1.setPosition(4);' (you could replace 4 with an int you scanned in, you get the idea).
If for some reason you want to scan input from within the Player, I'd just initialize a new Scanner, but make sure you are doing sc.close(); after you are done processing your input.

How do I make java wait for input before assigning the input to a variable to avoid java.util.NoSuchElementException

I'm studying Java classes and I'm trying to create a code where the user inputs how many objects (in this case "cube") they want to create.
In my main class I have this code written
System.out.println("Enter the amount of objects you want to create");
Scanner objNumInput = new Scanner(System.in);
int objNum = objNumInput.nextInt();
objNumInput.close();
Cube cubes[] = new Cube[objNum];
for (int i = 0; i < objNum; i++){
String cubeName = Cube.inputName();
double cubeLength = Cube.inputLength();
cubes[i] = new Cube(cubeName, cubeLength);
}
in my Cube class I have here:
public static String inputName(){
String cubeName;
Scanner input = new Scanner(System.in);
System.out.println("Enter the name: ");
cubeName = input.nextLine();
return cubeName;
}
public static double inputLength(){
double cubeLength;
Scanner input = new Scanner(System.in);
System.out.println("Enter the length: ");
cubeLength = input.nextDouble();
return cubeLength;
}
When I run it, I can input the number of "cubes" I want to create. Then, it keeps throwing an exception
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at Cube.inputName(Cube.java:40)
at Main.main(Main.java:88)
what's wrong?
Do not close your Scanner, it will close System.in as well.
When a Scanner is closed, it will close its input source if the source implements the Closeable interface
As I understand (correct me if I'm wrong) the reason why you close your objNumInput is that you want to use it in two different methods.
I would suggest you to pass the Scanner as input parameter into your methods inputName and inputLength. Then you'll be able to reuse the same scanner without closing it in between.
public static String inputName(Scanner scanner){
String cubeName;
System.out.println("Enter the name: ");
cubeName = scanner.nextLine();
return cubeName;
}
public static double inputLength(Scanner scanner){
double cubeLength;
System.out.println("Enter the length: ");
cubeLength = scanner.nextDouble();
return cubeLength;
}
...
System.out.println("Enter the amount of objects you want to create");
Scanner objNumInput = new Scanner(System.in);
int objNum = objNumInput.nextInt();
//objNumInput.close(); <-- Do not close the scanner
Cube cubes[] = new Cube[objNum];
for (int i = 0; i < objNum; i++){
String cubeName = Cube.inputName(objNumInput);
double cubeLength = Cube.inputLength(objNumInput);
cubes[i] = new Cube(cubeName, cubeLength);
}
put objNumInput.close(); after for loop in your main method.The reason your program flashes by without pausing the second time because System.in is closed when you do objNumInput.close(); in the line number 3 of main method
closing a Scanner object will close the underlying stream.
-your code only works one time because System.in is getting closed. You cannot "open" System.in again. A closed stream cannot be reopened

Math.pow(x,2) area of square

i am running a code, but i dont seem to be able to get the scanner working, anyone got an idea how?
public class verk34 {
public static void main(String[] args) {
System.out.println(Math.pow(x,2));
}
}
the thing is inserting a number on your own without altering the code.
As per my understanding, you need to add Scanner for taking input from end users and importing the java.util.Scanner statement.
Scanner in = new Scanner(System.in);
int x = in.nextInt();
First, you need to import scanner and Math
import java.util.Scanner
import java.lang.Math
Then If I am understanding correctly the problem, you need to do this in two steps :
-Instantiate a scanner and collect user input as an integer.
Scanner sc = new Scanner(System.in);
System.out.println(" Please enter the square area :");
int userValue = sc.nextInt();
The nextInt method will lock process and wait for a keyboard event before continuing. So enter a nulber with your keyboard.
-Use System.out.println with given value.
System.out.println(Math.pow(userValue,2));

Java : Could not load or find main class TrudiHw

I keep getting this error when trying to run my basic java code. It compiles with no errors at all but for some reason the program wont actually run. I am using textpad. Here is my code.
/*
*TrudiHw.java
*Trudi Farrell
*19/10/15
*/
import java.util.Scanner;
public class TrudiHw{
public static void main (String args[]){
int input;
int answer;
Scanner keyboard;
keyboard = new Scanner(System.in);
System.out.println("How many students are attendting the trip?");
input = keyboard.nextInt();
answer = input/5;
System.out.println("The amount of cars you will need is " +answer);
}
}

copyValueOf in Java?

import java.util.Scanner;
public class CourseSplitter {
public static void main(String args[]){
Scanner keyboard = new Scanner(System.in);
char[] course; //course code format: ABCDE##
String code;
//int num;
System.out.println("Input Course: ");
course = keyboard.next();
System.out.println(course);
code = String.copyValueOf(course, 0, 4);
System.out.println(code);
}
}
I don't know how I should let the user input the course when I'm using a character array instead of string. In short, how do I use the "scanner" on character arrays?
The instruction is the user will input a course code in the format: ABCDE##
Then, the program must split it into the course name and the course number. So, I had to use the copyValueOf method but it doesn't seem to work because from all the articles I read online, they used a char[] array but initialized the array with some value. So I was wondering how I could use the scanner on character arrays.
Why not just read a string from the scanner and then call String.toCharArray? It's not even clear why you need a char array here...
Why not just read a string directly with scanner.nextLine?
import java.util.Scanner;
public class CourseSplitter {
public static void main(String args[]){
Scanner keyboard = new Scanner(System.in);
System.out.println("Input Course: ");
String course = keyboard.nextLine();
System.out.println(course);
String code = course.substring(0, 5); //You put 4 but it left out the last letter in the course name. I changed it to 5 and it worked but I'm confused since the index always start with 0.
System.out.println(code);
String num = course.substring(5, 6);
System.out.println(num);
}
}

Categories

Resources