Results 1 to 3 of 3
  1. #1
    PVL268 is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default Hi folks, I have same problem with coding!!!

    Hi


    I have problems with output. It's should Print only 10 numbers per line.
    numbers per line should be controlled by a static variable.
    public static int getNumber(Scanner inputfile, PrintWriter output ){ }

    Here is the program:


    package chapt07;

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

    public class CHO7EX11 {

    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("src///CHO7EX11data.txt"));

    PrintWriter output = new PrintWriter("src//CHO7EX11out.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 + " read.");
    System.out.println(" " + sumNumbers + " is total.");
    System.out.println(" " + sumNumbers / totalNumbers + " is 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 + " read.");
    output.println(" " + sumNumbers + " is total.");
    output.println(" " + sumNumbers / totalNumbers + " is average.");

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

    }


    and the input file include (like example) this numbers

    0 0 -2 -3 -5 6 7 8 0 3 0 -23 -8 0 2 9 0 12 67 54


    I don't now how to do it 10 numbers per line.
    The program execute every number on single row.
    Some advise???
    Thank's

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    you should put it in a loop:
    for(int i = 1 ...)
    if(i%10==0) println(...)
    else print()

    please use code tags to make your code more readable.

    edit:
    Err... its ok if your code is short, (like mine)
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    PVL268 is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

Similar Threads

  1. hi folks
    By DevzAbhi in forum Introductions
    Replies: 2
    Last Post: 07-30-2008, 06:04 AM
  2. Hello Folks,
    By jayesh336 in forum Introductions
    Replies: 2
    Last Post: 02-06-2008, 07:35 AM
  3. Help On Coding problem
    By mandrake446 in forum New To Java
    Replies: 3
    Last Post: 12-08-2007, 08:01 AM
  4. Cannot solve the coding problem of my assignment
    By elimmom in forum New To Java
    Replies: 3
    Last Post: 08-13-2007, 12:33 PM
  5. Problem in my coding
    By one198 in forum New To Java
    Replies: 9
    Last Post: 08-09-2007, 11:07 AM

Posting Permissions

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