File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes printing data from objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "printing data from objects" Watch "printing data from objects" New topic
Author

printing data from objects

vladimir mujakovic
Greenhorn

Joined: Feb 12, 2013
Posts: 18
hi i been working on this project all day and am hitting a wall, I am supposed to write a program that accepts data from the user and prints it to the console. it involves using inheritance. i have for the most part figured out how to write the classes now i am stuck with my main method. i dont know how to store the data in the class object this is my code so far please help im stuck



i attempted to write as follows



but i get redlines on netbeans, i dont understand. the object is to print the user data to the console.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2368
    
    7

Did you print out the "s" once it's read in the Validator.getStringInput() method?

And what does Netbeans say about the red lines?


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
vladimir mujakovic
Greenhorn

Joined: Feb 12, 2013
Posts: 18
im sorry i must not be clear on what im asking.

let me rephrase,

im asking about how to print data thats been entered by the user, more importantly im trying to pass the user entered values to an object of the person class. and then print that.



so im trying to pass the user entered value to an object.

if i store first name like so

String firstName = sc.next();

why cant i simply pass this value to an object by typing -->

Person c = new Customer(firstName);
keep in mind im using inheritance and overriding the toString method, not sure if that makes a difference
im trying to create Person c = new Customer(variable value);
so that c equals the value of the user input so i can then print it. but netbeans wont allow me to do it.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2368
    
    7

vladimir mujakovic wrote:String firstName = sc.next();

why cant i simply pass this value to an object by typing -->

Person c = new Customer(firstName);


Ah does your Customer have a constructor like "Customer(String)?

Even Customer is a subclass of Person, you need a constructor for each class if you are NOT using the default empty constructor.
vladimir mujakovic
Greenhorn

Joined: Feb 12, 2013
Posts: 18
how would that look?

my code is above it has all the classes i have so far. i am using constructors with no arguments at the moment. what should they be?
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2368
    
    7

There are 2 ways to achieve what you want to do. And your current approach (using empty constructor in those Person etc classes) does not match up with using parameterized constructors in your main method.





I hope you see the difference. And this is what Netbeans is telling you "there are no parameterized constructors"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: printing data from objects