I get the following mistake when I compile it [duplicate] - java

This question already has answers here:
Missing method body, or declare abstract in Java
(4 answers)
Closed 7 years ago.
public class Hw7pr1
{
public static void main(String[] args);
String eName;
double payrate;
public Hw7pr1(String name, double rate)
{
eName = name;
payrate = rate;
}
public String getEName()
{
return eName;
}
public void setEName(String name)
{
eName = name;
}
public double getPayrate()
{
return payrate;
}
public void setPayrate(double payrate)
{
this.payrate = payrate;
}
}
This is the mistake I get when compiling it.
Hw7pr1.java:3: error: missing method body, or declare abstract
public void main(String[] args);
^

Take out the semicolon after your main method declaration
should be like this
public static void main(String[] args) {
...
}

Remove ";" at the end of your main method and add instead a bracket
public static void main(String[] args){
and at the end of your main method (which seems not really used - empty) a closing one
}
The class:
public class Hw7pr1 {
public static void main(String[] args) {
// do something
}
String eName;
double payrate;
public Hw7pr1(String name, double rate) {
eName = name;
payrate = rate;
}
public String getEName()
{
return eName;
}
public void setEName(String name)
{
eName = name;
}
public double getPayrate()
{
return payrate;
}
public void setPayrate(double payrate)
{
this.payrate = payrate;
}
}

Related

Why Do i get null value in the following code concerning java inheritence

I will provide the code and the output given by the compiler for better understanding of the problem. You will notice a null value for Registration Number...there is the problem.
You will notice at the output that i get null value for Registration number...i don't get it because it all seemed right, until it wasn't.😒
public class Student {
private String regNo;
//Student student = new Test();
public String getRegNo() {
return regNo;
}
public void setRegNo(String number) {
this.regNo = regNo;
}
public void displayRegistrationNumber(){
System.out.println("Registration number: "+ getRegNo());
}
}
-----------------------------------------------------------------'
public class Test extends Student{
private double sub1, sub2;
public double getSub1() {
return sub1;
}
public void setSub1(double sub1) {
this.sub1 = sub1;
}
public double getSub2() {
return sub2;
}
public void setSub2(double sub2) {
this.sub2 = sub2;
}
public void displayScore(){
System.out.printf("Scores: Subject1 = %.0f, Subject2 = %.0f \n",getSub1(), getSub2());
}
}
-----------------------------------------------------------------------
public class Results extends Test {
private double total;
public double getTotal() {
return total;
}
public void setTotal(double total) {
this.total = total;
}
public void displayTotalScores(){
setTotal(getSub1()+getSub2());
System.out.printf("Total marks: %.0f \n", getTotal());
}
}
---------------------------------------------------------------------
public class Main_method {
public static void main(String[] args){
Results results = new Results();
results.setRegNo("2017-04-06859");
results.setSub1(75.47);
results.setSub2(89);
results.displayRegistrationNumber();
results.displayScore();
results.displayTotalScores();
}
}
Here's the output. Notice the null
Registration number: null
Scores: Subject1 = 75, Subject2 = 89
Total marks: 164 `
Here instead of assigning parameter number to regNo you just reassigned its own value (which is null initially) to it:
public void setRegNo(String number) {
this.regNo = regNo;
}
so change it to:
this.regNo = number;
This is an expected behavior, the argument you passed to public void setRegNo(String number) is never used.
this.regNo = regNo;
reassigns the the regNo declared in Student class (which in this case is null ) back to the Object which calls it.
change the argument or the assignment statement
i.e
public void setRegNo(String regNo)
or
this.regNo = number;

Error with writing to a file in Java [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I am getting the following errors.
Exception in thread "main" java.lang.NullPointerException at ReadFile.createDogs(ReadFile.java:41) at ReadFile.main(ReadFile.java:55)
The file is opening to the desktop. I feel I am missing something very
simple. I have included the class file also. The error is specific to
dogOutput.println(dogInfo);
Thank you.
import java.io.*;
import java.io.PrintWriter;
public class ReadFile
{
private static Canine[] getCanine()
{
Canine[] canines= new Canine[5];
canines[0]=new Canine("Thoedore", "Male", "Medium", 7, 50);
canines[1]=new Canine("Magellan", "Male", "Medium", 5, 50);
canines[2]=new Canine("Duffy", "Male", "Small", 10, 10);
canines[3]=new Canine("Sammie", "Female", "Medium", 7, 65);
canines[4]=new Canine("Snowball", "Male", "Small", 13, 17);
return canines;
}
private static PrintWriter createFile(String fileName)
{
try{
File listOfDogs = new File(fileName);
PrintWriter infoToWrite= new PrintWriter
( new BufferedWriter
(new FileWriter(listOfDogs)));
}
catch (IOException e)
{
System.out.println(" An I/O Error Occurred.");
System.exit(0);
}
return null;
}
private static void createDogs(Canine dog, PrintWriter dogOutput)
{
String dogInfo= dog.name + " " + dog.gender+ " " + dog.size +" ";
dogInfo += Integer.toString(dog.age)+ " "+
Double.toString(dog.weight);
dogOutput.println(dogInfo);
}
public static void main (String[] args)
{
Canine[] canines= getCanine();
System.out.println("Here1");
PrintWriter dogOutput =
createFile("/Users/Teacher/Desktop/Dogs.text");
for(Canine dogs: canines)
{
createDogs(dogs, dogOutput);
System.out.println("Here5");
}
dogOutput.close();
}
private static class Canine extends Pet
{
public static final String type="Dog";
public Canine()
{
super.type=this.type;
}
public Canine(String name, String gender, String size, int age, double
weight)
{
super.type=this.type;
super.name=name;
super.gender=gender;
super.size=size;
super.age=age;
super.weight=weight;
}
public static void setType(String word)
{
}
}
private static class Pet
{
public static String type;
public static String color;
public static String gender;
public static String temperment;
public static String food;
public static int age;
public static double weight;
public static String name;
public static String size;
public Pet()
{
}
public Pet(String type,String color,String gender,String temperment,
String food,int age,double weight,String name,String size)
{
type=type;
color=color;
gender=gender;
temperment=temperment;
food=food;
age=age;
weight=weight;
name=name;
size=size;
}
public Pet(String name, String gender, String size, int age, double
weight)
{
name=name;
gender=gender;
size=size;
age=age;
weight=weight;
}
public static String getType()
{
return type;
}
public static String getName()
{
return name;
}
public static String getSize()
{
return size;
}
public static String getColor()
{
return color;
}
public static String getTemperment()
{
return temperment;
}
public static String getFood()
{
return food;
}
public static String getGender()
{
return gender;
}
public static int getAge()
{
return age;
}
public static void setType(String word)
{
type=word;
}
public static void getName(String word)
{
name=word;
}
public static void setSize(String word)
{
size=word;
}
public static void setColor(String word)
{
color=word;
}
public static void getTemperment(String word)
{
temperment=word;
}
public static void setFood(String word)
{
food=word;
}
public static void setGender(String word)
{
gender=word;
}
public static void setAge(int ageSet)
{
age=ageSet;
}
public static void setWeight(double weightSet)
{
weight=weightSet;
}
public static void setName(String NameSet)
{
name=NameSet;
}
public String toString()
{
return ("Type "+type+"\n" +" Name "+name+"\n"+" Age "+age+"\n"+
"Size "+ size+"\n"+" Gender "+gender+"\n"+" Weight "+weight);
}
}
}
Try renaming the dogOutput variable. In java, you can't pass variables between static methods with the same name.
The simple answer: In createFile() you always return null instead of the instance of PrintWriter you just created as infoToWrite.
The complete answer: You need to strengthen your knowledge of OOP. The unnecessary use of static members, for instance, can have undesired effects.

How to acces a void method from another class?

how do I call a void method from another class to a new class with main?
I have two classes, but I don't see the error I am making.
public class Person {
private int age;
private String name;
public Person(int a, String n) {
a = age;
n = name;
}
public void printInfo() {
System.out.println(age + name);
}
//
public class Main {
public static void main(String[] args) {
Person obj1 = new Person(22, "Dan");
obj1.printInfo();
}
}
EDIT: Move the main method to a different class and done.
TestPerson.java
public class TestPerson {
public static void main(String[] args) {
Person obj1 = new Person(22, "Dan");
obj1.printInfo();
}
}
You have few mistakes in your code. It should be like as follows :
public class Person {
private int age;
private String name;
//Your constructor was wrong
public Person(int a, String n) {
age = a;
name = n;
}
public void printInfo() {
System.out.println(age + name);
}
}

can i print a setter value (sysout) or do i have to use only getters in order to get an output ? (java)

I have just recently learned about setter , getters and this.(somthing).
I having quite a hard time undersatnding when to use getters and when to use setters .
Another thing , can i use setter method to print out ?
For Example :
class workerId {
private int workerAge;
private String workerName;
private int workerIde;
public void setWorkerAge(int newAge) {
newAge = workerAge;
}
public void setWorkerName(String newName) {
newName = workerName;
}
public int setIde(int ide) {
ide = workerIde;
return ide;
}
}
public class App {
public static void main(String[] args) {
workerId worker1 = new workerId();
worker1.setWorkerAge(41);
worker1.setWorkerName("dan ");
worker1.setIde(318574524);
System.out.println(worker1.setIde());
}
}
the system out print shows an error and i didnt understand why , is it because only getters can be used in the sysout command ?
No offense intended, but your setters are all wrong. You should assign your properties to the values passed in the setter, not setting the value again. So your code should look like this:
class workerId {
private int workerAge;
private String workerName;
private int workerIde;
public void setWorkerAge(int newAge) {
workerAge = newAge;
}
public void setWorkerName(String newName) {
workerName = newName;
}
public int setIde(int ide) {
workerIde = ide;
}
}
If you need getters, it should look like this:
class workerId {
private int workerAge;
private String workerName;
private int workerIde;
public void setWorkerAge(int newAge) {
workerAge = newAge;
}
public void setWorkerName(String newName) {
workerName = newName;
}
public int setIde(int ide) {
workerIde = ide;
}
public int getIde() {
return workerIde;
}
}
Then you can print, e.g. System.out.println(worker1.getIde());
You should be using a getter method to get the values.
class workerId {
private int workerAge;
private String workerName;
private int workerIde;
public void setWorkerAge(int newAge) {
workerAge = newAge;
}
public void setWorkerName(String newName) {
workerName=newName;
}
public int getIde() {
return workerIde;
}
public void setIde(int ide) {
workerIde = ide;
}
}
public class App {
public static void main(String[] args) {
workerId worker1 = new workerId();
worker1.setWorkerAge(41);
worker1.setWorkerName("dan ");
worker1.setIde(318574524);
System.out.println(worker1.getIde());
}
}
class workerId {
private int workerAge;
private String workerName;
private int workerIde;
public void setWorkerAge(int newAge) {
this.workerAge = newAge;
}
public void setWorkerName(String newName) {
this.workerName = newName;
}
public int setIde(int ide) {
this.workerIde = ide;
return this.workerIde;
}
}
public class Car {
public static void main(String[] args) {
workerId worker1 = new workerId();
worker1.setWorkerAge(41);
worker1.setWorkerName("dan ");
worker1.setIde(318574524);
System.out.println(worker1.setIde(56));
}
}

Java main class

Here is my java code
public class Account {
private String accName;
private String accId;
private int balance;
Account()
{
System.out.println("This is an empty constructor.");
}
Account(String name)
{
accName = name;
System.out.println("This is an valued constructor.");
}
public class main{
public Static void main(String[] args)
{
Account a1 = new Account("raff");
}
}
}
cmd says ' "(identifier)" expected
public Static void main(String[] args)'
I can't get the problem yet......
You have several problems there:
public class within public class
main shouldn't be a class there at all
"Static", a mispelling of the static keyword
Probably this is how you meant to write that:
public class Account {
private String accName;
private String accId;
private int balance;
Account() {
System.out.println("This is an empty constructor.");
}
Account(String name) {
accName = name;
System.out.println("This is an valued constructor.");
}
public static void main(String[] args) {
Account a1 = new Account("raff");
}
}
The static keyword, like all other Java keywords, should be written in lowercase:
public static void main(String[] args) {

Categories