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

Thread: Help! Can't make it print right!

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

    Question Help! Can't make it print right!

    Heres my code:

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

    public class Ch7Ex11 {

    public static final int N = 10;
    @SuppressWarnings("unused")
    private static final int getNumber = 0;


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

    int sumNumbers = 0;
    int totalNumbers = 0;


    IntClass oddCounter = new IntClass(), evenCounter = new IntClass(), zeroCounter = new IntClass();

    Scanner input = new Scanner(new FileReader("F:\\Ch7_Ex11Data.txt"));

    PrintWriter output = new PrintWriter("F:\\classifynumbersoutput.txt");

    while (input.hasNext()) {

    int number = getNumber(input, output);
    sumNumbers += number;
    totalNumbers++;

    if (totalNumbers % N == 0) {
    System.out.println();
    output.println();
    }


    classifyNumber(number, zeroCounter, evenCounter, oddCounter);

    }

    printResult(output, totalNumbers, sumNumbers, evenCounter, zeroCounter,
    oddCounter);
    input.close();
    output.close();

    }

    private static int getNumber(Scanner input, PrintWriter output) {
    int anInt = input.nextInt();
    System.out.println(anInt);
    output.println(anInt);

    return anInt;

    }

    private static void classifyNumber(int number, IntClass oddCounter,
    IntClass zeroCounter, IntClass evenCounter) {

    switch (number % 2) {

    case 0:
    evenCounter.addToNum(1);

    if (number == 0)
    zeroCounter.addToNum(1);
    ;
    break;

    case 1:
    case -1:
    oddCounter.addToNum(1);
    }

    }

    private static void printResult(PrintWriter output, int totalNumbers,
    int sumNumbers, IntClass zeroCounter, IntClass oddCounter,
    IntClass evenCounter) {

    System.out.println(" " + totalNumbers + " numbers were read.");
    System.out.println(" " + sumNumbers + " is total sum.");
    System.out.println(" " + sumNumbers / totalNumbers + " is the average.");
    System.out.println("Total numbers of odds: " + oddCounter);
    System.out.println("There are " + zeroCounter + " Zeros.");
    System.out.println("Total evens are: " + evenCounter);

    output.println(" " + totalNumbers + " numbers were read.");
    output.println(" " + sumNumbers + " is total sum.");
    output.println(" " + sumNumbers / totalNumbers + " is the average.");

    output.println("Total numbers of odds: " + oddCounter);
    output.println("There are " + zeroCounter + " Zeros.");
    output.println("Total evens are: " + evenCounter);
    }

    }

    The program accepts numbers from a file of unknown length and then is supposed to print them 10 numbers a line at a time. The program then is supposed to print how many numbers there are, the sum, the average, the total number of odds, the total number of evens, and the total number of zeros. I got almost everything to work, but I am having trouble with the ouput. How do I make it print only 10 numbers per line instead of printing like this:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    1
    2
    3
    ...
    ect. (these are fake inputs by the way)

    Thanks! Your help is greatly appreciated!


  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! Can't make it print right!

    Please use highlight tags.
    [highlight=Java] The code you want to highlight [/highlight]
    As for printing, look at the difference between print and println:

    PrintStream (Java 2 Platform SE 5.0)
    Note: System.out is a PrintStream.

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

    Default Re: Help! Can't make it print right!

    The problem is with this if statement:
    However, I tried to make it print instead of println, but I got errors and the code would not compile...

    [highlight = java]

    if (totalNumbers % N == 0) {
    System.out.println();
    output.println();
    }

    [/highlight]

Similar Threads

  1. make USB Connection with a finger print device
    By Sunil Raghuvanshi in forum Java Theory & Questions
    Replies: 0
    Last Post: January 15th, 2011, 03:24 AM
  2. how can i print the out put !
    By Faha in forum Java IDEs
    Replies: 5
    Last Post: December 30th, 2010, 04:41 AM
  3. How to make this print only the first 200 results?
    By noFear in forum Java Theory & Questions
    Replies: 2
    Last Post: September 3rd, 2010, 03:29 AM
  4. Print out a JFrame
    By ellias2007 in forum AWT / Java Swing
    Replies: 8
    Last Post: June 17th, 2010, 06:15 AM
  5. print space
    By brainTuner in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 1st, 2010, 06:09 PM

Tags for this Thread