• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

printing data from objects

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic