I'm new need help immediately

I have three classes

connecting with extends word

in my main class the contructor does not work

I have no idea .

class 1:

public class Person

{

private String name;

private String address;

private String telephone;

public Person(String N, String A, String T)

{

name = N;

address = A;

telephone = T;

}

public void setName(String N)

{

name = N;

}

public void setAddress(String A)

{

address = A;

}

public void setTelephone(String P)

{

telephone = P;

}

public void set(String N, String A, String T)

{

name = N;

address = A;

telephone = T;

}

public String getName()

{

return name;

}

public String getAddress()

{

return address;

}

public String getPhone()

{

return telephone;

}

}

Class 2 :

public class Customer extends Person

{

private String cuNum ;

private boolean mailing;

public Customer(String N, String A, String T,

String CN, boolean M)

{

super(N, A, T);

cuNum = CN;

mailing = M;

}

public void setcuNum(String CN)

{

cuNum = CN;

}

public void setMailing(String M)

{

mailing =M.charAt(0) == 'y' || M.charAt(0) == 'Y';

}

public String getcuNum()

{

return cuNum;

}

public boolean getMailing()

{

return mailing;

}

}

Main class:

import java.util.Scanner;

public class CustomerInfo

{

public static void main(String[] arg)

{

String address;

String name;

String telephone;

String cuNum;

String mailing;

Scanner keyboard = new Scanner(System.in);

System.out.println(" **************************************");

System.out.println(" *Person and Custmoer Classes*");

System.out.println(" **************************************");

System.out.println();

Customer Cu = new Customer(name, address, telephone, cuNum, mailing);

}

}

// the is this Customer Cu = new Customer(name, address, telephone,

//cuNum, mailing);

[2324 byte] By [thecanma] at [2007-11-27 10:39:16]
# 1

no body can help me,

thecanma at 2007-7-28 18:59:56 > top of Java-index,Java Essentials,New To Java...
# 2

> no body can help me,

not with the way you are posting your questions, no. You first say you need help "urgently". If you've spent any time on these forums, you should know that the only thing "urgently" does is piss off many of the smarter programmers, greatly diminishing your chances of getting any help whatsoever. Your second problem is that you are expecting us to drop everything and help you, to put in tremendous and prompt effort, and yet you show no effort to help us. You don't use code tags, so we really can't read your code, you say a constructor "doesn't work" and yet you don't say how it doesn't work, how it's supposed to work, what error messages you've gotten,....

I mean come on here. If you want help from us, you have to put in some work on this post first. We are volunteers, and your job is to make our job of helping you as easily as possible. You've failed that. Please try again, and we'll see.

petes1234a at 2007-7-28 18:59:56 > top of Java-index,Java Essentials,New To Java...
# 3

Oh, you need help immediately. No probs, I will drop everything I'm doing and perform the act of your slave straight away becuase you are soooooo much more important than me or anyone else.

NOT!

And it has only been about 10 minutes. Learn some patience butthole.

floundera at 2007-7-28 18:59:56 > top of Java-index,Java Essentials,New To Java...
# 4

Describe your problem in as much detail as needed, but not too much info. be precise, be concise. Don't ask "I don't know what to do" rather say, "my goal is to get my foo function create bar variables. I've tried this code, for (i = 0; i < foo.length; i++)

but it failed by causing an SnafuClassException that showed this error message: XXXXXXX

etc....

Again, if you expect us to work hard, dmn it, you must work hard. Get cracking!

petes1234a at 2007-7-28 18:59:56 > top of Java-index,Java Essentials,New To Java...
# 5

sorry if anything wrong

here the error message

CustomerInfo.java:19: cannot find symbol

symbol : constructor Customer(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)

location: class Customer

Customer Cu = new Customer(name, address, telephone, cuNum, mailing);

thecanma at 2007-7-28 18:59:56 > top of Java-index,Java Essentials,New To Java...
# 6

Look very very carefully at your Customer constructor and the parameters it is expecting.

floundera at 2007-7-28 18:59:56 > top of Java-index,Java Essentials,New To Java...
# 7

so carefully look and tell me, what kind of variables does your customer constructor accept? I'll give you that it accepts at least 4 strings, right? but what is that darn 5th parameter?

To see this you have to look at where you created the customer constructor. What is that little variable at the end of the parameter list called M? What type of variable is it? String? int? neither?

And then later when you try to use that constructor to initialize a young constructor variable, you throw a String in to the last parameter spot, hurting that poor little constructor's feelings. It only wanted to be respected, to have a variable of the proper type in that slot, but NO, NO, you wouldn't do that.

So, how do we solve this little melodrama before the nielson's ratings start falling? hm?There's a solution, and you provide the key.

petes1234a at 2007-7-28 18:59:56 > top of Java-index,Java Essentials,New To Java...
# 8

I 've been looking at for it 2 hours

I can not find anything wrong.

everything look find to see, but it still error.

thank anyway

thecanma at 2007-7-28 18:59:56 > top of Java-index,Java Essentials,New To Java...
# 9

well, we can only point you. You must see the light and follow it.

Good luck

petes1234a at 2007-7-28 18:59:56 > top of Java-index,Java Essentials,New To Java...
# 10

FFS. Here is your constructor:

public Customer(String N, String A, String T, String CN, boolean M)

Read the parameters off one-by-one and tell me what types they are. Then look at your code where you create a new Customer object and tell me what types the parameters are. DO THEY MATCH?

floundera at 2007-7-28 18:59:56 > top of Java-index,Java Essentials,New To Java...
# 11

FFS. Here is your constructor:

public Customer(String N, String A, String T, String CN, boolean M)

Read the parameters off one-by-one and tell me what types they are. Then look at your code where you create a new Customer object and tell me what types the parameters are. DO THEY MATCH?

When i changed new Customer object the mailing to boolean

it still has error.

thecanma at 2007-7-28 18:59:56 > top of Java-index,Java Essentials,New To Java...
# 12

> I 've been looking at for it 2 hours

>

> I can not find anything wrong.

>

> everything look find to see, but it still error.

The compiler is not lying.

You're trying to invoke a constructor that doesn't exist.

class Foo {

Foo(int i) {}

}

...

String str = "abc";

int i = 123;

Foo foo = new Foo(str, i);

How could that possibly work?

jverda at 2007-7-28 18:59:56 > top of Java-index,Java Essentials,New To Java...
# 13

> en i changed new Customer object the mailing to

> boolean

> it still has error.

Post updated code. When you do, highlight it and click code button to retain formatting and make it easier to read.

floundera at 2007-7-28 18:59:56 > top of Java-index,Java Essentials,New To Java...
# 14

updated codes

boolean mailing;

Customer Cu = new Customer(name, address, telephone, cuNum, mailing);

error message is even more than before

stomerInfo.java:19: variable name might not have been initialized

Customer Cu = new Customer(name, address, telephone, cuNum, mailing);

^

CustomerInfo.java:19: variable address might not have been initialized

Customer Cu = new Customer(name, address, telephone, cuNum, mailing);

^

CustomerInfo.java:19: variable telephone might not have been initialized

Customer Cu = new Customer(name, address, telephone, cuNum, mailing);

^

CustomerInfo.java:19: variable cuNum might not have been initialized

Customer Cu = new Customer(name, address, telephone, cuNum, mailing);

^

CustomerInfo.java:19: variable mailing might not have been initialized

Customer Cu = new Customer(name, address, telephone, cuNum, mailing);

^

5 errors

thecanma at 2007-7-28 18:59:56 > top of Java-index,Java Essentials,New To Java...
# 15

Read the error message. You now have a completely different problem. Declaring and initialising variables are two totally different things.

int x; // declaration

x = 5; // initialisation

int y = 10; // declaration and initialisation

floundera at 2007-7-28 19:00:01 > top of Java-index,Java Essentials,New To Java...
# 16

> > en i changed new Customer object the mailing to

> > boolean

> > it still has error.

>

> Post updated code. When you do, highlight it and

> click code button to retain formatting and make it

> easier to read.

thecanm,

Please pay attention to what people write.

When you post code, please use[code] and [/code] tags as described in Formatting tips on the message entry page. (http://forum.java.sun.com/help.jspa?sec=formatting) It makes it much easier to read.

I'm outta here.

jverda at 2007-7-28 19:00:01 > top of Java-index,Java Essentials,New To Java...
# 17

im trying to improve my debugging skills. is this the line that is causing the problem?

public void setMailing(String M)

pberardi1a at 2007-7-28 19:00:01 > top of Java-index,Java Essentials,New To Java...
# 18

variables inside a method should be explicitly initialized, u havnt done that so error,

To avoid that errors declare the variables as class members.

b.m.krajua at 2007-7-28 19:00:01 > top of Java-index,Java Essentials,New To Java...
# 19

> im trying to improve my debugging skills. is this

> the line that is causing the problem?

>

> >

> public void setMailing(String M)

>

>

>

Is that the Constructor? However, that will cause another error when OP fixes current ones.

floundera at 2007-7-28 19:00:01 > top of Java-index,Java Essentials,New To Java...
# 20

> variables inside a method should be explicitly

> initialized, u havnt done that so error,

> To avoid that errors declare the variables as class

> members.

NO! OP needs to actually assign values to the variables before he/she attempts to use them.

floundera at 2007-7-28 19:00:01 > top of Java-index,Java Essentials,New To Java...
# 21

> I 've been looking at for it 2 hours

>

> I can not find anything wrong.

>

> everything look find to see, but it still error.

> thank anyway

If you don't see the difference between String and boolean, then I think the problem is understanding what Java is. It's a strongly typed language.

http://en.wikipedia.org/wiki/Strong_typing#Variation_across_programming_languages

Are you coming from a background of some sort of scripting language, perhaps?

This (strong typing) is a pretty difficult concept to pick up at first, but hang in there.

Also, as pointed out, note that the errors you are now facing are completely different. So your post of "the error is still there" without any follow-up post would have been completely misleading. Note that the error gives you the line number that is causing trouble. This will usually be enough to help you solve the problem yourself.

bheilersa at 2007-7-28 19:00:01 > top of Java-index,Java Essentials,New To Java...
# 22

this also could be a problem no?

public static void main(String[] arg)

{

String address;

String name;

String telephone;

String cuNum;

String mailing;

pberardi1a at 2007-7-28 19:00:01 > top of Java-index,Java Essentials,New To Java...
# 23

> im trying to improve my debugging skills. is this

> the line that is causing the problem?

Post 14 (with the compiler errors) shows which line numbers the problems are on.

bheilersa at 2007-7-28 19:00:01 > top of Java-index,Java Essentials,New To Java...
# 24

she seems to be spinning her wheels in the mud. She needs to get a solid foundation of java knowledge before she can get out of the muck.

petes1234a at 2007-7-28 19:00:01 > top of Java-index,Java Essentials,New To Java...
# 25

> this also could be a problem no?

>

>public static void main(String[] arg) {

> String address;

> String name;

> String telephone;

> String cuNum;

> String mailing;

>

In and of itself, there is no problem here. There is no requirement that a variable must be assigned a value at the time it is declared.

But if I were to try and do something like String a = address.toString();

Then yes, that is the problem that is mentioned above, and is exactly what the compiler errors in post 14 are complaining about.

bheilersa at 2007-7-28 19:00:01 > top of Java-index,Java Essentials,New To Java...
# 26

String a = address.toString();

<PBS>

address is a String, I wouldn't call the toString method.

</PBS>

Also, that would give a NPE.

Message was edited by:

flounder

floundera at 2007-7-28 19:00:01 > top of Java-index,Java Essentials,New To Java...
# 27

What i am saying i guess is that he looks like he wants mailing to be a boolean because that is how it is in the constructor but he declares it as a String. I definitely dont wanna get flamed when its not even my problem but I also want to say what i think.

pberardi1a at 2007-7-28 19:00:01 > top of Java-index,Java Essentials,New To Java...
# 28

and if you read the rest of the posts you will see we have already addressed this problem.

floundera at 2007-7-28 19:00:01 > top of Java-index,Java Essentials,New To Java...
# 29

> What i am saying i guess is that he looks like he

> wants mailing to be a boolean because that is how it

> is in the constructor but he declares it as a String.

> I definitely dont wanna get flamed when its not even

> my problem but I also want to say what i think.

Yeah, that's a good point. So often it's hard to decide which type to use. I bet the specs say that the user will be entering the value as a "yes" or "no" (and probably also "YES", "Yes", "yEs", etc), while the business logic of the system is pushing the developer to treat the field as a boolean.

The API of the constructor (and getter/setter methods for that matter) would be better off adhering to the specs the caller (i.e. end-user) is expecting. My guess is this is just a personal project though, in which case you might as well treat "mailing" as a boolean everywhere.

bheilersa at 2007-7-28 19:00:01 > top of Java-index,Java Essentials,New To Java...
# 30

updated codes

still have the same problem with the constructor

the boolean may cause the problem

but i still need one boolean variable.

import java.util.Scanner;

public class CustomerInfo

{

public static void main(String[] arg)

{

String address;

String name;

String telephone;

String cuNum;

String mailing;

Scanner keyboard = new Scanner(System.in);

System.out.println(" **************************************");

System.out.println(" *Person and Custmoer Classes*");

System.out.println(" **************************************");

System.out.println();

Customer cust = new Customer (name, address, telephone, cuNum, mailing);

System.out.print("Enter the customer name: ");

name= keyboard.nextLine();

System.out.print("Enter the customer address: ");

address = keyboard.nextLine();

System.out.print("Enter the telephone number: ");

telephone= keyboard.nextLine();

System.out.print("Enter the customer Number: ");

cuNum= keyboard.nextLine();

System.out.print("Does he or she want to be on mailing list? ");

mailing = keyboard.nextLine();

// Displaying

System.out.println("Name: " + cust.getName());

System.out.println("Address: " + cust.getAddress());

System.out.println("Telephone: " + cust.getPhone());

System.out.println("Cust Number: " + cust.getcuNum());

System.out.println("Want to be on the mailing list: " + cust.getMailing());

}

}

thecanma at 2007-7-28 19:00:06 > top of Java-index,Java Essentials,New To Java...
# 31

God bless but you are frustrating.....

What about this concept don't you understand:

a constructor like so

public Fubar(boolean b)

{

}

will not accept a call like so:

String stringVar = "WTF";

Fubar myFubar = new Fubar(stringVar);

You can't put a String variable where a boolean is expected. Done. Period. End of story. What is so fcking hard about this concept that you don't get?you have to either change the constructor, or change how you call it.

petes1234a at 2007-7-28 19:00:06 > top of Java-index,Java Essentials,New To Java...
# 32

changes String mailing to boolean mailing

is that what u mean, but have problem.

thecanma at 2007-7-28 19:00:06 > top of Java-index,Java Essentials,New To Java...
# 33

> changes String mailing to boolean mailing

> is that what u mean, but have problem.

I have a feeling u really don't know what u are doing even with the examples provided to you.

let me ask u a question: what are constructers and why are they used?

Message was edited by:

deAppel

deAppela at 2007-7-28 19:00:06 > top of Java-index,Java Essentials,New To Java...
# 34

The fact of the matter is that no matter how much pleading, begging, or complaining, you're not going to get a square peg into a round hole. Either create a constructor that takes square pegs (a String at that position) or fill the slot with a round peg (a boolean variable).

Do you understand this concept?

petes1234a at 2007-7-28 19:00:06 > top of Java-index,Java Essentials,New To Java...
# 35

Immediately, you say?

georgemca at 2007-7-28 19:00:06 > top of Java-index,Java Essentials,New To Java...
# 36

> Immediately, you say?

Too late to help him now ;(

deAppela at 2007-7-28 19:00:06 > top of Java-index,Java Essentials,New To Java...
# 37

Another problem, you will need to have this line after you have received user input not before.

Customer cust = new Customer (name, address, telephone, cuNum, mailing);

floundera at 2007-7-28 19:00:06 > top of Java-index,Java Essentials,New To Java...
# 38

I have figured out.

this is the way it should be, no more problem

Customer cust = new Customer (name, address, telephone, cuNum,

mailing.equalsIgnoreCase("Y"));

thecanma at 2007-7-28 19:00:06 > top of Java-index,Java Essentials,New To Java...
# 39

What if the user enters "yes" instead?

floundera at 2007-7-28 19:00:06 > top of Java-index,Java Essentials,New To Java...
# 40

> What if the user enters "yes" instead?

That's pretty obvious! I looked into it, and found that "yes" does not equal "Y", so the mailing field will be set to false.

Seriously though, assuming the user is the developer, everything will be okay.

bheilersa at 2007-7-28 19:00:06 > top of Java-index,Java Essentials,New To Java...
# 41

> > What if the user enters "yes" instead?

>

> That's pretty obvious! I looked into it, and found

> that "yes" does not equal "Y", so the mailing field

> will be set to false.

>

> Seriously though, assuming the user is the developer,

> everything will be okay.

I don't really understand your point. I was trying to get the OP to think about how their program will be used. How many programs have only the developer as the sole user? IMHO, when you write code you should think about all the ways a user can fuck your program and make it crash it do something incorreclty and fix them so this will never happen.

floundera at 2007-7-28 19:00:06 > top of Java-index,Java Essentials,New To Java...
# 42

> I don't really understand your point. I was trying to

> get the OP to think about how their program will be

> used. How many programs have only the developer as

> the sole user? IMHO, when you write code you should

> think about all the ways a user can **** your

> program and make it crash it do something incorreclty

> and fix them so this will never happen.

Honestly, I guess I didn't really have a point. In all seriousness I agree with you. Even programs I have written knowing I would be the sole user, if I forget that my input is supposed to be only lower/upper case, then I'll probably crash it myself by putting in bad data.

Shouldn't validation of user input be the responsibility of the UI? I think the signature of the constructor should remain as is (boolean being the type of last parameter), and before the constructor is called the verification of the input is done.

bheilersa at 2007-7-28 19:00:06 > top of Java-index,Java Essentials,New To Java...
# 43

> Shouldn't validation of user input be the

> responsibility of the UI? I think the signature of

> the constructor should remain as is (boolean being

> the type of last parameter), and before the

> constructor is called the verification of the input

> is done.

Definately. I'm a firm believer that a constructor should only receive valid data. What I was suggesting was something like this as the last parameter.

string, string, string, (mailing.equalsIgnoreCase("Y") || mailing.equalsIgnoreCase("yes"))

Or if that is too ugly for your liking, do the validation beforehand and assign it to a boolean variable and use that as a parameter.

floundera at 2007-7-28 19:00:06 > top of Java-index,Java Essentials,New To Java...
# 44

how about i coded that

enter y for yes and n for no.

what do u think?

thecanma at 2007-7-28 19:00:06 > top of Java-index,Java Essentials,New To Java...
# 45

Still not a good idea. No guarantee that people will enter it correctly. And classes should be defensive and make sure that the inputs they get are correct.

Why not a boolean? true/false; yes/no. I think Boolean.valueOf("Y") is the way to go. Read up on it.

%

duffymoa at 2007-7-28 19:00:11 > top of Java-index,Java Essentials,New To Java...
# 46

> Still not a good idea. No guarantee that people will

> enter it correctly. And classes should be defensive

> and make sure that the inputs they get are correct.

>

> Why not a boolean? true/false; yes/no. I think

> Boolean.valueOf("Y") is the way to go. Read up on

> it.

>

> %

Still you have to admit he/she is getting a little closer to the goal. They're using a boolean where there should be a boolean, and at least that's a good thing.

petes1234a at 2007-7-28 19:00:11 > top of Java-index,Java Essentials,New To Java...
# 47

> . I'm a firm believer that a constructor

> should only receive valid data.

I disagree.

If a c'tor is public, you can't guarantee it will receive valid data. In general, you should validate the data and throw IllegalArgumentException or something, else the problem may not manifest itself until who knows when, when you try to use the invalidly initialized object.

jverda at 2007-7-28 19:00:11 > top of Java-index,Java Essentials,New To Java...
# 48

> > . I'm a firm believer that a constructor

> > should only receive valid data.

>

> I disagree.

>

> If a c'tor is public, you can't guarantee it will

> receive valid data. In general, you should validate

> the data and throw IllegalArgumentException or

> something, else the problem may not manifest itself

> until who knows when, when you try to use the

> invalidly initialized object.

This is a place where I get confused. It seems like you ought to validate all data entered through the UI, within the UI components. It makes sense to me, intuitively though I haven't worked it out scientifically, that validating before constructing the object would be preferable. Why construct an object you are going to throw away?

Then again, I also see that after you've delivered the app, someone may build on top of it and call your constructor directly. So I agree that validation should probably also be done there.

But I would not repeat the user-input validation within the constructor. I would do business logic validations, such as making sure all relationships are set correctly, and that if two variables have some sort of dependency, make sure their values make sense together.

Do you think the validation of user-entered information needs to be done in both places, or just within the c'tor? If not, what's the arguement for not validating in the UI? If we are programming MVC-style and the persisting object is swapped out for a new one, we'll have lost all our user-input validation.

bheilersa at 2007-7-28 19:00:11 > top of Java-index,Java Essentials,New To Java...
# 49

> > . I'm a firm believer that a constructor

> > should only receive valid data.

>

> I disagree.

Imagine when you where born, your parents decided to call you Veronica. Is it your responsibility to throw a StupidNameException? Hopefully a decent nurse would step in and say "Wouldn't Jeff be a better name?".

<he says this knowing he won't win an argument with jverd>

floundera at 2007-7-28 19:00:11 > top of Java-index,Java Essentials,New To Java...
# 50

> Imagine when you where born, your parents decided to

> call you Veronica. Is it your responsibility to throw

> a StupidNameException? Hopefully a decent nurse would

> step in and say "Wouldn't Jeff be a better name?".

I don't see this as a good analogy, though I can't immediately say why.

I'm not saying don't do any validation before calling the c'tor. Rather, I'm saying a public c'tor can't assume that validation has been done. Its job isn't to check that the name makes sense, mind you, but rather that its legal based on the Person class' contract.

> <he says this knowing he won't win an argument with

> jverd>

I've been known to change my mind on more than a few occasions.

jverda at 2007-7-28 19:00:11 > top of Java-index,Java Essentials,New To Java...
# 51

> > What if the user enters "yes" instead?

>

> That's pretty obvious! I looked into it, and found

> that "yes" does not equal "Y", so the mailing field

> will be set to false.

>

> Seriously though, assuming the user is the developer,

> everything will be okay.

hardly. He'll sooner rather than later forget the required input :)

jwentinga at 2007-7-28 19:00:11 > top of Java-index,Java Essentials,New To Java...
# 52

String address;

String name;

String telephone;

String cuNum;

String mailing;

where are these initialized?

m00dya at 2007-7-28 19:00:11 > top of Java-index,Java Essentials,New To Java...
# 53

> > . I'm a firm believer that a constructor

> > should only receive valid data.

>

> I disagree.

>

> If a c'tor is public, you can't guarantee it will

> receive valid data. In general, you should validate

> the data and throw IllegalArgumentException or

> something, else the problem may not manifest itself

> until who knows when, when you try to use the

> invalidly initialized object.

One way to conciliate both views would be to use a factory method, e.g. CreateCustomer(..).

The factory method would do the validation before calling the Ctor.

In the birth & nurse analogy, the input name "Veronica" would be checked by a validation process (a nurse proxy checking against a dictionary database ..) which would dismiss the name input by throwing a NameNotMatchingSexException...

Hopefully the second input ("Jeff") would be validated by the factory method which would then call the Name Ctor.

karma-9a at 2007-7-28 19:00:11 > top of Java-index,Java Essentials,New To Java...