Reputation: 211
I am having a small problem I am trying to print the contents of a couple of variables which are located in a a private method. but I simply keep getting 'Can Not Find Symbol'
Below is the code that I am trying to read the data from (including the println) also I am very new to java.
private void createBooking()
{
String title;
String firstName;
String lastName;
String bookingNo;
String roomType;
System.out.print("Enter title: ");
title = keyboard.next();
System.out.print("Enter first name: ");
firstName = keyboard.next();
System.out.print("Enter last name: ");
lastName = keyboard.next();
System.out.print("Enter booking number: ");
bookingNo = keyboard.next();
System.out.print("Enter room type: ");
roomType = keyboard.next();
aBooking = new Booking (title, firstName, lastName, bookingNo, roomType);
}
public void printCustomerName()
{
System.out.println (createBooking.title);
}
Upvotes: 0
Views: 519
Reputation: 12538
I don't understand totally what you wanna do. But i think you want something like this:
public String printCustomerName() {
// This creates the booking object (aBooking)
createBooking();
// You can access the firstname lastname like that (I assume that you have a getter method implemented..)
return aBooking.getFirstName() + " " + aBooking.getLastName();
}
But the createBooking() you should move to another place. Maybe into the Constructor and call it there..
Upvotes: 0
Reputation: 1123
You may want to consider changing the return type of the createBooking()
method from 'void'
to 'Booking'
, and then the last line would become:
private Booking createBooking() { ... ... return new Booking(title, firstName, lastName, bookingNo, roomType)`` }
After that, your printCustomerName()
might look like something like:
public void printCustomerName() { Booking booking = createBooking(); System.out.println (booking.title); // if title is visible, which it probably shouldn't be //or System.out.println (booking.getTitle()); // if such a method exists... }
Upvotes: 0
Reputation: 421290
You probably want to put these variables as member variables, and then simply access it without using the .
-operator.
class BookingClass {
// You also seem to need the following:
Booking aBooking;
String title;
String firstName;
String lastName;
String bookingNo;
String roomType;
private void createBooking() {
System.out.print("Enter title: ");
title = keyboard.next();
System.out.print("Enter first name: ");
firstName = keyboard.next();
System.out.print("Enter last name: ");
lastName = keyboard.next();
System.out.print("Enter booking number: ");
bookingNo = keyboard.next();
System.out.print("Enter room type: ");
roomType = keyboard.next();
aBooking = new Booking(title, firstName, lastName, bookingNo, roomType);
}
public void printCustomerName() {
System.out.println(title);
// ...should perhaps be
// System.out.println(firstName + " " + lastName);
}
}
Since you do create a Booking
instance however, you may want to get rid of title
, firstName
, lastName
, bookingNo
and roomType
and put them in the Booking
class instead. And then access them through aBooking.getTitle()
and so on...
Upvotes: 4
Reputation: 234654
When you do aBooking = new Booking(...)
you're creating a new Booking
object with all those attributes and storing it in the aBooking
field (I'm guessing it's a field since it's not declared anywhere). This means you have a aBooking field that holds all those attributes (assuming the Booking
constructor saves the parameters). So, to access those fields you go through the aBooking
field. Probably something like this:
System.out.println(aBooking.getTitle());
or, if you're not using getters (you should!):
System.out.println(aBooking.title);
The variables you declare inside the createBooking
method stop "existing" once you leave the method. They're not accessible in any way (well, almost).
Upvotes: 3
Reputation: 501
You cannot access to variable of a method and cannot use a method as a class instance using a dot operator.
createBooking.something is illegal , you can use that method: createBooking()
Upvotes: 0