Zain Mohamed Zain Mohamed - 1 month ago 7
Java Question

Output will not print the correct information

I'm trying to make a program which asks the user a particular bird then how many of them they had seen at that point. If the use at any point enters the word 'END' then the system should print out the most seen bird and the number seen. However, when running my program if I enter 'END' at random points it instead returns that the most seen was END with 0 seen. I can't figure out how to make it work. I've tried different methods but it's just not working properly. Also, I've set the maximum array limit to 10 possitions but it continues after 10 and if i enter a value the system crashes. Have I written the limit part properly? Or am I missing something important?

import java.util.Scanner;

public class testing
{
public static void main (String[] param)
{
birdInput();
most();
System.exit(0);
}

public static void birdInput()
{
int i = 0;
String birdInput;
int numberInput;
Scanner scanner = new Scanner(System.in);
int maxVal = Integer.MIN_VALUE;
int maxValIndex = -1;
while (true)
{
System.out.println("What bird did you see?");
birdInput = scanner.nextLine();
if (birdInput.equals("END"))
{
System.out.print("\nWell....I guess thanks for using this program?\n");
System.exit(0);
}
else
{
String[] birds = new String[10];
int[] numbers = new int[10];
birds[i] = scanner.nextLine();
System.out.println("How many did you see?");
numbers[i] = scanner.nextInt();
i++;
if (birds[i].equals("END"))
{
maxVal = numbers[i];
maxValIndex = i;
System.out.print("\nThe most common bird that you saw was the " + birds[maxValIndex] + " with " + maxVal + " being seen in total\n");
System.exit(0);
}
}
}
}
public static void most()
{
System.out.println("fdff");
}
}


This is my edit of Till Hemmerich's answer to my issue. I tried to remove the global variables and so combine the entire code into 1 method. However, I'm still having some issues. Been working at it but really confused.

import java.util.Scanner;

public class birds2
{
public static void main(String[] param)
{
birdInput();
System.exit(0);
}

public static void birdInput()
{
Scanner scanner = new Scanner(System.in);
String[] birds = new String[99999999];
int[] numbers = new int[99999999];
int i = 0;
int maxIndex;
while (i <= birds.length)
{
System.out.println("What bird did you see?");
birds[i] = scanner.nextLine();
System.out.println("How many did you see?");
numbers[i] = scanner.nextInt();
i++;
}
int newnumber = numbers[i];
if ((newnumber > numbers.length))
{
maxIndex = i;
i++;
}
if (birds[i].toUpperCase().equals("END"))
{
System.out.print("\nWell....I guess thanks for using this program?\n");
System.out.print("\nThe most common bird that you saw was the " + birds[maxIndex] + " with " + numbers[maxIndex] + " being seen in total\n");
System.exit(0);
}
}
}

Answer

I changed a lot so im going to explain my changes here in total.

First of all i had to move the Array Definition out of your while-loop as >mentioned above, since other wise you would override these Arrays every time. I also made them globally accessible to work with them in other methods.

    public static int maxIndex;
    public static String[] birds = new String[10];
    public static int[] numbers = new int[10];

in general I re structured the whole code a little bit to make it more readable and a little bit more object-orientated. For example I created an method called inputCheck() which returns our input as a String and check if it equals END so you do not have to write your logic for this twice. (it also considers writing end lower or Uppercased by just Upper our input before checking it"if (input.toUpperCase().equals("END"))")

static String inputCheck() {
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        if (input.toUpperCase().equals("END")) {
            end();
        }
        return input;
    }

this method can now be called every time you need an input like this:

birds[i] = inputCheck();

but you need to be carefull if you want to get an integer out of it you first have to parse it like this:Integer.parseInt(inputCheck()) after that I wrote a method to search for the biggest Value in your numbers Array and getting its index:

public static int getMaxIndex(int[] numbers) {
    for (int i = 0; i < numbers.length; i++) {
        int newnumber = numbers[i];
        if ((newnumber > numbers.length)) {
            maxIndex = i;
        }
    }
    return maxIndex;

}

it takes an int array as parameter and returns the index of the highest element in there as an Integer. Called like this:maxIndex = getMaxIndex(numbers);

Then after that I rewrote your end method. It now just calles our getMaxIndex method and prints some output to the console.

    public static void end() {
    maxIndex = getMaxIndex(numbers);
    System.out.print("\nWell....I guess thanks for using this program?\n");
    System.out.print("\nThe most common bird that you saw was the " + birds[maxIndex] + " with " + numbers[maxIndex] + " being seen in total\n");
    System.exit(0);

}

to fix your last problem (crashing after more then 10 inputs)I changed your while-loop. Since your array only has 10 places to put things it crashes if you try to put information in place number 11. it not looks like this:while (i <= birds.length) instead of while (true) this way the max loops it can take is the amout of places Array birds has and it wont crash anymore.

public static void birdInput() {
        int i = 0;
        while (i <= birds.length) {
            System.out.println("What bird did you see?");
            birds[i] = inputCheck();
            System.out.println("How many did you see?");
            numbers[i] = Integer.parseInt(inputCheck()); //you should check here if its actuall a number otherwiese your programm will crash 
            i++;

        }
    }

Here is the whole code in total:

import java.util.Scanner;

/** * * @author E0268617 */ public class JavaApplication1 {

    public static int maxIndex;
    public static String[] birds = new String[10];
    public static int[] numbers = new int[10];

    public static void main(String[] param) {
        birdInput();
        most();
        System.exit(0);
    }

    public static void birdInput() {
        int i = 0;
        while (i <= birds.length) {
            System.out.println("What bird did you see?");
            birds[i] = inputCheck();
            System.out.println("How many did you see?");
            numbers[i] = Integer.parseInt(inputCheck()); //you should check here if its actuall a number otherwiese your programm will crash 
            i++;

        }
    }

    static String inputCheck() {
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        if (input.toUpperCase().equals("END")) {
            end();
        }
        return input;
    }


public static int getMaxIndex(int[] numbers) {
    for (int i = 0; i < numbers.length; i++) {
        int newnumber = numbers[i];
        if ((newnumber > numbers.length)) {
            maxIndex = i;
        }
    }
    return maxIndex;

}

public static void end() {
     maxIndex = getMaxIndex(numbers);
    System.out.print("\nWell....I guess thanks for using this program?\n");
    System.out.print("\nThe most common bird that you saw was the " + birds[maxIndex] + " with " + numbers[maxIndex] + " being seen in total\n");
    System.exit(0);

}

public static void most() {
    System.out.println("fdff");
}
}

I hope you understand where the Problems had been hidden if you have any Questions hit me up.

Comments