I am a first year student and learning setter and getter at school.
When I run it, it is ignoring the following statement:
String Origin = scan.nextLine();
and then it goes to the next line.
Here is the main:
import java.util.Scanner;
public class FlightTest
{
public static void main (String [] args)
{
Scanner scan = new Scanner (System.in);
Flight Flight1 = new Flight ();
System.out.print ("Airline Name: ");
String AirlineName = scan.nextLine ();
System.out.print ("Flight Number: ");
int FlightNumber = scan.nextInt ();
System.out.print ("Origin: ");
String Origin = scan.nextLine(); // There is a problem here
System.out.print ("Destination: ");
String Destination = scan.nextLine ();
}
}
This is the class
public class Flight
{
private String AirlineName;
private int FlightNumber;
private String Origin;
private String Destination;
public String setAirlineName()
{
String Name = AirlineName;
return Name;
}
public String getAirlineName()
{
return AirlineName;
}
public int setFlightNumber ()
{
int Number = FlightNumber;
return Number;
}
public int getFlightNumber ()
{
return FlightNumber;
}
public String setOrigin ()
{
String Orig = Origin;
return Orig;
}
public String getOrigin ()
{
return Origin;
}
public String setDestination ()
{
String Desti = Destination;
return Desti;
}
public String getDestination ()
{
return Destination;
}
public String toString ()
{
String result = AirlineName + " flight number "
+ FlightNumber + " leaves from " + Origin + " to "
+ Destination + ".";
return result;
}
}
because scan.nextInt() doesn't use the whole line, the next call to scan.nextLine() is returning the end of the line that the flight number was on, which is probably just a newline.
Add another call to scan.nextLine()
System.out.print ("Flight Number: ");
int FlightNumber = scan.nextInt ();
scan.nextLine(); // get rid of rest of line
System.out.print ("Origin: ");
String Origin = scan.nextLine();
and I think things will work.
and your setter is wrong, you should pass a parameter.
private String airlineName;
public String getAirlineName() {
return airlineName;
}
public void setAirlineName(String airlineName) {
this.airlineName = airlineName;
}
you can use setter like this
Flight f = new Flight ();
System.out.print ("Airline Name: ");
f.setAirlineName(scan.nextLine ());
Related
just entered this community and this is my first question here, so please bear with a noob. I created two classes, first is Student, a basic one with fields, constructor and getters. The second one has the main method, a LinkedList, a multiple-entry Scanner (for...) and two simple methods.
My problem is that despite the fact that the For loop has maximum index 1 (x = 0; x < 2), the Scanner expects a third row input and an enter but does not print the third line. I add the two classes, maybe I made a mistake and I would appreciate your help. Thank you in advance.
public class Student {
private String name;
private String surname;
private int firstMark;
private int secondMark;
private int finalExamMark;
public Student(String name, String surname, int firstMark, int secondMark, int finalExamMark) {
this.name = name;
this.surname = surname;
this.firstMark = firstMark;
this.secondMark = secondMark;
this.finalExamMark = finalExamMark;
}
public String getName() {
return name;
}
public String getSurname() {
return surname;
}
public int getFirstMark() {
return firstMark;
}
public int getSecondMark() {
return secondMark;
}
public int getFinalExamMark() {
return finalExamMark;
}
#Override
public String toString (){
return this.name + " " + this.surname + " got " + this.firstMark + " at English, " + this.secondMark + " at Math and " + this.finalExamMark + " at the final exam.";
}
}
public class StudentMain {
static LinkedList<Student> courseAttend = new LinkedList<>();
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
addStudent();
printList(courseAttend);
}
private static void addStudent() {
for (int x = 0; x < 2; x++) {
String s1 = scanner.nextLine();
String[] split = s1.split("\\s");
courseAttend.add(new Student(split[0], split[1], Integer.parseInt(split[2]), Integer.parseInt(split[3]), Integer.parseInt(split[4])));
scanner.nextLine();
}
scanner.close();
}
private static void printList(LinkedList<Student> lista) {
for (Student elem : lista) {
System.out.println(elem);
}
}
}
For example, if I input (without quotes)
"Young John 9 9 9"
"Johnson Anne 8 8 8" and I press enter, the cursor moves to next line and waits another input. Only after that third line and the final enter, the message is displayed but the third line is not shown.
courseAttend should be a List<Student> courseAttend = new ...List<>(); like your methode argument printList(List<Student> students) because both could be also a ArrayList and it is the normal way to in implement variables if they have not to be sepciefied, like in your case.
The loop has two nextLine() but you ignore the second one so you are creating a student and waiting of the next input and not saving the input
When a User has to enter multiple arguments for any given prompt, you can expect to have typos. Your prompt expects the User to enter 5 arguments and they are not even from the same typ on a single line delimited with a whitespace. What if the user accidentally give you an ivalid input like a integer as name or a String as mark.
public class StudentMain {
static List<Student> courseAttend = new LinkedList<>();
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
for (int x = 0; x < 2; x++) {
courseAttend.add(createStudent());
}
scanner.close();
printList(courseAttend);
}
private static Student createStudent(){
System.out.print("name: ");
String name = scanner.nextLine();
System.out.print("surname: ");
String surname = scanner.nextLine();
System.out.print("first mark: ");
int firstMark = scanner.nextInt();
System.out.print("second mark: ");
int secondMark = scanner.nextInt();
System.out.print("final exam mark: ");
int finalExamMark = scanner.nextInt();
return new Student(name, surname, firstMark, secondMark ,finalExamMark);
}
private static void printList(List<Student> students) {
for (Student elem : students) {
System.out.println(elem);
}
}
}
I would like to use a test file to read several objects, but I got a null from step2 to step 4.
For name, if i input Steven, I would like to get ste123 back (123 should be a random number)
Here is the code:
public class Lab14Tester
{
public static void main(String[] args)
{
Introduction step1 = new Introduction();
System.out.println(step1.giveline1());
System.out.println(step1.giveline2());
System.out.println(step1.giveline3());
System.out.println(step1.giveline2());
System.out.println(step1.giveline1());
Scanner in= new Scanner(System.in);
System.out.print("\nName: ");
String inputname = in.nextLine();
Username step2 = new Username();
String givename = step2.givename();
System.out.println("Your username is " + givename);
System.out.print("\nGive me a number: ");
int inputnumber =in.nextInt();
OddEven step3 = new OddEven();
System.out.println(step3.givenumbertype());
System.out.print("\nGive me a number grade: ");
double grade = in.nextDouble();
NumberToLetter step4 = new NumberToLetter();
System.out.printf("\n%.1f", grade);
System.out.print(" is equal to a " + step4.giveletter());
}
}
public class Username
{
private String subname;
private int randomnumber;
private String outputname;
public void getname(String inputname)
{
subname = inputname.substring(0,3);
randomnumber = (int)(Math.random()*1000);
outputname = subname + randomnumber;
}
public String givename()
{
return outputname;
}
}
You could be having trouble because you're concatenating a String with an Integer.
Try this:
public void getname(String inputname)
{
subname = inputname.substring(0,3);
randomnumber = (int)(Math.random()*1000);
String randomnumberAsString = String.valueOf(randomnumber);
outputname = subname + randomnumberAsString;
}
Hello everyone I am an amateur in Java and had some specific questions about a program using ArrayLists. The program is made up of several classes, and its purpose is to add, change, remove, and display friends from a Phone Book. I have the add and display methods done, but I'm having trouble with the remove and change method. I saw a similar case on this site, but it did not help me solve my problems. Any help at all would be much appreciated. This is what I have so far:
package bestfriends;
import java.util.Scanner;
import java.util.ArrayList;
public class BFFHelper
{
ArrayList<BestFriends> myBFFs;
Scanner keyboard = new Scanner(System.in);
public BFFHelper()
{
myBFFs = new ArrayList<BestFriends>();
}
public void addABFF()
{
System.out.println("Enter a first name: ");
String firstName = keyboard.next();
System.out.println("Enter a last name: ");
String lastName = keyboard.next();
System.out.println("Enter a nick name: ");
String nickName = keyboard.next();
System.out.println("Enter a phone number: ");
String cellPhone = keyboard.next();
BestFriends aBFF = new BestFriends(firstName, lastName, nickName, cellPhone);
myBFFs.add(aBFF);
}
public void changeABFF()
{
System.out.println("I am in changeBFF");
}
public void displayABFF()
{
System.out.println("My Best Friends Phonebook is: ");
System.out.println(myBFFs);
}
public void removeABFF()
{
System.out.print("Enter a friend's name to be removed: ");
int i = 0;
boolean found = false;
while (i < myBFFs.size() && !found)
{
if(firstName.equalsIgnoreCase(myBFFs.get(i).getFirstName()) && lastName.equalsIgnoreCase(myBFFs.get(i).getLastName()))
{
found = true;
}
else
i++;
}
}
}
That was my Helper Class, for which I'm having trouble with the removeABFF method, and still need to create a changeABFF method from scratch. Next is my main class:
package bestfriends;
import java.util.Scanner;
public class BFFPhoneBook
{
public static void main(String args[])
{
int menuOption = 0;
Scanner keyboard = new Scanner(System.in);
BFFHelper myHelper = new BFFHelper();
do
{
System.out.println("1. Add a Friend");
System.out.println("2. Change a Friend");
System.out.println("3. Remove a Friend");
System.out.println("4. Display a Friend");
System.out.println("5. Exit");
System.out.print("Enter your selection: ");
menuOption = keyboard.nextInt();
switch (menuOption)
{
case 1:
myHelper.addABFF();
break;
case 2:
myHelper.changeABFF();
break;
case 3:
myHelper.removeABFF();
break;
case 4:
myHelper.displayABFF();
break;
case 5:
break;
default:
System.out.println("Invalid option. Enter 1 - 5");
}
} while (menuOption != 5);
}
}
This is my last class:
package bestfriends;
public class BestFriends {
private static int friendNumber = 0;
private int friendIdNumber;
String firstName;
private String lastName;
private String nickName;
private String cellPhoneNumber;
public BestFriends (String aFirstName, String aLastName, String aNickName, String aCellPhone)
{
firstName = aFirstName;
lastName = aLastName;
nickName = aNickName;
cellPhoneNumber = aCellPhone;
friendIdNumber = ++friendNumber;
// friendIdNumber = friendNumber++;
}
public boolean equals(Object aFriend)
{
if (aFriend instanceof BestFriends )
{
BestFriends myFriend = (BestFriends) aFriend;
if (lastName.equals(myFriend.lastName) && firstName.equals(myFriend.firstName))
return true;
else
return false;
}
else
return false;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public String getNickName()
{
return nickName;
}
public String getCellPhone()
{
return cellPhoneNumber;
}
public int getFriendId()
{
return friendIdNumber;
}
public String toString()
{
return friendIdNumber + ". " + firstName + " (" + nickName + ") " + lastName + "\n" + cellPhoneNumber + "\n";
}
}
To explore and manipulate a arraylist an iterator is used
the object lacks the Setters
declare variables
ArrayList<BestFriends> myBFFs;
Scanner keyboard = new Scanner(System.in);
BestFriends best;
public BFFHelper()
{
myBFFs = new ArrayList<BestFriends>();
best= new BestFriends();
}
Delete
public void removeABFF()
{
System.out.print("Enter a friend's name to be removed: ");
String name= keyboard.next().toLowerCase();// entry name to be removed
Iterator<BestFriends> nameIter = myBFFs.iterator(); //manipulate ArrayList
while (nameIter.hasNext()){
best = nameIter.next(); // obtained object list
if (best.getNickName().trim().toLowerCase().equals(name)){ // if equals name
nameIter.remove(best); // remove to arraylist
}
}
}
Update
public void changeABFF()
{
System.out.print("Enter a friend's name to be change: ");
String name= keyboard.next().toLowerCase().trim();//entry name to be update
Iterator<BestFriends> nameIter = myBFFs.iterator();
while (nameIter.hasNext()){
best = nameIter.next();
if (best.getNickName().trim().toLowerCase().equals(name)){// if equals name
best.setNickName("NEW DATE");//update data with new data Setters
....
}
}
}
In your remove method you do not accept any input of the values
public void removeABFF()
{
System.out.print("Enter a friend's name to be removed: ");
int i = 0;
boolean found = false;
while (i < myBFFs.size() && !found)
....
As you are using firstNamer and lastName to find the object you needs these values
System.out.println("Enter a first name: ");
String firstName = keyboard.next();
System.out.println("Enter a last name: ");
String lastName = keyboard.next();
Whenever I run this:
public static void searchForGerbil()
{
System.out.println("Please type in a gerbil lab ID");
Scanner keyboard = new Scanner(System.in);
String searchgerbil = keyboard.next();
for (int i = 0; i <gerbil.length; i++){
if ( searchgerbil.equals(gerbil[i].getId())){
System.out.println(gerbil);
}
else{
System.out.println("Gerbil " + searchgerbil + " doesnt exist");
}
}
}
I end up with this output when i input 123 for String searchgerbil:
[Lgerbillab.Gerbil;#42886462
Gerbil 123 doesnt exist
here is the rest of my code for reference:
Class gerbillab
package gerbillab;
import java.util.Scanner;
import gerbillab.Gerbil;
public class gerbillab{
public static int population;
public static int[] maxfood;
public static int[] foodeats;
public static int types;
public static String[] idnumber;
public static String g;
public static String gerbilId;
public static Gerbil[] gerbil;
public static String amountoffoodeaten;
public static String gerbilsearch;
public static String thisgerbil;
public static void main(String args[]){
Scanner keyboard = new Scanner(System.in);
System.out.println("How many types of food do the gerbils eat?");
String f = keyboard.nextLine();
int totalF = Integer.parseInt(f);
String[] food = new String[totalF];
maxfood = new int[totalF];
for
(int a = 0; a<food.length; a++){
System.out.println("Name of food number " + (a+1));
String foodname = keyboard.nextLine();
food[a] = foodname;
System.out.println("Max amount of food " + (a+1));
String m = keyboard.nextLine();
int maximum = Integer.parseInt(m);
maxfood[a] = maximum;
}
System.out.println("How many gerbils are in the lab?");
String numberofGerbils = keyboard.nextLine();
population = Integer.parseInt(numberofGerbils);
idnumber = new String[population];
String[] nickname = new String[population];
boolean[] bite = new boolean[population];
boolean[] escape = new boolean[population];
gerbil = new Gerbil[population];
for
(int b = 0; b<idnumber.length; b++){
System.out.println("What is the id number of gerbil " + (b+1));
String idnumberx = keyboard.nextLine();
idnumber[b] = idnumberx;
System.out.println("What is the name for gerbil " + (b+1));
String nicknamex = keyboard.nextLine();
nickname[b] = nicknamex;
int[] foodeats = new int[totalF];
for
(int c = 0; c<foodeats.length; c++){
System.out.println("how much " + food[c] + " did this gerbil eat");
String amountoffoodeaten = keyboard.nextLine();
foodeats[c] = Integer.parseInt(amountoffoodeaten);
}
System.out.println("Does this Gerbil bite? Please enter True or False");
String doesitbite = keyboard.nextLine();
if (doesitbite.equalsIgnoreCase("true"))
bite[b] = true;
else{
bite[b] = false;
}
System.out.println("Does this Gerbil escape? Enter True or False");
String doesitescape = keyboard.nextLine();
if (doesitescape.equalsIgnoreCase("true"))
escape[b] = true;
else{
escape[b] = false;
}
gerbil[b] = new Gerbil(idnumberx, nicknamex, foodeats, escape[b], bite[b], maxfood);
}
while (true){
System.out.println("What would you like to know?");
String question = keyboard.nextLine();
String search = "search";
String average = "average";
String end = "end";
String restart = "restart";
if (question.equalsIgnoreCase(search)){
new gerbillab().searchForGerbil();
}
else
if (question.equalsIgnoreCase(average)){
for(int i = 0; i < idnumber.length; i++){
System.out.println(idnumber[i]);
}
for(int i = 0; i < nickname.length; i++){
System.out.println(nickname[i]);
}
for(int i = 0; i < bite.length; i++){
System.out.println(bite[i]);
}
for(int i = 0; i < escape.length; i++){
System.out.println(escape[i]);
}
}
else
if (question.equalsIgnoreCase(end)){
System.exit(0);
}
else
if (question.equalsIgnoreCase(restart)){
new gerbillab().main(args);
}
else
System.out.println("Try again");
}
}
public static void searchForGerbil()
{
System.out.println("Please type in a gerbil lab ID");
Scanner keyboard = new Scanner(System.in);
String searchgerbil = keyboard.next();
for (int i = 0; i <gerbil.length; i++){
if ( searchgerbil.equals(gerbil[i].getId())){
System.out.println(gerbil);
}
else{
System.out.println("Gerbil " + searchgerbil + " doesnt exist");
}
}
}
}
Class Gerbil
package gerbillab;
public class Gerbil {
private String idnumber;
private String nickname;
private int[] totalfood;
private String[] foodname;
private boolean escape;
private boolean bite;
private int[] foodeats;
public String gerbilsearch;
public Gerbil(String idnumberx, String gerbilName, int[] gerbilfoodeats, boolean gerbilEscape, boolean gerbilBite, int[] maxfood) {
idnumber = idnumberx;
nickname = gerbilName;
foodeats = gerbilfoodeats;
escape = gerbilEscape;
bite = gerbilBite;
totalfood = maxfood;
}
public Gerbil(String[] typefood) {
foodname = typefood;
}
public int[] getfoodeaten() {
return foodeats;
}
public Gerbil(int[] numOfFood) {
totalfood = numOfFood;
}
public int[] getAmountFood() {
return totalfood;
}
public boolean getBite() {
return bite;
}
public boolean getEscape() {
return escape;
}
public String getId() {
return idnumber;
}
public String getName() {
return nickname;
}
public void setId(String newId) {
idnumber = newId;
}
public void setName(String newName) {
nickname = newName;
}
public String[] gettypesofFood() {
return foodname;
}
}
You are trying to print the object without overriding toString you get the default value of classname suffixed with the object's hashcode. You can override your toString as mentioned below.
Another issue is your try to print the entire array instead of the indexed element it currently refers to: System.out.println(gerbil);. You would need to get the indexed element System.out.println(gerbil[i]); (I assume you would want this since you are iterating over the array)
Given that an element in your array exists with ID you provide, take cue from the following toString method (auto-generated through eclipse IDE):
Add this to your Gerbil.java
#Override
public String toString() {
return "Gerbil [idnumber=" + idnumber + ", nickname=" + nickname
+ ", totalfood=" + Arrays.toString(totalfood) + ", foodname="
+ Arrays.toString(foodname) + ", escape=" + escape + ", bite="
+ bite + ", foodeats=" + Arrays.toString(foodeats)
+ ", gerbilsearch=" + gerbilsearch + "]";
}
Change in searchForGerbil()
Replace:
System.out.println(gerbil);
With:
System.out.println(gerbil[i]);
You can try and modify the toString to display the content as you wish to.
In addition to the points PopoFibo made, there's this:
for (int i = 0; i <gerbil.length; i++){
if ( searchgerbil.equals(gerbil[i].getId())){
System.out.println(gerbil);
}
else{
System.out.println("Gerbil " + searchgerbil + " doesnt exist");
}
}
The above code probably does not do what you are hoping for. Say your gerbil array has 10 gerbils, all with different ID's. When you go through the loop, then each time you compare gerbil[i] to the ID, it will display "Gerbil ... doesnt exist" if the ID isn't equal. But this is inside the loop, so that means that if the ID equals one of the array elements, you will get one output line with a gerbil, and 9 output lines that say the gerbil doesn't exist.
You have to move the "doesn't exist" message outside the loop. One way to do that is to declare a new variable boolean found = false before the loop. In the loop, when you find it, set found = true. Then, test found after the loop is done. (You can also use break; once you've found the gerbil inside the loop, because that probably means you can stop searching.)
import java.util.*;
import java.io.*;
public class Patient
{
private String patientId;
private String patientName;
private String gender;
private int age;
private String patientWardNumber;
private int patientBedNumber;
private String dateRegistered;
private String treatment;
private int wardedDuration;
private double price;
private double payment;
public Patient()
{
String patientId =" ";
String patientName =" ";
String patientWardNumber =" ";
String dateRegistered =" ";
String treatment =" ";
int patienBedDuration = 0;
int wardedDuration = 0;
int age = 0;
String gender = " ";
double price = 0.00;
}
public Patient(String ID,String N,String G,int A,String WN,int BN,String DR,String T,int WD)
{
patientId = ID;
patientName = N;
gender = G;
age = A;
patientWardNumber = WN;
patientBedNumber = BN;
dateRegistered = DR;
treatment = T;
wardedDuration = WD;
}
public void SetPatient(String ID,String N,String G,int A,String WN,int BN,String DR,String T,int WD)
{
patientId = ID;
patientName = N;
gender = G;
age = A;
patientWardNumber = WN;
patientBedNumber = BN;
dateRegistered = DR;
treatment = T;
wardedDuration = WD;
}
public String getPatientId()
{
return patientId;
}
public String getPatientName()
{
return patientName;
}
public String getGender()
{
return gender;
}
public int getAge()
{
return age;
}
public String getPatientWardNumber()
{
return patientWardNumber;
}
public int getPatientBedNumber()
{
return patientBedNumber;
}
public String getDateRegistered()
{
return dateRegistered;
}
public String getTreatment()
{
return treatment;
}
public int getWardedDuration()
{
return wardedDuration;
}
public double getPrice()
{
return price;
}
public double getPayment()
{
return payment;
}
public String toString()
{
return patientId+" "+patientName+" "+gender+" "+age+" "+patientWardNumber+" "+patientBedNumber+" "+dateRegistered+" "+treatment+" "+wardedDuration+" "+price+" "+payment;
}
public static void main(String[]args)
{
ArrayList PatientList = new ArrayList();
Scanner scan = new Scanner(System.in);
System.out.println("Enter how many patient are : ");
int num = scan.nextInt();
for(int i=0; i<num; i++)
{
System.out.println("Enter patient ID: ");
String patientId = scan.next();
System.out.println("Enter patient Name: ");
String patientName = scan.next();
System.out.println("Enter the patient's gender: ");
String gender = scan.next();
System.out.println("Enter the patient's age: ");
int age = scan.nextInt();
System.out.println("Enter patient ward number: ");
String patientWardNumber = scan.next();
System.out.println("Enter patient's Bed Number: ");
int patientBedNumber = scan.nextInt();
System.out.println("Enter patient's registeration date: ");
String dateRegistered = scan.next();
System.out.println("Enter the treatment: ");
String treatment = scan.next();
System.out.println("Enter the ward duration: ");
int wardedDuration = scan.nextInt();
Patient data = new Patient(patientId,patientName,gender,age,patientWardNumber,patientBedNumber,dateRegistered,treatment,wardedDuration);
PatientList.add(data);
}
System.out.println("Total patients are: "+PatientList.size());
System.out.println("Enter patient name : ");
Patient D= null;
Object data ;
String user=scan.next();
boolean found=false;
while(!PatientList.isEmpty())
{
if(D.getPatientName().equalsIgnoreCase(user))
{
found=true;
System.out.println(D.toString());
}
else if (found==false)
{
System.out.println("Patient is not found ! TRY AGAIN");
}
}
}
}
I got 2 problems while running this coding.First i cannot enter value for my ward duration. .Second,when i want to search my patient,it cannot display the information about the patient.Its say null pointer exception.i'm still beginner at java.someone can show me the solution?
This is my output
Because you haven't initialized your Patient object D inside the main() method.
Patient D= null;
and invoking a method on it.
if(D.getPatientName().equalsIgnoreCase(user)){ // Here D is null
found=true;
System.out.println(D.toString());
}
You have to create an object of Patient and initialized variable D like,
Patient D= new Patient();
For your "ward duration" input problem; you are reading strings with Scanner.next(). Note that this only reads one word. So if you were to enter "Level 1" for, e.g., treatment, only "Level" would be read; the "1" would be left on the stream and could corrupt later input (in your case, the "1" is being read by the nextInt() call for ward duration).
I suggest using Scanner.nextLine() for those string inputs instead. That way it will read every word on the full line, and will allow you to enter data that contains spaces without issue.
As for your NullPointerException, Kugathasan Abimaran's answer covers that well.
Also, you have another smaller issue. In your Patient constructor, you are not actually initializing any of the member fields. You are declaring and initializing local variables instead:
public Patient() {
String patientId = " ";
String patientName = " ";
String patientWardNumber = " ";
String dateRegistered = " ";
String treatment = " ";
int patienBedDuration = 0;
int wardedDuration = 0;
int age = 0;
String gender = " ";
double price = 0.00;
}
Should be:
public Patient() {
patientId = " ";
patientName = " ";
patientWardNumber = " ";
dateRegistered = " ";
treatment = " ";
patientBedNumber = 0; // <- note: "patienBedDuration" was probably a typo
wardedDuration = 0;
age = 0;
gender = " ";
price = 0.00;
}
By the way, traditionally "" is used to represent an empty string, not " ". Also, it's redundant to initialize the numeric fields to 0 because that's their default initial value anyways.