Scanner problem - need help
My code is becoming a scanner for some reason. When I run the code, it waits for input in the console, and when I enter anything, nothing happens, and it simply waits for more scanner input. I don't do anything with scanner, not even import it.
Here's the code.
Code Java:
import java.awt.Color;
import java.util.ArrayList;
public class Experiment {
private Aviary av;
private int[] results = new int[11];
public Experiment (Aviary x) {
av =x;
}
public void printResults() {
for(int i=0; i<=10; i++){
System.out.print(results[i]+" ");
}
System.out.println();
}
public int howManyRed(ArrayList<Bird> x) {
int count = 0;
for(int i = 0; i<x.size(); i++) {
if (x.get(i).getMyColor()==Color.red) {
count++;
}
}
return count;
}
public void runExperimentOnce() {
results[howManyRed(av.see10Birds())]++;
}
public void runExperimentManyTimes(int w) {
results = new int[11];
for(int i = 0; i<w; i++) {
runExperimentOnce();
}
double average=0;
for (int i =0; i <10; i++) {
average = average + (i * results[i]);
}
double weighted = 1.0 * average/w;
System.out.println(weighted + " red birds out of " + 10);
int slot = 0;
int counter = 0;
for(int i = 0; i<10; i++) {
if (results[i]> counter){
slot = i;
counter = results[i];
}
}
System.out.println("Highest category was " + counter + " with " + slot + " out of " + 1000000);
}
}
Re: Scanner problem - need help
Please surround your code with either highlight tags or code tags.
Where's your main method? There's no way for us to tell what happens without it.
Re: Scanner problem - need help
Sorry, I'm fairly new to programming and I don't really get what you mean by "code tags" and highlight tags. If you could explain those to me, that would be great.
And here is my main method. There are two other classes as well, aviary and bird. I can post those if needed too. My main method post below won't have code tags, sorry! But I'll start using them as soon as I figure them out.
The print methods are there for me to help figure out where the code is asking for scanner input. It prints test in console twice, meaning the scanner input is happening somewhere in the runExperimentManyTimes method, which happens to also be the most complicated. My updated version is below my main method.
Code Java:
import java.awt.Color;
import java.util.ArrayList;
public class PT5 {
public static void main(String[] args) {
ArrayList<Bird> birds = new ArrayList<Bird>(1000);
// put 1000 birds into the above ArrayList
for(int i=1; i<=1000; i++){
if(i%3==0){ // change the 3 to change the % of red birds
birds.add(new Bird(Color.red, i));
}else{
birds.add(new Bird(Color.blue, i));
}
}
System.out.println("test");
// create an Aviary
Aviary aviary = new Aviary(birds);
// create an Experiment
Experiment ex = new Experiment(aviary);
System.out.println("test");
// run the Experiment 1,000,000 times
ex.runExperimentManyTimes(1000000);
System.out.println("test");
// print out the contents of the results array of ints
ex.printResults();
System.out.println("test");
}// end of main
}
[B]Experiment.java Code below
Code Java:
import java.awt.Color;
import java.util.ArrayList;
public class Experiment {
private Aviary av;
private int[] results = new int[11];
public Experiment (Aviary x) {
av =x;
}
public void printResults() {
for(int i=0; i<=10; i++){
System.out.print(results[i]+" ");
}
System.out.println();
}
public int howManyRed(ArrayList<Bird> x) {
int count = 0;
for(int i = 0; i<x.size(); i++) {
if (x.get(i).getMyColor()==Color.red) {
count++;
}
}
return count;
}
public void runExperimentOnce() {
results[howManyRed(av.see10Birds())]++;
}
public void runExperimentManyTimes(int w) {
results = new int[11];
for(int i = 0; i<w; i++) {
runExperimentOnce();
}
double average=0;
for (int i =0; i <10; i++) {
results[howManyRed(av.see10Birds())]++;
}
double weighted = 1.0 * average/w;
System.out.println(weighted + " red birds out of " + 10);
int slot = 0;
int counter = 0;
for(int i = 0; i<10; i++) {
if (results[i]> counter){
slot = i;
counter = results[i];
}
}
System.out.println("Highest category was " + slot + " with " + counter + " out of " + 1000000);
}
}
Re: Scanner problem - need help
For information on how to use highlight tags, please read: Welcome! If it's your first time here, please read.
Likely it's not asking you for input, but is instead busy. It's also possible you've got an infinite loop somewhere Could you post the code for see10Birds()?
Re: Scanner problem - need help
Here's the code for see10birds.
Code Java:
public ArrayList<Bird> see10Birds() {
if (aviary.size() < 10) {
throw new RuntimeException("Not enough Birds");
}
ArrayList<Bird> newaviary = new ArrayList<Bird>(10);
for (int i = 0; i<=10; i++) {
int r = rand.nextInt(aviary.size());
Bird b = aviary.get(r);
newaviary.add(b);
aviary.remove(b);
}
for (int i = 0; i<=newaviary.size(); i++) {
Bird d = aviary.get(i);
aviary.add(d);
}
return newaviary;
}
Re: Scanner problem - need help
Going to side with helloworld922 on this, looks like it might be busy, have it shoot some statistics out in the for loops of runExperimentManyTimes example: System.out.println("Running experiment " + i + " of " + w);