aspose file tools*
The moose likes Beginning Java and the fly likes Need help with set and get using a scanner input with three different classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help with set and get using a scanner input with three different classes" Watch "Need help with set and get using a scanner input with three different classes" New topic
Author

Need help with set and get using a scanner input with three different classes

Brad Edwards
Greenhorn

Joined: Mar 19, 2012
Posts: 18
Trying to use a scanner in my main class to set variables in another class with a private variable....





Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

There are two main issues I see. First, you are going to have to flesh these methods out so that they do what you expect:


The second problem comes with your main method:



What is seFirstName? A method correct? How do you call methods with parameter?


Steve
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Welcome to the Ranch, Brad!

You need to TellTheDetails(⇐click) about exactly what problem you're having.

Also, as a matter of readability, unless your instructor requires them, I would suggest getting rid of all those comments that do nothing more that restate the variable's or method's name more verbosely. Well-named variables and methods are largely self-documenting, so simply repeating the obvious tends to just add clutter.
Brad Edwards
Greenhorn

Joined: Mar 19, 2012
Posts: 18
These are the instructions I was given by my instructor....

Lab4.java = source code for the driver class
Name.java = source code for the Name class
Item.java = source code for the Item class

1. Create a class named Name that contains the following:
A private string to represent first name
A private string to represent last name
A public constructor that accepts two values and assigns to the properties
Public Methods named get... to return the value of the property
Public methods named set... to assign values to the properties by using a single argument passed to each method

2. Create a class named Item that contains the following:
A private integer to represent item number
A private float to represent the cost
A private String to represent the manufacturer's name
A public constructor that accepts three values and assigns them to the properties
Public methods named get... to return the value of the property
Public methods named set... to assign values to the properties by using a single argument passed to each method. For the item number and cost set... methods, if the value is less than zero, return false, otherwise, set the property to the argument and return true.

3.Allocate an instance of the Item and Name class in a simple driver application class named Lab4, and verify they are working correctly.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Brad Edwards wrote:These are the instructions I was given by my instructor....

I still don't see a question. Exactly what problem(s) are you having?

While you're answering, a little tip for you: it's not absolutely necessary, but it may help. If you don't follow it, don't worry too much; what you have is a good start.

Classes should usually contain all the methods associated with their behaviour, and to me that includes accepting user input to create new instances.
One way to do that is with a static "factory" method (you might want to look it up); for example:and then your Log4 code simply becomes:
Name names = Name.from(input);
(Note the class name before the method call; you should almost always call static methods this way).

Do you see the advantage? Now your main() code is not concerned with how to construct a Name, because it's all contained in the Name class itself.

The technique is probably a little ahead of where you are right now, so you should check with your teacher if it's OK to use it (and also make sure you understand it, because s/he may ask you probing questions ).

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Brad Edwards wrote:These are the instructions I was given by my instructor....


Okay, so now we know what you're trying to accomplish.

We still know nothing about what specific problems you're having.
Brad Edwards
Greenhorn

Joined: Mar 19, 2012
Posts: 18
The problem is how do I get user input from the driver class (Lab4.java) to be set in the separate class file... ie. First Name of Name class etc...
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

I mentioned the answers in my first post. The instructor tells you the answers in his instructions too:

Public methods named set... to assign values to the properties by using a single argument passed to each method


Then you have to fix how you call the methods.
Brad Edwards
Greenhorn

Joined: Mar 19, 2012
Posts: 18
Bottom line is... I have no clue what to put in it to make it right....
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Noone her is going to do this for you, so you should at least try and ShowSomeEffort.

The instructor says:
Public methods named set... to assign values to the properties by using a single argument passed to each method


How do you assign a value to a variable in Java?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Brad Edwards wrote:Bottom line is... I have no clue what to put in it to make it right....

So, ask a question. Like Steve, all I hear so far is "I can't do this; do it for me".

Start with one problem; ask the question; and get that fixed. It's a good way to work in general when you have programming problems.

Winston
Brad Edwards
Greenhorn

Joined: Mar 19, 2012
Posts: 18
ok, I have been working on this problem for a month now to get it right. I have watched tons of youtube tutorials as well as countless google searches. I have tried many different ways to get the program to work, all of which has been a complete failure. Ive gotten the program to compile correctly however when I tested it to make sure that the variables were getting Set into the correct class file, I was getting a null value. Here is what I have so far



it is giving me these errors...

G:\School\ITEC2710 Java Application Programming\Labs\Lab4\Lab4.java:16: error: cannot find symbol
names.setFirstName(customerFirstName) = input.next();
^
symbol: variable customerFirstName
location: class Lab4
G:\School\ITEC2710 Java Application Programming\Labs\Lab4\Lab4.java:20: error: cannot find symbol
names.setLastName(customerLastName) = input.next();
^
symbol: variable customerLastName
location: class Lab4
G:\School\ITEC2710 Java Application Programming\Labs\Lab4\Lab4.java:27: error: cannot find symbol
setItemNum(itemNumber) = input.nextInt();
^
symbol: variable itemNumber
location: class Lab4
G:\School\ITEC2710 Java Application Programming\Labs\Lab4\Lab4.java:31: error: cannot find symbol
setCost(itemCost) = input.nextFloat();
^
symbol: variable itemCost
location: class Lab4
G:\School\ITEC2710 Java Application Programming\Labs\Lab4\Lab4.java:35: error: cannot find symbol
setMfgName(manufacturerName) = input.next();
^
symbol: variable manufacturerName
location: class Lab4
5 errors

Tool completed with exit code 1




Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Brad Edwards wrote:ok, I have been working on this problem for a month now to get it right. I have watched tons of youtube tutorials as well as countless google searches. I have tried many different ways to get the program to work, all of which has been a complete failure. Ive gotten the program to compile correctly however when I tested it to make sure that the variables were getting Set into the correct class file, I was getting a null value. Here is what I have so far



it is giving me these errors...

G:\School\ITEC2710 Java Application Programming\Labs\Lab4\Lab4.java:16: error: cannot find symbol
names.setFirstName(customerFirstName) = input.next();
^
symbol: variable customerFirstName
location: class Lab4
G:\School\ITEC2710 Java Application Programming\Labs\Lab4\Lab4.java:20: error: cannot find symbol
names.setLastName(customerLastName) = input.next();
^
symbol: variable customerLastName
location: class Lab4
G:\School\ITEC2710 Java Application Programming\Labs\Lab4\Lab4.java:27: error: cannot find symbol
setItemNum(itemNumber) = input.nextInt();
^
symbol: variable itemNumber
location: class Lab4
G:\School\ITEC2710 Java Application Programming\Labs\Lab4\Lab4.java:31: error: cannot find symbol
setCost(itemCost) = input.nextFloat();
^
symbol: variable itemCost
location: class Lab4
G:\School\ITEC2710 Java Application Programming\Labs\Lab4\Lab4.java:35: error: cannot find symbol
setMfgName(manufacturerName) = input.next();
^
symbol: variable manufacturerName
location: class Lab4
5 errors
Tool completed with exit code 1






You seem to have a contradiction here -- you say that you have gotten the code to compile, but all the errors that is being reported are compiler errors.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Brad Edwards
Greenhorn

Joined: Mar 19, 2012
Posts: 18
yes, I have changed the program since then to try and correct the problem I was having with the variables not being Set into the Name and Item Class files...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


As for the compiler error, basically the compiler is complaining that the expression is not valid.



There are many reasons for this, but the reason that the compiler is complaining about is that "customerFirstName" is not a variable that is in scope.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Brad Edwards wrote:yes, I have changed the program since then to try and correct the problem I was having with the variables not being Set into the Name and Item Class files...



So... you are saying that you got the program to compile, and you want us to help you with your current problem by looking at you older program that doesn't compile?

Henry
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Brad Edwards wrote:ok, I have been working on this problem for a month now to get it right...

Irrelevant (but possibly frustrating).

I have watched tons of youtube tutorials

Ugh.

as well as countless google searches.

Ah. Did you happen to find the Oracle/Sun tutorials when you did that? They're usually near the top of the tree if you Google for "Java tutorial {whatever}".

I have tried many different ways to get the program to work, all of which has been a complete failure. Ive gotten the program to compile correctly however when I tested it to make sure that the variables were getting Set into the correct class file, I was getting a null value. Here is what I have so far
...
it is giving me these errors...

Right. Other than Henry's point (which is absolutely right) let's deal with things one at a time:

First off: 'cannot find symbol' usually (not always) means that the compiler cannot find a name that you thought was a variable.

Lesson 1: Java is case-sensitive. Is it possible that you've misspelled something? In this case not (but always check).

So where is customerFirstName? Is it in Lab4?
(Come back when you have the answer)

Tip: once you understand the first few errors, the rest will be easy.

Winston
Brad Edwards
Greenhorn

Joined: Mar 19, 2012
Posts: 18
customerFirstName is in the Name class...
Brad Edwards
Greenhorn

Joined: Mar 19, 2012
Posts: 18
Henry the old program is the one that complied however it did not Set the variables correctly to the different classes. I have changed the program since then and these are the issues I am having with it now.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Brad Edwards wrote:customerFirstName is in the Name class...

So how do you think that Log4 is going to know anything about it?

Winston
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


Brad Edwards wrote:Henry the old program is the one that complied however it did not Set the variables correctly to the different classes. I have changed the program since then and these are the issues I am having with it now.


Oh.... then my previous post applies.



Henry Wong wrote:
As for the compiler error, basically the compiler is complaining that the expression is not valid.



There are many reasons for this, but the reason that the compiler is complaining about is that "customerFirstName" is not a variable that is in scope.


Brad Edwards
Greenhorn

Joined: Mar 19, 2012
Posts: 18
I thought by putting the names.setFirstName would show that the program needs to look for the argument in the Name class... so by putting names.setFirstName(names.customerFirstName) would solve the problem?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Brad Edwards wrote:I thought by putting the names.setFirstName would show that the program needs to look for the argument in the Name class... so by putting names.setFirstName(names.customerFirstName) would solve the problem?

It might (if the variable isn't private), but basically, it's bad practise.

Think about it: what are you trying to do? You're trying to access the customerFirstName of an object that doesn't yet exist.

My suggestion (this will be becoming boring for those who have seen it before, but it's the same as I give to all newbies):
1. STOP CODING.
2. Turn your computer OFF.
3. Get out a pencil and paper and write down in English (or your native language) the steps you need to do to solve your problem.
4. When (and only when) you know and understand every step...
Turn your computer back on and either
(a) Start from scratch; or
(b) Re-factor your existing program to do what it is supposed to do.

Until you understand the problem, programming will be a very hard road.

Winston
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Winston Gutkowski wrote:
Brad Edwards wrote:I thought by putting the names.setFirstName would show that the program needs to look for the argument in the Name class... so by putting names.setFirstName(names.customerFirstName) would solve the problem?

It might (if the variable isn't private), but basically, it's bad practise.

Think about it: what are you trying to do? You're trying to access the customerFirstName of an object that doesn't yet exist.

My suggestion (this will be becoming boring for those who have seen it before, but it's the same as I give to all newbies):
1. STOP CODING.
2. Turn your computer OFF.
3. Get out a pencil and paper and write down in English (or your native language) the steps you need to do to solve your problem.
4. When (and only when) you know and understand every step...
Turn your computer back on and either
(a) Start from scratch; or
(b) Re-factor your existing program to do what it is supposed to do.

Until you understand the problem, programming will be a very hard road.

Winston



I think the OP needs to go back and review method calling a bit -- the parameter that the OP is trying to pass is actually the name of the variable that is used by the parameters method. This is a local variable and out of scope outside of the method call. Furthermore, even if it was in scope, it doesn't make sense to use the local variable to set the local variable. You need to pass the value that will be assigned to the variable -- not the variable.

Henry
Brad Edwards
Greenhorn

Joined: Mar 19, 2012
Posts: 18
Thank you all for your help... I have finally figured out the what my problem was... All I needed was a gentle shove (Thanks Winston).
It works great and I'm now off suicide watch...

Brad
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Brad Edwards wrote:It works great and I'm now off suicide watch...

Glad to hear. Canceled me 911 speed-dial...

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with set and get using a scanner input with three different classes