whats wrong?

This is my Account class

publicclass Account{

privateint accnum;

privateint purchases;

public Account(int accnum){

this.accnum = accnum;

}

publicint getAccnum(){

return accnum;

}

publicint getPurchases(){

return purchases;

}

publicvoid setPurchases(int purchases){

this.purchases = purchases;

}

}

And this is my AccountManager class

import java.util.Vector;

publicclass AccountManager{

private Vector<Account> v;

public AccountManager(){

v =new Vector<Account>();

}

publicvoid add(Account a){

v.addElement(a);

}

public Account search(int number){

for(Account a : v)

{

if(a.getAccnum()==number)//here i am getting error why?

return a;

}

returnnull;

}

}

Why my code if(a.getAccnum()==number) is giving me error?

[2572 byte] By [schumachera] at [2007-11-27 9:41:47]
# 1
Error?
BigDaddyLoveHandlesa at 2007-7-12 23:21:24 > top of Java-index,Java Essentials,New To Java...
# 2

> This is my Account class

> > public class Account {

> private int accnum;

> private int purchases;

>

> lic Account(int accnum) {

> this.accnum = accnum;

> }

>

> public int getAccnum() {

> return accnum;

> }

>

> public int getPurchases() {

> return purchases;

> }

>

> public void setPurchases(int purchases) {

> this.purchases = purchases;

> }

> }

>

>

> And this is my AccountManager class

> > import java.util.Vector;

>

> public class AccountManager {

> private Vector<Account> v;

>

> public AccountManager() {

> v = new Vector<Account>();

> }

>

> public void add(Account a) {

> v.addElement(a);

> }

>

> public Account search(int number) {

> for(Account a : v)

> {

> if(a.getAccnum()==number) //here i am getting

> ing error why?

> return a;

> }

> return null;

> }

> }

>

>

> Why my code if(a.getAccnum()==number) is giving me

> error?

Good thing they left it nice and generic and didn't put anymore information other than error - oh wait they do, so why do you think they include specific information - maybe to help solve problems, and for that reason don't you think it would be useful to include the whole thing ?

Aknibbsa at 2007-7-12 23:21:24 > top of Java-index,Java Essentials,New To Java...
# 3
well i am using eclipse it just have an X sign on the left of that line.
schumachera at 2007-7-12 23:21:24 > top of Java-index,Java Essentials,New To Java...
# 4
I have no error.
BigDaddyLoveHandlesa at 2007-7-12 23:21:24 > top of Java-index,Java Essentials,New To Java...
# 5
> I have no error.Thankyou same here. I forgot to save it Duh !
schumachera at 2007-7-12 23:21:25 > top of Java-index,Java Essentials,New To Java...
# 6
you need a main method
pberardi1a at 2007-7-12 23:21:25 > top of Java-index,Java Essentials,New To Java...
# 7
> you need a main methodRolling on the Floor Laughing :)
schumachera at 2007-7-12 23:21:25 > top of Java-index,Java Essentials,New To Java...
# 8
> > you need a main method> > Rolling on the Floor Laughing :)throws GlassHouseException :-)
georgemca at 2007-7-12 23:21:25 > top of Java-index,Java Essentials,New To Java...
# 9
> ...> Rolling on the Floor Laughing :)Perhaps it's not meant as a sarcastic remark, but be careful who you make fun of: you were the one with "a red cross" in his IDE and "forgot to save it"...
prometheuzza at 2007-7-12 23:21:25 > top of Java-index,Java Essentials,New To Java...
# 10
I am almost afraid to say this, but i did not mean that as sarcastic. Uh Oh I said it anyway. Waiting for stuff to get thrown at me.
pberardi1a at 2007-7-12 23:21:25 > top of Java-index,Java Essentials,New To Java...
# 11

> I am almost afraid to say this, but i did not mean

> that as sarcastic. Uh Oh I said it anyway. Waiting

> for stuff to get thrown at me.

Not only that, but the original poster just emailed me and said you were nothing but a big poofter and he could take your hardest punch easily

georgemca at 2007-7-12 23:21:25 > top of Java-index,Java Essentials,New To Java...
# 12

> I am almost afraid to say this, but i did not mean

> that as sarcastic. Uh Oh I said it anyway. Waiting

> for stuff to get thrown at me.

Just joking bro . thanks

it was not the main method but i forgot to file/save in eclipse. everythings working fine. i am new to data structures and inheritance, interfaces and polymorphism so will be bugging you guys. Thanks for the effor. appologize if i was rude pberadi1.

schumachera at 2007-7-12 23:21:25 > top of Java-index,Java Essentials,New To Java...
# 13
Just one more question. i dont want to post another topic. i am trying to create a word guesser program using the power of inheritance, interfaces. what should i use? vector, array or arraylist just a 2 player game?. after this i can start coding. Thanks
schumachera at 2007-7-12 23:21:25 > top of Java-index,Java Essentials,New To Java...
# 14

> Just one more question. i dont want to post another

> topic. i am trying to create a word guesser program

> using the power of inheritance, interfaces. what

> should i use? vector, array or arraylist just a 2

> player game?. after this i can start coding.

>

> Thanks

Post another topic!

georgemca at 2007-7-12 23:21:25 > top of Java-index,Java Essentials,New To Java...
# 15
Vector is a legacy class so there is no reason to prefer it over ArrayListunless other code expects a Vector. Try this:private List < ElementClass > yourList = new ArrayList < ElementClass > ();
BigDaddyLoveHandlesa at 2007-7-21 23:08:01 > top of Java-index,Java Essentials,New To Java...
# 16

> Just one more question. i dont want to post another

> topic. i am trying to create a word guesser program

> using the power of inheritance, interfaces.

What do you mean with "power of inheritance" and "interfaces"? It looks like you're just naming some OO-buzz-words at random. Please elaborate.

> what should i use? vector, array or arraylist just a 2

> player game?. after this i can start coding.

What do you need a dynamic list for?

Before starting such a game it is a good thing to write down (in plain English or whatever language you're familiar with) what the program is supposed to do in detail. After you've done that, you underline all nouns and verbs. The nouns are (possible) classes and the verbs are the (possible) methods of those classes.

Good luck.

prometheuzza at 2007-7-21 23:08:01 > top of Java-index,Java Essentials,New To Java...
# 17
> The nouns are (possible) classes and the verbs are the (possible) methods of those classes.When I do that I end up with too many things with names like BombDiggity
BigDaddyLoveHandlesa at 2007-7-21 23:08:01 > top of Java-index,Java Essentials,New To Java...
# 18

> > The nouns are (possible) classes and the verbs are

> > the (possible) methods of those classes.

>

> When I do that I end up with too many things with

> names like BombDiggity

@OP: Ah yes, forgot to mention this: the adjectives are your interfaces which can be randomly implemented by your classes.

Example:

public class BigDaddy extends Player implements BombDiggity {

// ...

}

prometheuzza at 2007-7-21 23:08:01 > top of Java-index,Java Essentials,New To Java...
# 19

mr promptheus thanks alot for the explanation. Ready to code looks like i will just use plain Array since my game consist of only 2 players.

******

player 1: x

sorry wrong word

player2: a

correct

a*****

player1: point 1

player1: p

correct

app**

player1: point 2

something like this. i ad to find a way to store my secret word in a method and then replace all the characters by *****. one way by looking at the api docs was to use Stringbuffer setCharAt method which will replace the original characers with *****. or the other way is to store it in a char Array. Lets see what happens

schumachera at 2007-7-21 23:08:01 > top of Java-index,Java Essentials,New To Java...
# 20

> well i am using eclipse it just have an X sign on the

> left of that line.

There is more information provided than that.

If you mouse over the red X, you'll see details. Also, if you try to build (or save with auto-build on) the Problems pane (or view or window or whatever eclipse calls it) will show details.

jverda at 2007-7-21 23:08:01 > top of Java-index,Java Essentials,New To Java...
# 21

> > well i am using eclipse it just have an X sign on

> the

> > left of that line.

>

> There is more information provided than that.

>

> If you mouse over the red X, you'll see details.

> Also, if you try to build (or save with auto-build

> on) the Problems pane (or view or window or whatever

> eclipse calls it) will show details.

Thanks that was really helpful.

schumachera at 2007-7-21 23:08:01 > top of Java-index,Java Essentials,New To Java...
# 22
No diggity!
BigDaddyLoveHandlesa at 2007-7-21 23:08:01 > top of Java-index,Java Essentials,New To Java...
# 23
how to take input from a user just a character? Scanner s = new Scanner(System.in);char c = s.next()//errorI tried searching on google cannot find it.
schumachera at 2007-7-21 23:08:01 > top of Java-index,Java Essentials,New To Java...
# 24

> how to take input from a user just a character?

> Scanner s = new Scanner(System.in);

> char c = s.next()//error

>

> I tried searching on google cannot find it.

Don't use "chocolate chip cookie" as a search string then. If you search for "Scanner+Java", it's the first hit:

http://www.google.com/search?hl=en&q=Scanner+java&btnG=Google+Search

Now look at the next() method in the API docs to see what it returns.

prometheuzza at 2007-7-21 23:08:01 > top of Java-index,Java Essentials,New To Java...
# 25

> > how to take input from a user just a character?

> > Scanner s = new Scanner(System.in);

> > char c = s.next()//error

> >

> > I tried searching on google cannot find it.

>

> Don't use "chocolate chip cookie" as a search

> string then. If you search for "Scanner+Java",

> it's the first hit:

> http://www.google.com/search?hl=en&q=Scanner+java&btnG

> =Google+Search

>

> Now look at the next() method in the API docs

> to see what it returns.

Yes prompt i googled it and got the same results what you have but not even one page specifies how to get a char input through scanner class. I checked the api docs i know next() takes String but i want a user to enter char. i know i can use BufferedInputStream but i am in love with scanner class.

schumachera at 2007-7-21 23:08:01 > top of Java-index,Java Essentials,New To Java...
# 26

> ...

> Yes prompt i googled it and got the same results what

> you have but not even one page specifies how to get a

> char input through scanner class. I checked the api

> docs i know next() takes String but i want a user to

> enter char. i know i can use BufferedInputStream but

> i am in love with scanner class.

If you can't find a method in the Scanner class that returns a single char, it means there is no such method.

Look at the String class to see how you would go about extracting a single char from a String object (a String you get by using Scanner's next() or better yet nextLine() method!).

Good luck.

prometheuzza at 2007-7-21 23:08:01 > top of Java-index,Java Essentials,New To Java...
# 27

> char input through scanner class. I checked the api

> docs i know next() takes String but i want a user to

> enter char. i know i can use BufferedInputStream but

So you looked at Scanner's docs.

You didn't see a method that reads a single char.

But yet you think there's a method that will read a single char? Like maybe a secret method that Sun didn't want to document?

> i am in love with scanner class.

That's a counterproductive attitude. Use the right tool for the job.

jverda at 2007-7-21 23:08:01 > top of Java-index,Java Essentials,New To Java...
# 28
I bet what you really want to do is read a line of input, get the first character of that and discard the rest.
BigDaddyLoveHandlesa at 2007-7-21 23:08:01 > top of Java-index,Java Essentials,New To Java...
# 29

> I bet what you really want to do is read a line of

> input, get the first character of that and discard

> the rest.

Was my question too vague? All i was trying to ask is how to read a single line character input from scanner class;

Scanner s = new Scanner(System.in);

char c = s.next.charAt(0);

something like this.

schumachera at 2007-7-21 23:08:01 > top of Java-index,Java Essentials,New To Java...
# 30
I bet what you really want to do, even if there was a way to read one character from a Scanner, is to discard the rest of the input line.
BigDaddyLoveHandlesa at 2007-7-21 23:08:06 > top of Java-index,Java Essentials,New To Java...
# 31

> > char input through scanner class. I checked the

> api

> > docs i know next() takes String but i want a user

> to

> > enter char. i know i can use BufferedInputStream

> but

>

> So you looked at Scanner's docs.

>

> You didn't see a method that reads a single char.

>

> But yet you think there's a method that will read a

> single char? Like maybe a secret method that Sun

> didn't want to document?

Yes this have been done in the past. i just can't find that post. will post back once i get it.

schumachera at 2007-7-21 23:08:06 > top of Java-index,Java Essentials,New To Java...
# 32
System.out.println("Guess a letter");char guess = s.next().charAt(0);
schumachera at 2007-7-21 23:08:07 > top of Java-index,Java Essentials,New To Java...
# 33

> > > char input through scanner class. I checked the

> > api

> > > docs i know next() takes String but i want a

> user

> > to

> > > enter char. i know i can use BufferedInputStream

> > but

> >

> > So you looked at Scanner's docs.

> >

> > You didn't see a method that reads a single char.

> >

> > But yet you think there's a method that will read

> a

> > single char? Like maybe a secret method that Sun

> > didn't want to document?

>

> Yes this have been done in the past. i just can't

> find that post. will post back once i get it.

Um, no.

What you see in the docs is what you get.

If there's no method that returns char in Scanner's docs, then Scanner doesn't have a method that returns a char.

jverda at 2007-7-21 23:08:07 > top of Java-index,Java Essentials,New To Java...
# 34

> > System.out.println("Guess a letter");

> char guess = s.next().charAt(0);

>

I believe that has already been suggested to you (or something very like it). Note that Scanner is not returning a char, however.

jverda at 2007-7-21 23:08:07 > top of Java-index,Java Essentials,New To Java...
# 35
Bad Bad Sun. I wish sun can add a char read in method also in the scanner class.
schumachera at 2007-7-21 23:08:07 > top of Java-index,Java Essentials,New To Java...
# 36

> > > > System.out.println("Guess a letter");

> > char guess = s.next().charAt(0);

> >

>

> I believe that has already been suggested to you (or

> something very like it). Note that Scanner is not

> returning a char, however.

i see where you going. i can pass that char now to my method to check and see if that matches the secret word.

schumachera at 2007-7-21 23:08:07 > top of Java-index,Java Essentials,New To Java...
# 37
Why not read it in as a String and then cast it to a char
pberardi1a at 2007-7-21 23:08:07 > top of Java-index,Java Essentials,New To Java...
# 38
> Why not read it in as a String and then cast it to a> charYou can't cast a String to a char.
jverda at 2007-7-21 23:08:07 > top of Java-index,Java Essentials,New To Java...
# 39
oops
pberardi1a at 2007-7-21 23:08:07 > top of Java-index,Java Essentials,New To Java...
# 40
> oops"No SCJP for you!"
Navy_Codera at 2007-7-21 23:08:07 > top of Java-index,Java Essentials,New To Java...
# 41
> > oops> > "No SCJP for you!" http://www.urbandictionary.com/define.php?term=Java+Nazi
BigDaddyLoveHandlesa at 2007-7-21 23:08:07 > top of Java-index,Java Essentials,New To Java...
# 42
> > > oops> > > > "No SCJP for you!"> > http://www.urbandictionary.com/define.php?term=Java+Na> ziThe greatest thing I've read all day. (I wondered where you got that from.)
Navy_Codera at 2007-7-21 23:08:07 > top of Java-index,Java Essentials,New To Java...
# 43

> > > > oops

> > >

> > > "No SCJP for you!"

> >

> >

> http://www.urbandictionary.com/define.php?term=Java+Na

>

> > zi

>

> The greatest thing I've read all day. (I wondered

> where you got that from.)

That was, until I read this:

http://www.urbandictionary.com/define.php?term=Java+Script

Navy_Codera at 2007-7-21 23:08:07 > top of Java-index,Java Essentials,New To Java...
# 44
> That was, until I read this:> http://www.urbandictionary.com/define.php?term=Java+Sc> riptI bet all the thumbs down for that one were lodged by script kiddies.
floundera at 2007-7-21 23:08:07 > top of Java-index,Java Essentials,New To Java...
# 45
> I bet all the thumbs down for that one were lodged by> script kiddies.I certainly don't doubt it.
Navy_Codera at 2007-7-21 23:08:12 > top of Java-index,Java Essentials,New To Java...
# 46
> Bad Bad Sun. I wish sun can add a char read in method> also in the scanner class.Why? It's a scanner and it's designed to return tokens. It does that. If you want to split the tokens up further, do that. If you don't want tokens, don't use a scanner.
ejpa at 2007-7-21 23:08:12 > top of Java-index,Java Essentials,New To Java...
# 47

You know, scanner is another one of those classes that I rarely (if ever) use (along with Layout Managers.)

@OP - why not something like this?

import java.io.*;

class Test {

public static void main(String[] argv) throws Exception {

InputStreamReader isr = new InputStreamReader(System.in);

System.out.print("Enter a char: ");

char c = (char)(isr.read());

System.out.println("Char = " + c);

}

}

Navy_Codera at 2007-7-21 23:08:12 > top of Java-index,Java Essentials,New To Java...
# 48

> You know, scanner is another one of those classes

> that I rarely (if ever) use (along with Layout

> Managers.)

>

> @OP - why not something like this?

> > import java.io.*;

>

> class Test {

> public static void main(String[] argv) throws

> s Exception {

>

> InputStreamReader isr = new

> ew InputStreamReader(System.in);

> System.out.print("Enter a char: ");

> char c = (char)(isr.read());

> System.out.println("Char = " + c);

> }

> }

>

>

You'll probably have to hit Return in most cases to get anything into System.in.

jverda at 2007-7-21 23:08:12 > top of Java-index,Java Essentials,New To Java...
# 49
> You'll probably have to hit Return in most cases to> get anything into System.in.You will. (Was that a requirement - to not have to hit the enter key? I confess to not having read the entire thread......)
Navy_Codera at 2007-7-21 23:08:12 > top of Java-index,Java Essentials,New To Java...