Reputation: 31
I have this program but I need to remove the global variables. Been trying to move things around and rewrite parts but I can't really figure it out.
import java.util.Scanner;
public class birds3
{
public static int maxIndex; //These three lines are the ones I'm talking about
public static String[] birds = new String[99999999];
public static int[] numbers = new int[99999999];
public static void main(String[] param)
{
whatBird();
inputCheck();
birdInput();
System.exit(0);
}
public static void whatBird()
{
System.out.println("\nType 'END' to finish the program and display the most common bird. \n\nEnter anykey to continue.");
return;
}
public static String inputCheck()
{
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
if (input.toUpperCase().equals("END"))
{
end();
}
return input;
}
public static void birdInput()
{
int i = 0;
while (i <= birds.length)
{
System.out.println("\nWhat bird did you see?");
birds[i] = inputCheck();
System.out.println("\nHow many did you see?");
numbers[i] = Integer.parseInt(inputCheck());
i++;
}
}
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);
}
}
If you could give me some tips on how to fix my issues then please do.
EDIT: Okay I've removed some of the global variables but something still isn't working. No matter what I enter or what value, if I enter end it'll always just print the first thing I entered and the first value. Whats the issue now?
import java.util.Scanner;
public class birds4
{
public static int maxIndex;
public static void main(String[] param)
{
String[] birds = new String[99999999];
int[] numbers = new int[99999999];
whatBird();
inputCheck(birds, numbers);
birdInput(birds, numbers);
System.exit(0);
}
public static void whatBird()
{
System.out.println("\nType 'END' to finish the program and display the most common bird. \n\nEnter anykey to continue.");
return;
}
public static String inputCheck(String[] birds, int[] numbers)
{
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
if (input.toUpperCase().equals("END"))
{
for (int i = 0; i < numbers.length; i++)
{
int newnumber = numbers[i];
if ((newnumber > numbers.length))
{
maxIndex = i;
}
}
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);
}
return input;
}
public static void birdInput(String[] birds, int[] numbers)
{
int i = 0;
while (i <= birds.length)
{
Scanner scanner = new Scanner(System.in);
System.out.println("\nWhat bird did you see?");
birds[i] = inputCheck(birds, numbers);
System.out.println("\nHow many did you see?");
numbers[i] = scanner.nextInt();
i++;
}
}
}
Upvotes: 1
Views: 385
Reputation: 140603
You have to learn about using "real" Objects, instead of using all static methods and fields.
Of course, your main class needs to be static so that you can call it from the command line. But then you normally do things like:
public class Example {
private final String exampleString;
public Example(String exampleString) { this.exampleString = exampleString; }
public String getExampleString { return exampleString; }
public static void main(String[] args) {
Example myExample = new Example("whatever");
System.out.println("The not so global example is: " + myExample.getExampleString());
}
}
That is the whole point of OOP: classes have non-static fields and methods; and you instantiate objects using new; and then each object has its own copy of those fields (whereas static fields are shared between all such objects; and thus "global"; and thus they should generally be avoided).
Upvotes: 3
Reputation: 18792
By way of example if String[] birds = new String[99999999];
and int[] numbers = new int[99999999];
are not global, and are needed by birdInput();
, you pass their reference by :
birdInput(birds,numbers);
And the signature of birdInput()
should be changed to :
public static void birdInput(String[] birds, int[] numbers)
Note that in some cases you need to change the logic and / or returned values.
Upvotes: 0