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 2 of 2

Thread: Help Please!

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Help Please!

    Hello,
    I was having some problems with my Java code. The program writen below is for a class. The question asks:
    In this exercise, you are asked to rewrite the Programming Example Classify Numbers...rewrite the program to incorporate the following requirements:
    a. Data to the program is input from a file of unspecified length
    b. Save the output of the program to a file
    c. Write the method getNumber so that it reads a number from the input file (opened in the method main), outputs the number to the output file (opened in the method main), and sends the number read to the method main. Print only 10 numbers per line.
    d. Have the program find the sum and average of the numbers
    e. Write the method printResults so that it outputs the final results to the output file (opened in the method main). Other than outputting the appropriate counts, the definition of the method printResults should also output the sum and the average of the numbers

    The program that you are asked to rewrite pretty much take in numbers from the keyboard (20 at a time) and then says how many of the numbers are even, odd, or zero.

    For this program to work, I wrote my own class IntClass so the variables would pass from method to method, but I'm still getting errors, please help! Thanks!

    Code for IntClass:

    public class IntClass{
    private int x;
    public IntClass(){ x= 0;}
    public IntClass(int num) { x = num;}
    public void setNum(int num){ x = num;}
    public int getNum() { return x;}
    public void addToNum(int num) {x = x +num;}
    public void multiplyToNum(int num) { x = x *num;}
    public int compareTo(int num){ return (x-num);}
    public boolean equals (int num) { if (x == num)
    return true;
    else return false;}
    public String toString(){ return (String.valueOf(x));}
    }

    My current code:


    import java.util.*;
    import java.io.*;

    public class Evensoddszeros{

    public static void main (String [] args)throws IOException{

    int evenscount =0;
    int oddscount=0;
    int zeroscount =0;
    int count =0;

    IntClass number = new IntClass (0);
    IntClass zeros = new IntClass (0);
    IntClass odds = new IntClass (0);
    IntClass evens = new IntClass (0);

    int sum=0;
    int average =0;

    Scanner inFile = new Scanner( new FileReader ("F:\\Ch7_Ex11Data.txt"));
    PrintWriter outFile = new PrintWriter("F:\\classifynumbersoutput.txt");

    while(inFile.hasNext()){
    getNumber(number, inFile, outFile);
    classifyNumber(number.getNum(), zeros, odds, evens);
    count ++;
    if (count % 10 == 0)
    outFile.println();
    sum = sum + number.getNum();
    average = sum/count;

    outFile.close();
    inFile.close();

    }
    }

    public static void getNumber(IntClass number, Scanner inFile, PrintWriter outFile ){

    number.setNum(inFile.nextInt());
    outFile.print(number.getNum() + " ");
    }




    public static void printResult (IntClass Number, IntClass evens, IntClass odds, IntClass zeros, int sum, int average){


    outFile.println("There are " + evens + " evens, " + " there are " + odds + " odds, " + "and there are " + zeros + " zeros");

    outFile.println("The sum of the numbers is: " + sum);

    outFile.println("The average of the numbers is: " + average);


    }



    public static void classifyNumber(int number, IntClass zeroscount, IntClass oddscount, IntClass evenscount){

    switch (number %2){

    case 0: IntClass evens.addToNum(1);
    if ( number == 0);
    IntClass zeros.addToNum(1);
    break;
    case 1:
    case -1: IntClass odds.addToNum(1);
    }
    }
    }

  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Help Please!

    I am starting to feel like a broken record.

    #1: This is obviously homework, but you should still say so. (This means that there are limits to what you are allowed to do)

    #2: Highlight your code with: [highlight=Java] Your code here [/highlight]

    #3: Read Be Precise and that site in general

    What errors are you getting, exactly? Do you have any idea why you might get errors there? On the line that you are getting errors, what do you think the line should be doing?

    Also, you should not be wrapping your ints like that, you might want to look into these commands
    someNum += anotherNum; //Adds anotherNum to someNum, and saves it in someNum
    someNum -= anotherNum; //Subtracts anotherNum from someNum, and saves it in someNum
    someNum *= anotherNum; //Same as someNum = someNum*anotherNum; (or multiplies anotherNum and someNum, than saves it in someNum)
    someNum /= anotherNum; //someNum = someNum / anotherNum;
    Last edited by Tjstretch; November 4th, 2011 at 11:10 PM.