Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Scanner problem - need help

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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);
    	}
     
    	}
    Last edited by helloworld922; February 22nd, 2011 at 02:48 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    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
    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);
    	}
     
    	}
    Last edited by helloworld922; February 22nd, 2011 at 02:36 PM.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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()?

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Scanner problem - need help

    Here's the code for see10birds.

    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;
    }

  6. #6
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default 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);

Similar Threads

  1. Basic Java File I/O with Scanner Class Problem
    By miss confused in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 26th, 2010, 08:04 AM
  2. [SOLVED] Problem with Scanner ?
    By derekeverett in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 19th, 2010, 04:34 AM
  3. Help With Scanner
    By jtphenom in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 12th, 2009, 08:49 PM
  4. Simple scanner problem
    By Sinensis in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 20th, 2009, 09:01 PM
  5. Homework problem (scanner problems)
    By TommyFiz in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 20th, 2009, 06:10 PM