File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

printing data from objects

 
vladimir mujakovic
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3222
12
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you print out the "s" once it's read in the Validator.getStringInput() method?

And what does Netbeans say about the red lines?
 
vladimir mujakovic
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3222
12
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3222
12
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic