Re: Need basic help....
TheBigPJ wrote:
It doesn't seem to want to save my char to a file. Can anyone see what
is wrong? Ive spent a lot of time trying to figure it out and just
cant seem to work it out.
I can't see that you called your savea()-function anywhere =)
I know I have a lot of altering to do yet before I can even think im
*snip*
public void startCommandLine()
{
boolean finished;
finished = false;
BufferedReader keyboardInput = new BufferedReader(new
InputStreamReader(System.in));
String temp;
try {
while (finished == false) {
System.out.println("Type in your option:\n1.Set Char (e.g. 1.G or
1.r)\n2.Show and Save\n3.Compare with? (e.g. 3.r)\n4.Exit");
temp = keyboardInput.readLine();
if(temp.charAt(0) == '1')
{
this.newa(temp.charAt(2));
}
else if(temp.equals("2"))
{
System.out.println("You char value is: " + (int)this.returna() +
".\n");
I would have used a method for this if I were you. Overriding
Object.toString would suffice here. Then you can just type
System.out.println(this);
}
*snip*
public void comparea(char comparee)
{
if((int)this.a < (int)comparee)
{
System.out.println(a + " is less than " + comparee + ".");
}
else if((int)this.a == (int)comparee)
{
System.out.println(a + " is the same as " + comparee + ".");
}
else if((int)this.a > (int)comparee)
{
System.out.println(a + " is more than " + comparee + ".");
}
}
}
This is ok for learning purposes, but normally one would place such code
in a method overriding Object.equals taking in another Object as an
argument instead. Here's an example for your class. Note that a get()
method is used here in place of your returna() method (they would be
identical).
public boolean equals(Object otherObject)
{
q4 comparee = (q4)otherObject;
//Test whether comparee object is a valid q4 object and not a
//null-reference. Someone please fill me in here, I don't
//remember what a wrongful cast does :p
//I dont think you need to cast these chars to int to compare //them
below.
//If char value lower or higher, return false. Otherwise return //true
if( (int)this.a < (int)comparee.get() ||
(int)this.a > (int)comparee.get() )
{
return false
}
else
{
return true;
}
}
I suggest you take care of the output somewhere else.