Blue J - identifier expected message? [duplicate] - java

This question already has answers here:
Java Reserve Keywords
(4 answers)
Closed 7 years ago.
i'm writing the following code and i'm getting the error 'identifier expected' on the line
private String class;
public class Records
{
private String name;
private String class;
private int cabinNumber;
public Records(String n, String c, int cn)
{
name = n;
class = c;
cabinNumber = cn;
}
public void setClass (String c){
class = c;
}
public void set cabinNumber (int cn){
cabinNumber = cn;
}
public String name(){
return name;
}
public String class(){
return class;
}
public int cabinNumber(){
return cabinNumber;
}
}
can someone please explain why this is happening and what I can do to fix it?
thank you!

class is a java keyword, you cannot have a variable with this name.

Related

Why is in Java wrong constructor called? [duplicate]

This question already has answers here:
java "void" and "non void" constructor
(5 answers)
Closed 2 years ago.
This a class with 2 constructors:
public class Product {
private String name;
private boolean[] r;
private boolean[] i;
public Product(String name, boolean[] r, boolean[] i) {
this.name = name;
this.r = r;
this.i = i;
}
public Product Product(String name, String r, String i){
this.name= name;
this.r= BooleanStringHelper.parse(r,'1');
this.i= BooleanStringHelper.parse(i, '1');
return this;
} }
if I call in the main() the following code:
Product p = new Product("MyProduct", "000001111100", "111100000011");
then the first constructor is called, but I would like to call the second constructor
public Product Product(String name, String r, String i)
Why is wrong constructor called?
The second one is not a constructor, it's a method.

Why is there a non-static variable error on my constructor? [duplicate]

This question already has answers here:
Inner classes in Java - Non static variable error
(4 answers)
Java inner class and static nested class
(28 answers)
Closed 2 years ago.
I'm having trouble getting my code to compile. I've stared at this code for hours and I have no idea what is wrong.
Here's my code:
code
public class test{
class University {
private String name;
private int enrollment;
private boolean urban;
public University() {
name = "TSU";
enrollment = 8000;
urban = true;
}
public University(String name, int enrollment, boolean urban) {
this.name = name;
this.enrollment = enrollment;
this.urban = urban;
}
public int getEnrollment() {
return enrollment;
}
public void setEnrollment(int n) {
if (n > 0) {
n = n;
} else {
n = 0;
}
}
}
public static void main(String[] args) {
University tsu = new University();
University vu = new University("Vandy", 5800, true);
University mt = new University("MTSU", 7000, false);
tsu.setEnrollment(8500);
vu.setEnrollment(60000);
System.out.println(tsu.name + "\'s enrollment is " + tsu.getEnrollment());
}
}
This is the error I keep getting:
error
test.java:36: error: non-static variable this cannot be referenced from a static context
University tsu = new University();
^
Thank you in advance for your help!
Your code defines University as a non-static class (see Nested Classes for a more detailed explanation). You should instead declare University as follows:
static class University {
// ...
}

java.lang.Object cannot be converted to the class i created [duplicate]

This question already has answers here:
What are Generics in Java? [closed]
(3 answers)
Closed 3 years ago.
im having a problem calling a method i created for a class when it is returned from a list. I get a "java.lang.Object cannot be converted to Thing"
error when running the following code
public class Test1
{
public static void main(String args[])
{
Thing whipersnaper = new Thing(30, "whipersnaper");
List storage = new List();
storage.insert(whipersnaper);
Thing x = storage.getItem(0);
x.doubleWeight();
System.out.println(x.getWeight());
}
}
here is the "Thing" class
public class Thing
{
private int weight;
private String name;
public Thing(int weight,String name){
this.weight = weight;
this.name = name;
}
public void doubleWeight(){
this.weight *= 2;
}
public int getWeight(){
return this.weight;
}
}
finally here is the List class
public class List
{
private Object[] itemList;
private int size;
public List()
{
this.itemList = new Object[10];
this.size = 0;
}
public void insert(Object item){
itemList[size] = item;
size++;
}
public Object getItem(int index){
return itemList[index];
}
}
i need the list to be able to hold objects of any type and not exclusively Thing objects.
i have tried to google a solution but I cant find a good way to phrase the question to get an answer. thanks in advance.
Change that line Thing x = storage.getItem(0); with Thing x = (Thing) storage.getItem(0);
Thing x = (Thing) storage.getItem(0);

Java.lang.NullPointerException error in Java program [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 9 years ago.
Keep getting this error, sorry I am a beginner in Java.
Exception in thread "main": java.lang.NullPointerException
at assignment01.Student.addGrade(Student.java:28)
at assignment01.GpaTest.main(GpaTest.java:11)
package assignment01;
public class Grades
{
private double qualPts;
private int numCred;
public double getGPA()
{
if(numCred!=0)
{
return(qualPts/numCred);
}
return numCred;
}
public void addGrade(int creds, double grade)
{
grade+=creds+numCred;
qualPts+=creds*grade;
}
public int getNumCred()
{
return numCred;
}
}
.
package assignment01;
public class Student
{
private String name;
private String bNumber;
private Grades grades;
public Student(String name, String bNumber)
{
this.name=name;
this.bNumber=bNumber;
}
public void addGrade(int creds, double grade)
{
grades.addGrade(creds, grade);
}
.
package assignment01;
public class GpaTest {
public static void main(String[] args)
{
Student theStudent= new Student("Ethan","00000000");
int CREDITS_ENROLLED1=4;
double GRADE1=90;
theStudent.addGrade(1, 100);
theStudent.addGrade(CREDITS_ENROLLED1,GRADE1);
System.out.println("Determining the grades of student named Ethan.");
System.out.println("Ethan has a grade of 90.");
}
}
grades variable is not initialized. You need to initialize it inside Student constructor:
public Student(String name, String bNumber)
{
this.name=name;
this.bNumber=bNumber;
this.grades = new Grades();
}
You may wish to initialize qualPts and numCred as well
Initialize the object 'grades'.

java.lang.NullPointerException error how to fix? [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
having problem with null pointer exception and i read few article bout that error and still coundnt figure out what the problem.
The error happen at CompatibleActivity[topIndex]=new Activity(aNum,bNum,c);
topIndex=0 by the way.
Can anyone highlight the problem im having?
here my class
public class Schedulingtest {
public static void main (String[] args) throws IOException
{
Scanner fileScan;
fileScan=new Scanner(new File("data.txt"));
Schedule compatibility = new Schedule();
while(fileScan.hasNext())
{String url=fileScan.nextLine();
compatibility.addActivity(url);
}
}
public class Schedule{
import java.util.Scanner;
import java.util.Arrays;
public class Schedule {
Activity[] CompatibleActivity;
int totalTime=0,topIndex=0;
Scanner urlScan;
public Schedule(){
Activity[] CompatibleActivity=new Activity[30];
}
public int getTotalTime()
{return totalTime;
}
public void addActivity(String entry){
urlScan=new Scanner(entry);
urlScan.useDelimiter(" ");
String c=null;
int aNum = 0,bNum=0;
while(urlScan.hasNext())
{String a=urlScan.next();
String b=urlScan.next();
c=urlScan.next();
aNum=Integer.parseInt(a);
bNum=Integer.parseInt(b);
}
CompatibleActivity[topIndex]=new Activity(aNum,bNum,c);
topIndex++;
System.out.println("Activity added: start "+aNum+ " stop "+bNum+" "+c );
}
}
Activity Class
public class Activity {
private int start,stop,duration;
private String name;
public Activity(int Start,int Stop,String Name)
{
start=Start;
stop=Stop;
name=Name;
duration=Stop-Start;
}
public String getName()
{return name;
}
public int getStart()
{return start;
}
public int getStop()
{return stop;
}
public int getDuration()
{return duration;
}
public boolean compatible(int Start1,int Stop1,int toMatchsideStart,int toMatchsideStop)
{
int Start=Start1;
int Stop=Stop1;
int toMatchStart=toMatchsideStart;
int toMatchStop=toMatchsideStop;
if(toMatchStop<=Start)
{return true;
}
if(toMatchsideStart>=Stop)
{return true;
}
else
{return false;}
}
public String toString()
{return( name+"<"+start+","+stop+">"); }
}
Most likely you have CompatibleActivity declared in your class as
private Activity[] CompatibleActivity;
which declares a reference and initializes it to null by default. You need to assign it a real array with enough elements (e.g. in your constructor):
CompatibleActivity = new Activity[myBigNumber];
If the NullPointerException is definitely on that line, then it can only be caused by CompatibleActivity being null. You will need to find in your code where that Array Object is declared and make sure that it is also instantiated, e.g.
Activity[] CompatibleActivity = new Activity[size];
Check if you've initialized the array before you access one of its cells. You need an expression like
CompatibilityActivity = new Activity[1]; // or any other positve number if size is bigger
Activity[] CompatibleActivity = new Activity[size];
// put some element in it like
CompatibleActivity[0]=new CompatibleActivity();

Categories

Resources