//this is my main class
import java.io.*;
import java.util.*;
public class TheInnovator{
private String name;
private String age;
private String designation;
private String course;
private String yrlvl;
public TheInnovator(String name, String age, String designation, String course, String yrlvl){
this.name = name;
this.age = age;
this.designation = designation;
this.course = course;
this.yrlvl = yrlvl;
}
public void setName(String name){
this.name = name;
}
public void setAge(String age){
this.age = age;
}
public void setDesignation (String designation){
this.designation = designation;
}
public void setCourse(String couse){
this.course = course;
}
public void setYrlvl (String yrlvl){
this.yrlvl = yrlvl;
}
public String getName(){
return name;
}
public String getAge(){
return age;
}
public String getDesignation(){
return designation;
}
public String getCourse(){
return course;
}
public String getYrlvl(){
return yrlvl;
}
}
// this is my Main Driver
import java.util.*;
public class MainDriver{
public static void main(String args[]){
TheInnovator theinnov = new TheInnovator();
Scanner input = new Scanner(System.in);
theinnov.setName = (input.nextLine());
theinnov.setAge = (input.nextLine());
theinnov.setDesignation = (input.nextLine());
theinnov.setCourse = (input.nextLine());
theinnov.setYrlvl = (input.nextLine());
System.out.println("Name: " + theinnov.getName());
System.out.println("\nAge: " + theinnov.getAge());
System.out.println("\nDesignation: " + theinnov.getDesignation());
System.out.println("\nCourse: " + theinnov.getCourse());
System.out.println("\nYear Level: " + theinnov.getYrlvl());
}
}
So my problem is everytime I run the MainDriver.java, it cannot find my setter variables. what's wrong or what's missing in my code? thank you for a quik response! anyway I'm using notepad++ on this because it's a requirement.
You need to remove the = sign. It makes the compiler think that setName is a field and not a method.
theinnov.setName (input.nextLine());
class Quick {
public static void main(String[]args) {
Don op=new Don();
Scanner sc=new Scanner(System.in);
System.out.println("enter");
op.setName(sc.nextLine());
System.out.println("Name: " + op.getName());
}
}
class Don
{
private String name;
public void setName(String name)
{
this.name = name;
}
public String getName(){
return name;
}
}
Output: Name: loyal
Related
This question already has answers here:
Why does my ArrayList contain N copies of the last item added to the list?
(5 answers)
Closed 1 year ago.
I have an array in the main class which holds Employee class objects. I'm trying to generate a unique ID for each object but it is printing the same ID for all the objects
Main class:
public static void main(String[] args) {
Employee employee1 = new Employee("Luke", 36);
Employee employee2 = new Employee("Martin", 49);
Employee employee3 = new Employee("Kevin", 21);
Employee employee4 = new Employee("Sam", 43);
Employee employee5 = new Employee("Nicole", 45);
Employee employee6 = new Employee("Linta", 21);
Employee[] allEmployees = { employee1, employee2, employee3, employee4, employee5, employee6 };
for (int i = 0; i < allEmployees.length; i++) {
System.out.println(allEmployees[i]);
}
}
Employee class
public class Employee {
private String name;
private int age;
private static String employeeID = "0";
Employee(String name, int age) {
this.name = name;
this.age = age;
employeeID = getNextUniqueID();
}
public String getName() {
return name;
}
public String setName(String name) {
this.name =name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static String getNextUniqueID() {
int id = Integer.parseInt(employeeID);
++id;
return Integer.toString(id);
}
public String getEmployeeID() {
return employeeID;
}
public String toString() {
return getName() + " " + getAge() + " " + getEmployeeID();
}
}
I want the employeeID as string and I can't use java.util.UUID; for my project.
You need a static variable associated with the class to maintain the unique id and an instance variable to keep that particular employee's ID in the class.
private String employeeID; // instance member
private static String uniqueID = "0"; // static class variable
public static String getNextUniqueID() {
int id = Integer.parseInt(uniqueID); // get the static variable
++id; // increment it
uniqueID = Integer.toString(id); // update the static variable
return uniqueID; // return the value to use for the employee
}
Then in the Employee constructor, use the static member:
Employee(String name, int age) {
this.name = name;
this.age = age;
employeeID = Employee.getNextUniqueID();
}
updated Employee class:
public class Employee {
private String name;
private int age;
private String employeeID;
private static String uniqueID = "0";
Employee(String name, int age) {
this.name = name;
this.age = age;
employeeID = Employee.getNextUniqueID();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static String getNextUniqueID() {
int id = Integer.parseInt(uniqueID);
++id;
uniqueID = Integer.toString(id);
return uniqueID;
}
public String getEmployeeID() {
return employeeID;
}
public String toString() {
return getName() + " " + getAge() + " " + getEmployeeID();
}
}
Output:
Luke 36 1
Martin 49 2
Kevin 21 3
Sam 43 4
Nicole 45 5
Linta 21 6
You should store last generated id in static field but use non static for id of certain employee.
Also you should use AtomicInteger type for thread safety which you can convert to String. Check that:
import java.util.concurrent.atomic.AtomicInteger;
public class Employee {
private String employeeID;
private String name;
private int age;
private static AtomicInteger lastGeneratedId = new AtomicInteger(0);
Employee(String name, int age) {
this.name = name;
this.age = age;
employeeID = getNextUniqueID();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static String getNextUniqueID() {
return String.valueOf(lastGeneratedId.incrementAndGet());
}
public String getEmployeeID() {
return employeeID;
}
public String toString() {
return getName() + " " + getAge() + " " + getEmployeeID();
}
public static void main(String[] args) {
Employee employee1 = new Employee("Luke", 36);
Employee employee2 = new Employee("Martin", 49);
Employee employee3 = new Employee("Kevin", 21);
Employee employee4 = new Employee("Sam", 43);
Employee employee5 = new Employee("Nicole", 45);
Employee employee6 = new Employee("Linta", 21);
Employee[] allEmployees = { employee1, employee2, employee3, employee4, employee5, employee6 };
for (int i = 0; i < allEmployees.length; i++) {
System.out.println(allEmployees[i]);
}
}
}
I'm new to programming and i'd like some help.
I want to make a class that can add name,age and multiple phone numbers ( in some cases it will be 1, in others 4, etc...) and then show all the info.
I don't want to make it by creating another class for the ArrayList,
I'd like to do it all inside this class, I guess it's something simple to do but I can't figure this out and I'm not finding the solution I want.
what can I do about it? thx in advance, first time posting If I did something wrong please tell me.
import java.util.ArrayList;
public class Athlete
{
private String name;
private int age;
private ArrayList<String> phones = new ArrayList();
public Athlete(String name, int age)
{
this.name = name;
this.age = age;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public ArrayList<String> getPhones()
{
return phones;
}
public void setPhones(ArrayList<String> phones)
{
this.phones = phones;
}
#Override
public String toString()
{
return "Athlete{" + "name=" + name + ", age=" + age + ", phones=" + phones + '}';
}
}
You could make an inner class for PhoneList and use that type instead of directly working with the ArrayList.
public class Athlete
{
private String name;
private int age;
private PhoneList phoneList;
public Athlete(String name, int age)
{
this.name = name;
this.age = age;
phoneList = new PhoneList();
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
// Maybe just return the toString (or a read only list)
public PhoneList getPhones()
{
return phoneList;
}
publicvoid addPhone(String phone)
{
phoneList.add(phone);
}
#Override
public String toString()
{
return "Athlete{" + "name=" + name + ", age=" + age + ", phones=" + phoneList + '}';
}
private class PhoneList
{
private ArrayList<String> list = new ArrayList<String>();
private void add(String phone)
{
list.add(phone);
}
#Override
public String toString()
{
StringBuffer b = new StringBuffer(32);
for (String ph : list)
{
b.append(ph + "\n"); // Or something
}
return b.toString();
}
}
}
There is a compliation error stating that class name is already define i can't find the way to resolve it
further the class name are declared only once and can't find the place where the things are going wrong
package practo;
import java.io.*;
import java.lang.*;
import java.util.*;
#SuppressWarnings("unused")
class Customer /* compilation error occurs here */
{
private int id;
private String name;
private String email;
private String address;
void setid(int id)
{
this.id=id;
}
int getid()
{
return id;
}
void setname(String name)
{
this.name=name;
}
String getname()
{
return name;
}
void setemail(String email)
{
this.email=email;
}
String getemail()
{
return email;
}
void setaddress(String address)
{
this.address=address;
}
String getaddress()
{
return address;
}
class PhoneNumber
{
private String phoneNumber;
private String heldFromDate;
private String heldToDate;
void setphoneNumber(String phoneNumber)
{
this.phoneNumber=phoneNumber;
}
String getphoneNumber()
{
return phoneNumber;
}
void setheldToDate(String heldToDate)
{
this.heldToDate=heldToDate;
}
String getheldToDate()
{
return heldToDate;
}
public String getHeldFromDate() {
return heldFromDate;
}
public void setHeldFromDate(String heldFromDate) {
this.heldFromDate = heldFromDate;
}
class NumberType
{
private String code;
private String description;
void setcode(String code)
{
this.code=code;
}
void setdescription(String description)
{
this.description=description;
}
String getcode()
{
return code;
}
String getdescription()
{
return description;
}
}
}
}
class x1
{
public void main(String args[])
{
#SuppressWarnings("resource")
Scanner s=new Scanner(System.in);
Customer c=new Customer();
Customer.PhoneNumber p=c.new PhoneNumber();
Customer.PhoneNumber.NumberType n=p.new NumberType();
System.out.println("Enter the customer details");
System.out.println("Enter the id :");
int id=s.nextInt();
c.setid(id);
System.out.println(c.getid());
System.out.println("Enter the name :");
String name=s.nextLine();
c.setname(name);
System.out.println(c.getname());
System.out.println("Enter the email :");
String email=s.nextLine();
c.setemail(email);
System.out.println(c.getemail());
System.out.println("Enter the address :");
String address=s.nextLine();
c.setaddress(address);
System.out.println(c.getaddress());
System.out.println("Enter the customer contact details");
System.out.println("Enter the phone number :");
String phoneNumber=s.nextLine();
p.setphoneNumber(phoneNumber);
System.out.println(p.getphoneNumber());
System.out.println("Enter the held from date (dd/MM/yyyy) :");
String heldFromDate=s.next();
p.setHeldFromDate(heldFromDate);
System.out.println(p.getHeldFromDate());
System.out.println("Enter the held to date (dd/MM/yyyy) :");
String heldToDate=s.next();
p.setheldToDate(heldToDate);
System.out.println(p.getheldToDate());
System.out.println("Enter number type code :");
String code=s.next();
n.setcode(code);
System.out.println(n.getcode());
System.out.println("Enter number type description");
String description=s.next();
n.setdescription(description);
System.out.println(n.getdescription());
}
}
Your class does not give me any compilation error. You might try making the class public i.e. public class Customer and file name having the name Customer.java. It may happen that the package practo already contains a class named Customer.
Can you please verify Customer class is not duplicate ? If it is not there, can you choose Clean from the Project menu, it might fix these errors.
Sometime eclipse trouble us.
Lots of recommendations for improvement:
Open public class per file, and a file for each class. Your arrangement is confusing.
Learn and follow the Java coding standards.
Using a Date for a date instead of a String is a better design, especially with JDK 8 and the java.time package.
Learn JUnit instead of that x1.main.
These are examples of how your classes should look.
Customer.java
package practo;
/**
* Created by Michael
* Creation date 5/29/2016.
* #link https://stackoverflow.com/questions/37511168/it-show-the-error-that-customer-is-already-defined-please-let-me-know-whats-wron
*/
public class Customer {
private int id;
private String name;
private String email;
private String address;
public Customer() {
this(0, "", "", "");
}
public Customer(int id, String name, String email, String address) {
this.id = id;
this.name = name;
this.email = email;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
PhoneNumber.java:
package practo;
/**
* Created by Michael
* Creation date 5/29/2016.
* #link https://stackoverflow.com/questions/37511168/it-show-the-error-that-customer-is-already-defined-please-let-me-know-whats-wron
*/
public class PhoneNumber {
private String phoneNumber;
private String heldFromDate; // Bad design. This ought to be a Date, not a String
private String heldToDate; // Bad design. This ought to be a Date, not a String
public PhoneNumber() {
this("", "", "");
}
public PhoneNumber(String phoneNumber, String heldFromDate, String heldToDate) {
this.phoneNumber = phoneNumber;
this.heldFromDate = heldFromDate;
this.heldToDate = heldToDate;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getHeldFromDate() {
return heldFromDate;
}
public void setHeldFromDate(String heldFromDate) {
this.heldFromDate = heldFromDate;
}
public String getHeldToDate() {
return heldToDate;
}
public void setHeldToDate(String heldToDate) {
this.heldToDate = heldToDate;
}
}
Check if you have another class called Customer in the package practo. That would cause a name conflict.
I wrote a REST web service which is returning JSON as below
[{"id":0,"name":"Vishal","age":"23","dob":"21/1/1992","phone":"9966558","sslc":"90","hsc":"90","college":"90"},
{"id":0,"name":"Karthik","age":"27","dob":"14/8/1988","phone":"995674","sslc":"99","hsc":"100","college":"100"},
{"id":0,"name":"Jeeva","age":"29","dob":"10/1/1987","phone":"77422","sslc":"99","hsc":"99","college":"100"},
{"id":0,"name":"Arya","age":"26","dob":"10/1/1989","phone":"55668","sslc":"100","hsc":"99","college":"99"}]
But I want the output with the "student" appended as below.
{"student":[{"id":0,"name":"Vishal","age":"23","dob":"21/1/1992","phone":"9966558","sslc":"90","hsc":"90","college":"90"},
{"id":0,"name":"Karthik","age":"27","dob":"14/8/1988","phone":"995674","sslc":"99","hsc":"100","college":"100"},
{"id":0,"name":"Jeeva","age":"29","dob":"10/1/1987","phone":"77422","sslc":"99","hsc":"99","college":"100"},
{"id":0,"name":"Arya","age":"26","dob":"10/1/1989","phone":"55668","sslc":"100","hsc":"99","college":"99"}]}
how can I achieve this output?
Below is the Product Class
#XmlRootElement(name="student")
public class Student implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public Student() {
super();
}
public Student(int id, String name, String age, String dob, String phone,
String sslc, String hsc, String college) {
super();
this.id = id;
this.name = name;
this.age = age;
this.dob = dob;
this.phone = phone;
this.sslc = sslc;
this.hsc = hsc;
this.college = college;
}
private int id;
private String name;
private String age;
private String dob;
private String phone;
private String sslc;
private String hsc;
private String college;
#XmlElement
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
#XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#XmlElement
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
#XmlElement
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
#XmlElement
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
#XmlElement
public String getSslc() {
return sslc;
}
public void setSslc(String sslc) {
this.sslc = sslc;
}
#XmlElement
public String getHsc() {
return hsc;
}
public void setHsc(String hsc) {
this.hsc = hsc;
}
#XmlElement
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
#Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age
+ ", dob=" + dob + ", phone=" + phone + ", sslc=" + sslc
+ ", hsc=" + hsc + ", college=" + college + "]";
}
}
Below is the service class.
#GET
#Path("/student.srv")
#Produces("application/json")
public Response getStudentJson(){
DAOLayer daoLayer=new DAOLayer();
List<Student> studentsList=null;
try {
studentsList=daoLayer.getStudents();
} catch (SQLException e) {
e.printStackTrace();
}
return Response.ok(studentsList).build();
}
Kindly help me to achieve the above mentioned output.
Thanks in Advance.
To get the desired output, you will have to create one single root object containing a List<Student> student and return it:
Root.java
#XmlRootElement(name="root")
public class Root implements Serializable {
#XmlList
private List<Student> student = new ArrayList<Student>();
// getter and setter
}
Service.java
#GET
#Path("/student.srv")
#Produces("application/json")
public Response getStudentJson(){
DAOLayer daoLayer=new DAOLayer();
List<Student> studentsList=null;
try {
studentsList=daoLayer.getStudents();
} catch (SQLException e) {
e.printStackTrace();
}
Root root = new Root();
root.setStudent(studentsList),
return Response.ok(root).build();
}
I am having errors below and I am confused why this is a Lab to make a storage spot for a students information.
import java.util.Arrays;
I have a problem with the square brackets below
public class Student {
private String studentID;
private String studentName;
private String studentMajor;
private double studentGPA;
private String studentGrad;
private long[] ;
And a problem with Student() below
public Student(){
studentID = "";
studentName = "";
studentMajor = "";
studentGPA = 0;
studentGrad = "";
}
public Student(String stuID, String stuName, String stuMajor, double stuGPA, String stuGrad) {
studentID = stuID;
studentName = stuName;
studentMajor = stuMajor;
studentGPA = stuGPA;
studentGrad = stuGrad;
}
public void setID(String ID) {
studentID = ID;
}
public void setName(String name) {
studentName = name;
}
public void setMajor(String major) {
studentMajor = major;
}
public void setGPA(double GPA) {
studentGPA = GPA;
}
public void setGrad(String Grad) {
studentGrad = Grad;
}
public String getID() {
return studentID;
}
public String getName() {
return studentName;
}
public String getMajor() {
return studentMajor;
}
public double getGPA() {
return studentGPA;
}
public String getGrad() {
return studentGrad;
}
public void printData() {
System.out.println("Student ID: "+studentID);
System.out.println("Student Name: " +studentName );
System.out.println("Student Major: "+studentMajor);
System.out.println("Student GPA: "+ studentGPA);
System.out.println("Student Year of Graduation: " +studentGrad);
}
}
private long[]; is incorrect syntax.
To complete the variable you must insert an identifier (the variable name).
In most IDE's (i.e. Eclipse and Netbeans), this incomplete variable will cause the next line to have issues. This is why public Student(){ (despite being completely valid code) is giving you an error. If you temporarily delete the line private long[]; you should see this error go away.
From the code you supplied, it looks like private long[]; can be deleted anyway as an array isn't being used.