Results 1 to 4 of 4

Thread: Java Help

  1. #1
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Cool Java Help

    Write a program called EvenNumbers.java. This program has a countEvenNumbers method that accepts a Scanner parameter reading input form a file (the filename is either numbers.txt or a name accepted from the console) with a series of integers, and reports various statistics about the integers to the console. Report the total number of numbers, the sum of the numbers, the count of even numbers and the percent of even numbers. For example, if the input file contains the following text:

    5 7 2 8 9 10 12 98 7 14 20 22

    Then the method should produce the following console output:

    12 numbers, sum = 214
    8 evens (66.67%)

    -------------------------------------------------------------------------

    This is a question form my lab and this is my attempt. For some reason I can't get the sum or the percent to work correctly. What am I doing wrong?

    -------------------------------------------------------------------------
    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class EvenNumbers {
        public static void main(String[] args)
                throws FileNotFoundException {
    			Scanner input = new Scanner(new File("numbers.txt"));
    			while (input.hasNextLine()) {
    			    String line = input.nextLine();
    			    Scanner lineScan = new Scanner(line);
                 
    				 double sum = 0.0;
    				 int count = 0;
    				 int id = lineScan.nextInt();                  
    			    while (lineScan.hasNext()) {
    			        String word = lineScan.next();
    			        count++;
    			    }
    			    System.out.println(count + " numbers, sum = " + sum);
    				 System.out.println(count + " evens(" + count / count + "%)");
    			}
    	 }
    	 
    	 public int countEvens(int[] nums) {
                int count = 0;
                for (int i = 0; i < nums.length; i++) {
                      if (nums[i] % 2 == 0)
                         count++;
                }
    				return count;
        }
    }
    Last edited by son012189; 10-27-2010 at 09:21 PM.

  2. #2
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    "This program has a countEvenNumbers method that accepts a Scanner parameter reading input form a file"

    I don't see this in your program...
    Sincerely, Joshua Green
    Please REP if I help :)

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I can't understand various parts of that code. You say "int id = lineScan.nextInt();" but you don't actually use id anywhere! Likewise word. You even define a method, countEvens() which you don't use.

    Forgive me if I have misunderstood, but I won't even ask what you intended by those lines and that method. Because I don't think you intended anything.

    This program has a countEvenNumbers method that accepts a Scanner parameter reading input form a file (the filename is either numbers.txt or a name accepted from the console) with a series of integers, and reports various statistics about the integers to the console.

    Start with a plan.

    The program description says that there will be (at least) two methods: main() and countEvenNumbers(). Figure out a recipe, expressed in plain English words that says what you are going to do in each of these methods.

    Then once you know what it is you are trying to do, work one step at a time. If your code doesn't compile and you can't understand the message copy and post the entire message. Try and use System.out.println() to test that each step is doing exactly what you expect and, if it isn't, you will, again, have a specific question to ask.

  4. #4
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Default

    @joshdgreen I know that. That's because I named my method countEvens instead. Thanks

    @pbrockway2 My professor was the one that told me to put "int id = lineScan.nextInt();". I don't know why, he just told me to put that in there. I didn't realize I didn't use my method. Thanks for the advice, I'll try it again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •