Cannot find symbol error Netbeans - java

Hi i am new to Java and i am writting a simple programm that will emulate a diving competition. I use NetBeans and i cannot find out why it gives me this error.
The code is this :
package agoneskataduseon;
import java.util.Scanner;
public class AgonesKataduseon {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int numAth, numJud, numDiv;
System.out.print("Give the number of the athletes in the competition : ");
numAth = input.nextInt();
System.out.print("Give the number of the judges : ");
numJud = input.nextInt();
System.out.print("Give the numbe rof dives : ");
numDiv = input.nextInt();
DivingCompetition game = new Diving Competition(numAth, numJud, numDiv);
}
}
The DivingCompetition class(it is not finished) :
import java.util.Scanner;
public class DivingCompetition {
int numa;
int numj;
int numd;
Athlete[] arr1 = new Athlete[12];
Athlete[] arr2 = new Athlete[12];
public DivingCompetition(int numAthletes, int numJudges, int numDives){
numa = numAthletes;
numj = numJudges;
numd = numDives;
}
public void readAthletesName(){
String nam;
int i=0;
Scanner input = new Scanner(System.in);
while(i < numa){
System.out.print("Give the name of athlete num "+(i+1)+" : ");
nam = input.nextLine();
arr1[i] = new Athlete(nam);
i++;
}
}
public void runCompetition(){
int i=0;
Dive dv = new Dive(numj);
while(i<12){
arr1[i].addDive(dv);
}
}
}
And for the record the Athlete class:
public class Athlete {
String names;
Dive[] arrayd = new Dive[6];
double fullScore;
int point;
public Athlete(String name){
names = name;
fullScore = 0;
point = 0;
}
public void addDive(Dive dive){
arrayd[point] = dive;
fullScore = fullScore + arrayd[point].computeScore();
point++;
}
}
I would also give you the Dive class but i dont see the point of doing that
NetBeans is giving me an error in the main function :
Cannot find symbol
Symbol: class DivingCompetition
location: class AgonesKataduseon
And my question is why it gives me this error?
This problem also happens if i try to make an object of Athlete or Dive inside the main function

Netbeans will try to help and give you suggestions to fix your code if you ask it to. All you have to do is to place the cursor on the line where the problem is and press Alt + Enter.
It actually gives you this keyboard shortcut at that bottom of the error message when you hover over the erroneous statement.
See the Code Assistance page in the manual for more info.

Related

java program bug in eclipse

I am new to java ,I just want to create array of humans object in class Earth
But I am getting error :
Exception in thread "main" java.lang.NullPointerException
at Earth.main(Earth.java:14)
I don't know what's wrong with my program it seems everything regarding syntax is correct .
Input:
2
12
aks (...and .. program crash)
import java.util.*;
public class Human {
String name;
int age;
int height;
String eyecolor;
//construct necessary
public Human() {
}
public void speak() {
System.out.println("Hello My name is " + name);
System.out.println("I am "+height + "inches tall");
}
public void eat() {
System.out.println("eating...");
}
}
import java.util.*;
public class Earth {
public static void main(String args[]) {
Human humans[] = new Human[10];
System.out.println("Enter the number of humans\n");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i=0;i<n;i++) {
int age;
String name;
age = sc.nextInt();
name = sc.next();
humans[i].age=age;
humans[i].name=name;
}
for(int i=0;i<n;i++) {
System.out.printf("name is %s and age is %d \n", humans[i].name,humans[i].age);
}
sc.close();
}
}
Your statement :
Human humans[] = new Human[10];
creates an array to hold 10 humans, but it does not create those humans. Instead, each of the 10 entries is initialised to null - hence your exception when you try to use them in humans[i].age=age;
Instead, create the humans in the loop :
for(int i=0;i<n;i++) {
int age;
String name;
age = sc.nextInt();
name = sc.next();
humans[i] = new Human(); // Add This
humans[i].age=age;
humans[i].name=name;
}
It would also be a good idea to move the declaration of the array to after the user has entered the number of humans they want; As it stands, there's nothing to stop the user entering a number more than 10, which would also cause a problem. So try something like :
System.out.println("Enter the number of humans\n");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Human humans[] = new Human[n];

Output will not print the correct information

I'm trying to make a program which asks the user a particular bird then how many of them they had seen at that point. If the use at any point enters the word 'END' then the system should print out the most seen bird and the number seen. However, when running my program if I enter 'END' at random points it instead returns that the most seen was END with 0 seen. I can't figure out how to make it work. I've tried different methods but it's just not working properly. Also, I've set the maximum array limit to 10 possitions but it continues after 10 and if i enter a value the system crashes. Have I written the limit part properly? Or am I missing something important?
import java.util.Scanner;
public class testing
{
public static void main (String[] param)
{
birdInput();
most();
System.exit(0);
}
public static void birdInput()
{
int i = 0;
String birdInput;
int numberInput;
Scanner scanner = new Scanner(System.in);
int maxVal = Integer.MIN_VALUE;
int maxValIndex = -1;
while (true)
{
System.out.println("What bird did you see?");
birdInput = scanner.nextLine();
if (birdInput.equals("END"))
{
System.out.print("\nWell....I guess thanks for using this program?\n");
System.exit(0);
}
else
{
String[] birds = new String[10];
int[] numbers = new int[10];
birds[i] = scanner.nextLine();
System.out.println("How many did you see?");
numbers[i] = scanner.nextInt();
i++;
if (birds[i].equals("END"))
{
maxVal = numbers[i];
maxValIndex = i;
System.out.print("\nThe most common bird that you saw was the " + birds[maxValIndex] + " with " + maxVal + " being seen in total\n");
System.exit(0);
}
}
}
}
public static void most()
{
System.out.println("fdff");
}
}
This is my edit of Till Hemmerich's answer to my issue. I tried to remove the global variables and so combine the entire code into 1 method. However, I'm still having some issues. Been working at it but really confused.
import java.util.Scanner;
public class birds2
{
public static void main(String[] param)
{
birdInput();
System.exit(0);
}
public static void birdInput()
{
Scanner scanner = new Scanner(System.in);
String[] birds = new String[99999999];
int[] numbers = new int[99999999];
int i = 0;
int maxIndex;
while (i <= birds.length)
{
System.out.println("What bird did you see?");
birds[i] = scanner.nextLine();
System.out.println("How many did you see?");
numbers[i] = scanner.nextInt();
i++;
}
int newnumber = numbers[i];
if ((newnumber > numbers.length))
{
maxIndex = i;
i++;
}
if (birds[i].toUpperCase().equals("END"))
{
System.out.print("\nWell....I guess thanks for using this program?\n");
System.out.print("\nThe most common bird that you saw was the " + birds[maxIndex] + " with " + numbers[maxIndex] + " being seen in total\n");
System.exit(0);
}
}
}
You're re-declaring the birds and numbers arrays in each iteration of the loop. They should be declared and initialized only once, before the loop.
I changed a lot so im going to explain my changes here in total.
First of all i had to move the Array Definition out of your while-loop as >mentioned above, since other wise you would override these Arrays every time.
I also made them globally accessible to work with them in other methods.
public static int maxIndex;
public static String[] birds = new String[10];
public static int[] numbers = new int[10];
in general I re structured the whole code a little bit to make it more readable and a little bit more object-orientated.
For example I created an method called inputCheck() which returns our input as a String and check if it equals END so you do not have to write your logic for this twice. (it also considers writing end lower or Uppercased by just Upper our input before checking it"if (input.toUpperCase().equals("END"))")
static String inputCheck() {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
if (input.toUpperCase().equals("END")) {
end();
}
return input;
}
this method can now be called every time you need an input like this:
birds[i] = inputCheck();
but you need to be carefull if you want to get an integer out of it you first have to parse it like this:Integer.parseInt(inputCheck())
after that I wrote a method to search for the biggest Value in your numbers Array and getting its index:
public static int getMaxIndex(int[] numbers) {
for (int i = 0; i < numbers.length; i++) {
int newnumber = numbers[i];
if ((newnumber > numbers.length)) {
maxIndex = i;
}
}
return maxIndex;
}
it takes an int array as parameter and returns the index of the highest element in there as an Integer. Called like this:maxIndex = getMaxIndex(numbers);
Then after that I rewrote your end method. It now just calles our getMaxIndex method and prints some output to the console.
public static void end() {
maxIndex = getMaxIndex(numbers);
System.out.print("\nWell....I guess thanks for using this program?\n");
System.out.print("\nThe most common bird that you saw was the " + birds[maxIndex] + " with " + numbers[maxIndex] + " being seen in total\n");
System.exit(0);
}
to fix your last problem (crashing after more then 10 inputs)I changed your while-loop. Since your array only has 10 places to put things it crashes if you try to put information in place number 11. it not looks like this:while (i <= birds.length) instead of while (true) this way the max loops it can take is the amout of places Array birds has and it wont crash anymore.
public static void birdInput() {
int i = 0;
while (i <= birds.length) {
System.out.println("What bird did you see?");
birds[i] = inputCheck();
System.out.println("How many did you see?");
numbers[i] = Integer.parseInt(inputCheck()); //you should check here if its actuall a number otherwiese your programm will crash
i++;
}
}
Here is the whole code in total:
import java.util.Scanner;
/**
*
* #author E0268617
*/
public class JavaApplication1 {
public static int maxIndex;
public static String[] birds = new String[10];
public static int[] numbers = new int[10];
public static void main(String[] param) {
birdInput();
most();
System.exit(0);
}
public static void birdInput() {
int i = 0;
while (i <= birds.length) {
System.out.println("What bird did you see?");
birds[i] = inputCheck();
System.out.println("How many did you see?");
numbers[i] = Integer.parseInt(inputCheck()); //you should check here if its actuall a number otherwiese your programm will crash
i++;
}
}
static String inputCheck() {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
if (input.toUpperCase().equals("END")) {
end();
}
return input;
}
public static int getMaxIndex(int[] numbers) {
for (int i = 0; i < numbers.length; i++) {
int newnumber = numbers[i];
if ((newnumber > numbers.length)) {
maxIndex = i;
}
}
return maxIndex;
}
public static void end() {
maxIndex = getMaxIndex(numbers);
System.out.print("\nWell....I guess thanks for using this program?\n");
System.out.print("\nThe most common bird that you saw was the " + birds[maxIndex] + " with " + numbers[maxIndex] + " being seen in total\n");
System.exit(0);
}
public static void most() {
System.out.println("fdff");
}
}
I hope you understand where the Problems had been hidden if you have any Questions hit me up.

How to fix my Array class

I am trying to figure out how to get my array to run correctly, I know I have to change the array value to an input but I cannot get the program to compile if any one can help that be great.
I am trying to have the program take input for grades and names of students and in the end output their name and grade.
Edit sorry this is my first it posting i have an error
Student.java:60: error: class, interface, or enum expected I am in java 101 so this is why it is such low level java, we only know the basics
import java.util.Scanner;
public class students
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("How many students?: ");
int numofstudents = keyboard.nextInt();
Student s = new Student();
s.setMultipleStudents();
s.toString();
System.out.println("Enter the Grade for the student: ");
int gradeofstudnets = keyboard.nextInt();
}
}
and my second class is
import java.util.Scanner;
public class Student
{
Scanner scan = new Scanner(System.in);
private String name;
private int grade;
private int[] multiplegradeinputs = new int[10];
private String[] multipleStudent = new String[10];
public Student()
{
}
public Student(String n, int g)
{
name = n;
grade = g;
}
public String setMultipleStudents()
{
String n = "";
for(int i = 1; i < multipleStudent.length; i++)
{
System.out.println("Enter student #" + i +" name: " );
n = scan.nextLine();
multipleStudent[i] = n;
}
return null;
}
public String multiplegradeinputs()
{
for(int i = 1; i <multiplegradeinputs.length; i++)
{
System.out.println("Enter the Grade of the student#" + i +" : ");
grade = scan.nextInt();
multiplegradeinputs[i] = grade;
}
} <--- error here
public String toString()
{
String temp = "";
for(int i = 1; i < multipleStudent.length; i++)
{
temp += multipleStudent[i] + " ";
}
return temp;
}
}
Add return statement in your multiplegradeinputs() method:
public String multiplegradeinputs()
{
for(int i = 1; i <multiplegradeinputs.length; i++)
{
System.out.println("Enter the Grade of the student#" + i +" : ");
grade = scan.nextInt();
multiplegradeinputs[i] = grade;
}
return null; //Add this line
}
Or change your methods to void return type if they dont return anything.
Class names have to be capitalized in java, so instead of
public class students
you should write
public class Students
Also instead of writing
keyboard.nextInt();
You should write
Integer.parseInt(keyboard.nextLine());
This is mainly because java is full of bugs and technical specifications that you won't find easily. Let me know if this fixes it for you, since you didn't post the exact error message you got.
As for the error that you pointed out, it's because your function expects a String as a return value no matter what, so either change that to void if you can or return a null string. To do that just add the following line at the very end of the method.
return null;
You should create a Student object which holds the properties of the student, e.g. Name and Grades. You should then store all the student objects in some kind of data structure such as an array list in the students class.
Adding to the answer provided by #hitz
You have a bug in the for loops:
for(int i = 1; i <multiplegradeinputs.length; i++)
for(int i = 1; i < multipleStudent.length; i++)
You will never populated multiplegradeinputs[0] and multipleStudent[0] because you start the loop at index == 1 and thus you will have only 9 student names stored instead of 10.
Change to:
for(int i = 0; i <multiplegradeinputs.length; i++)
for(int i = 0; i < multipleStudent.length; i++)
Remember even though the length in 10, the indices always start with 0 in Java and in your case will end with 9.
import java.util.Scanner;
public class Student
{
Scanner scan = new Scanner(System.in);
private String name;
private int grade;
private int[] multiplegradeinputs = new int[10];
private String[] multipleStudent = new String[10];
public Student()
{
}
public Student(String n, int g)
{
name = n;
grade = g;
}
public String setMultipleStudents()
{
String n = "";
for(int i = 1; i < multipleStudent.length; i++)
{
System.out.println("Enter student #" + i +" name: " );
n = scan.nextLine();
multipleStudent[i] = n;
}
return null;
}
public void multiplegradeinputs()
{
for(int i = 1; i <multiplegradeinputs.length; i++)
{
System.out.println("Enter the Grade of the student#" + i +" : ");
grade = scan.nextInt();
multiplegradeinputs[i] = grade;
}
}
public String toString()
{
String temp = "";
for(int i = 1; i < multipleStudent.length; i++)
{
temp += multipleStudent[i] + " ";
}
return temp;
}
}
this is the 2nd class
import java.util.Scanner;
public class students
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("How many students?: ");
int numofstudents = keyboard.nextInt();
Student s = new Student();
s.setMultipleStudents();
s.toString();
System.out.println("Enter the Grade for the student: ");
int gradeofstudnets = keyboard.nextInt();
}
}
You are missing a return value in the multiplegradeinputs() method.

Java: The constructor gerbil(int) is undefined

So I've read over all of the constructor undefined posts on stackoverflow and tried the solutions and they haven't worked for me. Maybe I'm trying it wrong. I keep getting "the constructor Gerbil(int) is undefined."
The code that's the problem:
GerbilArray[i] = new Gerbil(i);
My full code:
import java.util.Scanner;
public class Gerbil {
public String name;
public String id;
public String bite;
public String escape;
public Gerbil() {
this.name = "";
this.id = "";
this.bite = "";
this.escape = "";
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("How many foods?");
int totalFood = keyboard.nextInt();
System.out.println("How many gerbils in the lab?");
int numberOfGerbils = keyboard.nextInt();
Gerbil[] GerbilArray = new Gerbil[numberOfGerbils];
for(int i = 0; i <= numberOfGerbils; i++){
GerbilArray[i] = new Gerbil(i);
System.out.print("Lab ID:");
String id = keyboard.next();
System.out.print("Gerbil Nickname:");
String name = keyboard.next();
System.out.print("Bite?");
String bite = keyboard.next();
System.out.print("Escapes?");
String city = keyboard.nextLine();
for (int j = 0; j < totalFood; j++) {
System.out.println("How many of food " + (j+1) + "do you eat?:");
}
}
}
}
Also you've probably seen that my nested for-loop isn't finished as well. I'm trying to make an array inside of an object that will store "x" amount of integers inside of my object listed from the user (int totalFood) but I have no idea how.
You don't have a constructor Gerbil(int a) in the class Gerbil and you try to call it!
Just call it this way:
GerbilArray[i] = new Gerbil();

constructor ConferenceRooms in class ConferenceRooms cannot be applied to given types;

Can't figure out this compiling error...
import java.util.*;
import java.util.Scanner;
import java.io.*;
/**
* Write a description of class Bookings here.
*
* #author (your name)
* #version (a version number or a date)
*/
public class Bookings
{
int day = 5;
int hour = 8;
boolean RunMeOnce = false;
Scanner input = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
String fileName;
ConferenceRooms[] conRoom = new ConferenceRooms[3]; // 0 = room 1, 1 = room 2, 2 = room 3
MeetingRooms[] meetRoom = new MeetingRooms[3];
OfficeRooms[] offRoom = new OfficeRooms[3];
public Bookings(String [] args){
}
public void main(){
if (RunMeOnce = false)
{
for(int i=0; i<3; i++){
conRoom[i] = new ConferenceRooms(); //<<<<<<< ERROR OCCURS HERE
meetRoom[i] = new MeetingRooms();
offRoom[i] = new OfficeRooms();
}
RunMeOnce = true;
}
System.out.println("Hello and welcome to One Trinity Greens Room Booking, please choose an option from the list below.");
System.out.println("1. View availabile rooms");
System.out.println("2. Make a booking");
System.out.println("3. Amend a booking");
System.out.println("4. Delete a booking");
int selection = input.nextInt();
switch(selection){
case 1:
for(int i=0; i<3; i++){
System.out.println("Conference Rooms " + i+1);
conRoom[i].conferenceBookings();
System.out.println("Meeting Rooms " + i+1);
meetRoom[i].meetingBookings();
System.out.println("Office Rooms " + i+1);
offRoom[i].officeBookings();
}
break;
case 2:
break;
case 3:
break;
case 4:
break;
}
}
public void searchRooms(){
}
public void accessAvailability(){
}
public void viewBookings(){
}
public void searchBusiness(){
}
public void addBooking(){
}
public void deleteAllBookings(){
}
public void amendBooking(){
}
public void cancelBooking(){
}
}
public ConferenceRooms(String pbusinessName, String proomType, String pavailability, String proomInformation, String pmeetingBookings){
super(pbusinessName, proomType, pavailability);
roomInformation = proomInformation;
}
Oh My.. you have a lot of errors...
1. boolean RunMeOnce = false; // '=' is assigning, '==' is comparing
This is not declared as static . So, you will need an object of type Bookings to call it.
Change your code in main() to
Bookings bk = new Bookings();
if (bk.RunMeOnce)
2. You dont need 2 Scanners to the same Stream (InputStream)
Scanner input = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
3. Show us your conferenceRoom and other classes for discovering more errors :)
You are not calling the constructor correctly. If the ConferenceRooms constructor you've provided is the only one you have, then it's clear why you're getting the error: provide the missing arguments!

Categories

Resources