My program is supposed to read animal names off of a file and determine whether or not they are between walrus
and dinosaur
in a dictionary. I thought this program was correct.
However, I keep on receiving the wrong output and I am assuming my problem is happening in the compare.to
method and my if
statements.
If anyone was wondering, it is a requirement for me to use character arrays.
Can someone please explain to me what is wrong with my program?
Scanner inFile = null;
try {
// will read this file
inFile = new Scanner (new File("Prog505c.dat"));
}
// will print file not found if no file is found and will also exit the program.
catch (FileNotFoundException e) {
System.out.println ("File not found!");
System.exit (0);
}
String strLine = " ";
int Scope;
do {
strLine=inFile.nextLine() ;
char[] animals = strLine.toCharArray();
String dino = "Dinosaur";
char[] dinosaur = dino.toCharArray();
String wal = "Walrus";
char[] walrus = wal.toCharArray();
int ResultOne =animals.toString().compareToIgnoreCase(walrus.toString());
int ResultTwo =animals.toString().compareToIgnoreCase(dinosaur.toString());
if (ResultOne > 0&& ResultTwo < 0) {
System.out.println(strLine +" Not between");
} else {
System.out.println(strLine + " between");
}
}while (inFile.hasNextLine()) ;
My output is
Vampire between
Monkay between
Elephant between
Ape Not between
Lion between
Hippopotamus between
Ant between
Zebra between
Yak between
Antelope between
Dingo between
Whale between
My output is supposed to be
Vampire between
Monkey between
Elephant between
Ape not between
Lion between
Hippopotamus between
Ant not between
Zebra not between
Yak not between
Antelope not between
Dingo not between
Whale not between