0

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
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
a.p
  • 25
  • 3

4 Answers4

1

This line is your problem:

if (ResultOne > 0&& ResultTwo < 0)

This is checking if the string is both after "Walrus" and before "Dinosaur", which is clearly impossible. That check never passes, so it always goes to the else block and prints "between". To fix it, simply change && to ||.

Douglas
  • 5,017
  • 1
  • 14
  • 28
0

1) Use Conditional-OR and,

2) When converting from char[] to String, Use new String(charArray) instead of toString() because arrays don't override toString. (P.S. Whoever gave you this assignment wanted you to understand and learn this concept.)

Ref > How to convert a char array back to a string?

ajc
  • 1,685
  • 14
  • 34
0

you have your if with an impossible option, you need to re organize it:

if (ResultOne < 0 &&  ResultTwo > 0)
{//here between}
else
{here not between}

as you put the if never will be posible because you need a world below dinosaur and above walus at same time.

Xenolion
  • 12,035
  • 7
  • 33
  • 48
Ferba
  • 16
  • 5
0

The problem with your code is, it's comparing the entire input string with the entire string (i.e) you are comparing "Vampire" with "Dinosaur" and "Walrus" here:

int ResultOne =animals.toString().compareToIgnoreCase(walrus.toString());

int ResultTwo =animals.toString().compareToIgnoreCase(dinosaur.toString());

That's why its giving the output as "between" everytime, since your code has "between" in the else statement which will be executed when your condition is not satisfied. In your case Walrus, Dinosaur and Vampire is not equal.

Actually you need to compare the first character of input string with the first character of dinosaur and walrus. So the code should be like this :

int ResultOne =String.valueOf(animals[0]).compareToIgnoreCase(String.valueOf(dinosaur[0]));

int ResultTwo =String.valueOf(animals[0]).compareToIgnoreCase(String.valueOf(walrus[0]));

And your if condition should be like this:

if (!(ResultOne > 0 &&  ResultTwo < 0))

Hope this helps!

Dora
  • 191
  • 8