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?
> 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 ?
well i am using eclipse it just have an X sign on the left of that line.
> I have no error.Thankyou same here. I forgot to save it Duh !
> you need a main methodRolling on the Floor Laughing :)
> > you need a main method> > Rolling on the Floor Laughing :)throws GlassHouseException :-)
> ...> 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"...
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.
> 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
> 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.
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
> 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!
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 > ();
> 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.
> 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
> > 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 {
// ...
}
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
> 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 >

> > 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.
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.
> 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.
> > 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.
> ...
> 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.
> 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 >

I bet what you really want to do is read a line of input, get the first character of that and discard the rest.
> 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.
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.
> > 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.
System.out.println("Guess a letter");char guess = s.next().charAt(0);
> > > 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 >

> > 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 >

Bad Bad Sun. I wish sun can add a char read in method also in the scanner class.
> > > > 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.
Why not read it in as a String and then cast it to a char
> 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 >

> > oops> > "No SCJP for you!" http://www.urbandictionary.com/define.php?term=Java+Nazi
> > > 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.)
> > > > 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
> 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.
> I bet all the thumbs down for that one were lodged by> script kiddies.I certainly don't doubt it.
> 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 >

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);
}
}
> 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 >

> 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......)