Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing Value from Scanner Class to Class Constructor

 
Nigel Foley
Greenhorn
Posts: 6
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am new to this forum/java programming so I apologise in advance if I explain this incorrectly

I have a main method that takes values from the scanner and passes them to the customer object


}

When I run this code the customer object is instantiated but it is set to default field values and not to the values that are picked up by the scanner.
I have found a similar thread http://www.coderanch.com/t/408970/java/java/passing-arguments-constructor
but don't quite understand what is going on.

I would really appreciate it, if somebody could spare time to have a look and help me out

Thanks
Nigel

I have attached the associated Customer code below in case it is needed.








 
Henry Wong
author
Marshal
Pie
Posts: 21399
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

First, it will help if you added some code tags... like so...

Nigel Foley wrote:




 
Henry Wong
author
Marshal
Pie
Posts: 21399
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nigel Foley wrote:Hi All,

I am new to this forum/java programming so I apologise in advance if I explain this incorrectly

I have a main method that takes values from the scanner and passes them to the customer object

...

When I run this code the customer object is instantiated but it is set to default field values and not to the values that are picked up by the scanner.
I have found a similar thread http://www.coderanch.com/t/408970/java/java/passing-arguments-constructor
but don't quite understand what is going on.

I would really appreciate it, if somebody could spare time to have a look and help me out

Thanks
Nigel


Hint: take a look at the constructor of your user class...



What is it that you are attempting to do?? (hint: it is not what you think it is)


And BTW, welcome to the ranch.

Henry
 
Nigel Foley
Greenhorn
Posts: 6
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,

Thanks for pointing me in the right direction, I changed up the constructor to use the 'this' keyword(now I reference the current object) and everything work fine.

Nigel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic