Java: The constructor gerbil(int) is undefined - java

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

Related

Cannot find symbol error Netbeans

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.

Java passing class array into array

I am a student and looking for help with an assignment. Here is the task: Create a CollegeCourse class. The class contains fields for the course ID (for example, “CIS 210”), credit hours (for example, 3), and a letter grade (for example, ‘A’).
Include get() and set()methods for each field. Create a Student class containing an ID number and an array of five CollegeCourse objects. Create a get() and set() method for the Student ID number. Also create a get() method that returns one of the Student’s CollegeCourses; the method takes an integer argument and returns the CollegeCourse in that position (0 through 4). Next, create a set() method that sets the value of one of the Student’s CollegeCourses; the method takes two arguments—a CollegeCourse and an integer representing the CollegeCourse’s position (0 through 4).
I am getting runtime errors on the second for loop where I am trying to get the data into the course array. It is asking for both the CourseID and Hours in the same line and regardless of what I respond with it I am getting an error, it almost seems like it is trying to get all the arrays variables at the same time. Here is my code which includes three classes. Any help to send me in the right direction is appreciated as I have spent a ton of time already researching to resolve.
public class CollegeCourse {
private String courseId;
private int creditHours;
private char grade;
public CollegeCourse(String id, int hours, char grade)
{
courseId=id;
creditHours = hours;
this.grade = grade;
}
public void setCourseId(String id)
{
courseId = id;//Assign course id to local variable
}
public String getCourseId()
{
return courseId;//Provide access to course id
}
public void setHours(int hours)
{
creditHours = hours;//Assign course id to local variable
}
public int getHours()
{
return creditHours;//Provide access to course id
}
public void setGrade(char grade)
{
this.grade = grade;//Assign course id to local variable
}
public char getGrade()
{
return grade;//Provide access to course id
}
}
Student Class
public class Student {
final int NUM_COURSES = 5;
private int studentId;
private CollegeCourse courseAdd;//Declares a course object
private CollegeCourse[] courses = new CollegeCourse[NUM_COURSES];
//constructor using user input
public Student(int studentId)
{
this.studentId=studentId;
}
public void setStudentId(int id)
{
studentId = id;//Assign course id to local variable
}
public int getStudentId()
{
return studentId;//Provide access to course id
}
public void setCourse(int index, CollegeCourse course)
{
courses[index] = course;
}
public CollegeCourse getCourse(int index)
{
return courses[index];
//do I need code to return the courseId hours, grade
}
}
InputGrades Class
import java.util.Scanner;
public class InputGrades {
public static void main(String[] args) {
final int NUM_STUDENTS = 2;
final int NUM_COURSES = 3;
Student[] students = new Student[NUM_STUDENTS];
int s;//subscript to display the students
int c;//subscript to display courses
int stId;
int csIndex;
String courseId = "";
int hours = 0;
//String gradeInput;
char grade = 'z';
CollegeCourse course = new CollegeCourse(courseId,hours, grade);//not sure if I am handling this correctly
Scanner input = new Scanner(System.in);
for(s = 0; s<NUM_STUDENTS; ++s)
{
students[s] = new Student(s);
System.out.print("Enter ID for student #" + (s+1) + ":");
stId = input.nextInt();
input.nextLine();
students[s].setStudentId(stId);
for(c=0; c < NUM_COURSES; ++c)
{
csIndex=c;
System.out.print("Enter course ID #" + (c+1) + ":");
courseId = input.nextLine();
course.setCourseId(courseId);
System.out.print("Enter hours:");
hours = input.nextInt();
input.nextLine();
course.setHours(hours);
String enteredGrade = "";
while(enteredGrade.length()!=1) {
System.out.print("Enter grade:");
enteredGrade = input.nextLine();
if(enteredGrade.length()==1) {
grade = enteredGrade.charAt(0);
} else {
System.out.println("Type only one character!");
}
}
course.setGrade(grade);
students[s].setCourse(csIndex, course);
}
}
for(s = 0; s<NUM_STUDENTS; ++s)
{
System.out.print("\nStudent# " +
students[s].getStudentId());
System.out.println();
for(c=0;c<NUM_COURSES;++c)
System.out.print(students[s].getCourse(c) + " ");
System.out.println();
}
}
}
After input.nextInt() you need to add one more input.nextLine(); and than you can read grade.
System.out.print("Enter hours:");
hours = input.nextInt();
input.nextLine();
course.setHours(hours);
Why it is needed? See this question: Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods
You should add a very simple length validation when you input the grade:
String enteredGrade = "";
while(enteredGrade.length()!=1) {
System.out.print("Enter grade:");
enteredGrade = input.nextLine();
if(enteredGrade.length()==1) {
grade = enteredGrade.charAt(0);
} else {
System.out.println("Type only one character!");
}
}
so the full main class code:
import java.util.Scanner;
/**
* Created by dkis on 2016.10.22..
*/
public class App {
public static void main(String[] args) {
final int NUM_STUDENTS = 10;
final int NUM_COURSES = 5;
Student[] students = new Student[NUM_STUDENTS];
//String name;
int s;//subscript to display the students
int c;//subscript to display courses
int stId;
int csIndex;
String courseId = "";
int hours = 0;
char grade = 'z';
CollegeCourse course = new CollegeCourse(courseId,hours, grade);//not sure if I am handling this correctly
Scanner input = new Scanner(System.in);
for(s = 0; s<NUM_STUDENTS; ++s)
{
students[s] = new Student(s);
System.out.print("Enter ID for student #" + s+1 + ":");
stId = input.nextInt();
input.nextLine();
students[s].setStudentId(stId);
for(c=0; c < NUM_COURSES; ++c)
{
//CollegeCourse course = students[s].getCourse(c);
csIndex=c;
System.out.print("Enter course ID#" + c+1 + ":");
courseId = input.nextLine();
course.setCourseId(courseId);
System.out.print("Enter hours:");
hours = input.nextInt();
input.nextLine();
course.setHours(hours);
String enteredGrade = "";
while(enteredGrade.length()!=1) {
System.out.print("Enter grade:");
enteredGrade = input.nextLine();
if(enteredGrade.length()==1) {
grade = enteredGrade.charAt(0);
} else {
System.out.println("Type only one character!");
}
}
course.setGrade(grade);
students[s].setCourse(csIndex, course);
}
}
for(s = 0; s<NUM_STUDENTS; ++s)
{
System.out.print("\nStudent# " +
students[s].getStudentId());
for(c=0;c<NUM_COURSES;++c)
System.out.print(students[s].getCourse(c) + " ");
System.out.println();
}
}
}

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.

Methods' outputting twice with arrays

I have to basically make a program that would add a name given a prompt and place it in the array. It should do the same for the age of the person. I have been forced to do this with methods. The only issue i am having is that with the 3rd last line, the name get's asked twice. I don't know how to fix that. Any help is appreciated.
public class Testing1 {
public static int[] ageinput(String names[], int q2){
int holderage[] = new int[q2];
for(int x = 0; x<q2;x++) {``
System.out.println("Please input the age of " + names[x]);
Scanner age = new Scanner(System.in);
int a1 = age.nextInt();
holderage[x] = a1;
}
return holderage;
}
public static String[] nameinput(int q2){
String holdername[] = new String[q2];
for (int x = 0; x<q2;x++) {
System.out.println("Please input the name of the person");
Scanner name = new Scanner(System.in);
String n1 = name.nextLine();
holdername[x]=n1;
}
return holdername;
}
public static void output(String names[], int ages[]){
for(int x = 0; x<names.length;x++){
System.out.println(names[x]+" is "+ages[x]+" years old");
}
}
public static void main(String[] args) {
System.out.println("How many names do you want to input?");
Scanner question = new Scanner(System.in);
int q1 = question.nextInt();
output(nameinput(q1),ageinput(nameinput(q1),q1));
}
}
In output(nameinput(q1),ageinput(nameinput(q1),q1)); you call the method nameinput twice, so the code also will be executed twice.
You could ask the names in nameinput, store them into an array and pass that array to ageinput.
#Aadithya Gowthaman, try this one in your editor.
package com.aamir;
import java.util.Scanner;
public class Testing1 {
public static int[] ageinput(String names[], int q2){
int holderage[] = new int[q2];
for(int x = 0; x<q2;x++) {
System.out.println("Please input the age of " + names[x]);
Scanner age = new Scanner(System.in);
int a1 = age.nextInt();
holderage[x] = a1;
}
return holderage;
}
public static String[] nameinput(int q2){
String holdername[] = new String[q2];
for (int x = 0; x<q2;x++) {
System.out.println("Please input the name of the person");
Scanner name = new Scanner(System.in);
String n1 = name.nextLine();
holdername[x]=n1;
}
return holdername;
}
public static void output(String names[], int ages[]){
for(int x = 0; x<names.length;x++){
System.out.println(names[x]+" is "+ages[x]+" years old");
}
}
public static void main(String[] args) {
System.out.println("How many names do you want to input?");
Scanner question = new Scanner(System.in);
int q1 = question.nextInt();
String [] namesArray = nameinput(q1);
int [] ageArray = ageinput(namesArray, q1);
output(namesArray, ageArray);
}
}

Creating an array of arrays in Java

Say I have a Player class such as:
public class Player {
String name;
int chips;
int betVal;
}
Is the following code correct for creating the array of the players?
public static void main(String[] args) {
int playerCount;
int startingChip;
out.print("How many players? ");
playerCount = myScanner.nextInt();
Player[] aPlayer = new Player[playerCount + 1];
for (int i = 0; i < playerCount + 1; i++){
aPlayer[i] = new Player();
}
out.print("Enter starting chip amount: ");
startingChip = myScanner.nextInt();
}
If so, how would I assign the name, chip amount and the betVal to each player? How would I access and alter them later on in the code?
EDIT: Will it be easier leaving the Player as an object or an array (name,chips,betVal) for accessing it later on?
First off, your variables need to be private (there's a section of programmers who prefer public variables but most prefer private).
You could set the values of the Player object either through an overloaded constructor or through the setters.
public class Player {
private String name;
private int chips;
private int betVal;
public Player(){
//default constructor to initialize without any parameters
}
public Player(String name, int chips, int betVal){
this.name=name;
this.chips=chips;
this.betVal=betVal;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChips() {
return chips;
}
public void setChips(int chips) {
this.chips = chips;
}
public int getBetVal() {
return betVal;
}
public void setBetVal(int betVal) {
this.betVal = betVal;
}
}
In which case, your initialization could be either
aPlayer[i] = new Player("Jason", 5,1000);
or using the setters as in
for (int i = 0; i < playerCount + 1; i++){
aPlayer[i] = new Player();
}
aPlayer[i].setName("Jason");
aPlayer[i].setChips(5);
aPlayer[i].setBetVal(1000);
Considering your sample program, I guess option 2 plays well.
You can access each player by telling the array which one you want to access.
`aPlayer[0].name = "JAG";`
would work for example.
Why not ask all the user's inputs before creating anything ? For exemple :
public static void main(String[] args) {
int playerCount;
int startingChip;
out.print("How many players? ");
playerCount = myScanner.nextInt();
out.print("Enter starting chip amount: ");
startingChip = myScanner.nextInt();
Player[] aPlayer = new Player[playerCount];
for (int i = 0; i < playerCount; i++){
aPlayer[i] = new Player();
aPlayer[i].setChips(startingChip);
}
}
You should also use a List. And finally, if you want to ask for the name of each player, do it directly in the loop :
for (int i = 0; i < playerCount; i++){
aPlayer[i] = new Player();
aPlayer[i].setChips(startingChip);
out.print("What's the player " + i + " name? ");
aPlayer[i].setName(myScanner.next());
}
try something like this...
ask for individual players info in the loop itself.
System.out.println("How many players? ");
Scanner myScanner = new Scanner(System.in);;
playerCount = myScanner.nextInt();
Player[] aPlayer = new Player[playerCount];
for (int i = 0; i < playerCount; i++){
aPlayer[i] = new Player();
System.out.println("Enter Name for Player " + i+1);
String name = myScanner.next();
System.out.println("Enter chips for Player " + i+1);
int chips = myScanner.nextInt();
System.out.println("Enter betVal for Player " + i+1);
int betVal = myScanner.nextInt();
aPlayer[i].name = name;
aPlayer[i].chips = chips;
aPlayer[i].betVal = betVal;
}
Also make the instance variables of Player class private, and access them using getters and setters.

Categories

Resources