Question with odd/even counter and writing to files

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 993 Views - Last Post: 02 October 2011 - 08:20 PM Rate Topic: -----

#1 BlahBlahMan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 02-June 11

Question with odd/even counter and writing to files

Posted 02 October 2011 - 03:24 PM

Hi!
I have a question about counting the odd and even numbers from a file cause i keep getting 0 even and 0 odd when there are numbers in there and also I cant seem to get my program to write to two different files into odd and even, the files keep coming out as a blank, any help is appreciated!
import java.io.*;
import java.util.Scanner;
////////////////////////////////////////////////////////////////////////////
class Hwk03
{
//--------------------------------------------------------------------------
   public static void main ( String [] args ) throws Exception
   {
      Scanner kb = new Scanner(System.in);
      System.out.print("Enter name of file containing integers: ");
      String filename = kb.nextLine();
      File file = new File(filename);
      Scanner inputFile = new Scanner(file);
      int n = 0;
      while(inputFile.hasNextInt())
      {
         inputFile.nextInt();
         n++;
      }
      System.out.println("Processed " + n + " integers.");

      int evenc = 0;
      int oddc = 0;
      while(inputFile.hasNextInt())
      {
         int i = inputFile.nextInt();
         if(i % 2 == 0)
         {
            evenc++;
         }
         else
         {
            oddc++;
         }
      }
      System.out.println(evenc + " even");
      System.out.println(oddc + " odd");

      PrintWriter even = new PrintWriter(filename + ".even");
      PrintWriter odd = new PrintWriter(filename + ".odd");
      while(inputFile.hasNextInt())
      {
         int number = inputFile.nextInt();
         if(number % 2 == 0)
         {
            even.println(number);
         }
         else
         {
            odd.println(number);
         }
      }
      inputFile.close();

   }
//--------------------------------------------------------------------------
} // end class Hwk03
////////////////////////////////////////////////////////////////////////////



This post has been edited by BlahBlahMan: 02 October 2011 - 03:33 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Question with odd/even counter and writing to files

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Question with odd/even counter and writing to files

Posted 02 October 2011 - 03:31 PM

Please be coherent, hasNextXXX should match nextXXX

while(inputFile.hasNextLine())
      {
         inputFile.nextLine();
         n++;
      }


or
while(inputFile.hasNext())
      {
         inputFile.next();
         n++;
      }


but not
while(inputFile.hasNextLine())
      {
         inputFile.next();
         n++;
      }



after you last while
sure that that one will be false

while(inputFile.hasNextInt())
Was This Post Helpful? 0
  • +
  • -

#3 BlahBlahMan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 02-June 11

Re: Question with odd/even counter and writing to files

Posted 02 October 2011 - 03:34 PM

View Postpbl, on 02 October 2011 - 03:31 PM, said:

after you last while
sure that that one will be false

while(inputFile.hasNextInt())


thanks for the help!
but im not sure what you mean by making sure one of them will be false
Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Question with odd/even counter and writing to files

Posted 02 October 2011 - 03:35 PM

how many numbers are on a line?

You are not closing and reopening the input file between your read loops.
Was This Post Helpful? 1
  • +
  • -

#5 BlahBlahMan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 02-June 11

Re: Question with odd/even counter and writing to files

Posted 02 October 2011 - 03:37 PM

View Postn8wxs, on 02 October 2011 - 03:35 PM, said:

how many numbers are on a line?

You are not closing and reopening the input file between your read loops.


the program is supposed to read from a file with 1 number on each line
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Question with odd/even counter and writing to files

Posted 02 October 2011 - 03:44 PM

after you "scanned" your file you will have to close/re-open it or rewind it
Was This Post Helpful? 2
  • +
  • -

#7 BlahBlahMan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 02-June 11

Re: Question with odd/even counter and writing to files

Posted 02 October 2011 - 03:46 PM

View Postpbl, on 02 October 2011 - 03:44 PM, said:

after you "scanned" your file you will have to close/re-open it or rewind it


ok i tried that but the only way i could do it was by creating a new scanner for each time
such as
Scanner inputFile2 = new Scanner(file);

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Question with odd/even counter and writing to files

Posted 02 October 2011 - 03:49 PM

It is an option, not the cleanest one, but an option
However better to

inputFile.close();
inputFile = new Scanner(the file);
Was This Post Helpful? 1
  • +
  • -

#9 BlahBlahMan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 02-June 11

Re: Question with odd/even counter and writing to files

Posted 02 October 2011 - 03:53 PM

so now my code looks like this, however i can't seem to get it to print the even and odd integers into separate files
import java.io.*;
import java.util.Scanner;
////////////////////////////////////////////////////////////////////////////
class Hwk03
{
//--------------------------------------------------------------------------
   public static void main ( String [] args ) throws Exception
   {
      Scanner kb = new Scanner(System.in);
      System.out.print("Enter name of file containing integers: ");
      String filename = kb.nextLine();
      File file = new File(filename);
      Scanner inputFile = new Scanner(file);
      int n = 0;
      while(inputFile.hasInt())
      {
         inputFile.nextInt();
         n++;
      }
      System.out.println("Processed " + n + " integers.");
      inputFile.close();
      inputFile = new Scanner(file)
      int evenc = 0;
      int oddc = 0;
      while(inputFile.hasNextInt())
      {
         int i = inputFile.nextInt();
         if(i % 2 == 0)
         {
            evenc++;
         }
         else
         {
            oddc++;
         }
      }
      System.out.println(evenc + " even");
      System.out.println(oddc + " odd");
      inputFile.close();
      inputFile = new Scanner(file);
      PrintWriter even = new PrintWriter(filename + ".even");
      PrintWriter odd = new PrintWriter(filename + ".odd");
      while(inputFile.hasNextInt())
      {
         int number = inputFile.nextInt();
         if(number % 2 == 0)
         {
            even.println(number);
         }
         else
         {
            odd.println(number);
         }
      }
      inputFile.close();

   }
//--------------------------------------------------------------------------
} // end class Hwk03
////////////////////////////////////////////////////////////////////////////



View Postpbl, on 02 October 2011 - 03:49 PM, said:

It is an option, not the cleanest one, but an option
However better to

inputFile.close();
inputFile = new Scanner(the file);


that looks much cleaner! haha
i just added that into the code
thanks!
now i just gotta try and get the printwriter to work >.<

This post has been edited by BlahBlahMan: 02 October 2011 - 03:52 PM

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Question with odd/even counter and writing to files

Posted 02 October 2011 - 03:54 PM

Kind of risky
How can you assume that scanner.nextInt() and scanner.nextLine() (or next()) will match ?

Why to you need a fisrt past to count the number of lines in the file if you do not use the n++ value anyhow ?
Was This Post Helpful? 0
  • +
  • -

#11 BlahBlahMan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 02-June 11

Re: Question with odd/even counter and writing to files

Posted 02 October 2011 - 04:03 PM

View Postpbl, on 02 October 2011 - 03:54 PM, said:

Kind of risky
How can you assume that scanner.nextInt() and scanner.nextLine() (or next()) will match ?

Why to you need a fisrt past to count the number of lines in the file if you do not use the n++ value anyhow ?


erm im not quite sure about that, but in our assignment we were supposed the have a result saying something similar to
Enter name of file containing integers:
Processed x integers
x even
x odd

and then going on to create a .even and a .odd file containing the odd and even numbers of the file provided

so i forgot to close the .even and .odd file and now it works perfectly fine!
thanks to everyone that helped!
I do have a question though, is it possible to clean up my code and make it easier to read somehow? cause it looks a bit messy
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Question with odd/even counter and writing to files

Posted 02 October 2011 - 06:22 PM

Gheeezzz !!!
How many times will you read your file for nothing ? :) what about ?
class Hwk03
{
//--------------------------------------------------------------------------
   public static void main ( String [] args ) throws Exception
   {
      Scanner kb = new Scanner(System.in);
      System.out.print("Enter name of file containing integers: ");
      String filename = kb.nextLine();
      File file = new File(filename);
      Scanner inputFile = new Scanner(file);
      PrintWriter even = new PrintWriter(filename + ".even");
      PrintWriter odd = new PrintWriter(filename + ".odd");
      int evenc = 0;
      int oddc = 0;

      while(inputFile.hasNextInt())
      {
         int number = inputFile.nextInt();
         if(number % 2 == 0)
         {
            even.println(number);
            evenc++;
         }
         else
         {
            odd.println(number);
            oddc++;
         }
      }
      inputFile.close();
      System.out.println(evenc + " even");
      System.out.println(oddc + " odd");

   }
//--------------------------------------------------------------------------
} // end class Hwk03
////////////////////////////////////////////////////////////////////////////



Was This Post Helpful? 0
  • +
  • -

#13 BlahBlahMan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 02-June 11

Re: Question with odd/even counter and writing to files

Posted 02 October 2011 - 07:02 PM

View Postpbl, on 02 October 2011 - 06:22 PM, said:

Gheeezzz !!!
How many times will you read your file for nothing ? :) what about ?
class Hwk03
{
//--------------------------------------------------------------------------
   public static void main ( String [] args ) throws Exception
   {
      Scanner kb = new Scanner(System.in);
      System.out.print("Enter name of file containing integers: ");
      String filename = kb.nextLine();
      File file = new File(filename);
      Scanner inputFile = new Scanner(file);
      PrintWriter even = new PrintWriter(filename + ".even");
      PrintWriter odd = new PrintWriter(filename + ".odd");
      int evenc = 0;
      int oddc = 0;

      while(inputFile.hasNextInt())
      {
         int number = inputFile.nextInt();
         if(number % 2 == 0)
         {
            even.println(number);
            evenc++;
         }
         else
         {
            odd.println(number);
            oddc++;
         }
      }
      inputFile.close();
      System.out.println(evenc + " even");
      System.out.println(oddc + " odd");

   }
//--------------------------------------------------------------------------
} // end class Hwk03
////////////////////////////////////////////////////////////////////////////





ahahaha yaaa i forgot u can add the writing to file inside the other loop >.<
sooo i got this now!
import java.io.*;
import java.util.Scanner;
////////////////////////////////////////////////////////////////////////////
class Hwk031
{
//--------------------------------------------------------------------------
   public static void main ( String [] args ) throws Exception
   {
      Scanner kb = new Scanner(System.in);
      System.out.print("Enter name of file containing integers: ");
      String filename = kb.nextLine();
      File file = new File(filename);
      Scanner inputFile = new Scanner(file);
      PrintWriter even = new PrintWriter(filename + ".even");
      PrintWriter odd = new PrintWriter(filename + ".odd");
      int n = 0;
      while(inputFile.hasNextInt())
      {
         inputFile.nextInt();
         n++;
      }
      System.out.println("Processed " + n + " integers.");
      inputFile.close();
      inputFile = new Scanner(file);
      int evenc = 0;
      int oddc = 0;
      while(inputFile.hasNextInt())
      {
         int i = inputFile.nextInt();
         if(i % 2 == 0)
         {
            even.println(i);
            evenc++;
         }
         else
         {
            odd.println(i);
            oddc++;
         }
      }
      inputFile.close();

      System.out.println(evenc + " even");
      System.out.println(oddc + " odd");


   }
//--------------------------------------------------------------------------
} // end class Hwk031
////////////////////////////////////////////////////////////////////////////

This post has been edited by BlahBlahMan: 02 October 2011 - 07:11 PM

Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Question with odd/even counter and writing to files

Posted 02 October 2011 - 07:35 PM

You still read the file twice when you only need to read it once as shown in my previous post
Was This Post Helpful? 0
  • +
  • -

#15 BlahBlahMan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 02-June 11

Re: Question with odd/even counter and writing to files

Posted 02 October 2011 - 07:52 PM

View Postpbl, on 02 October 2011 - 07:35 PM, said:

You still read the file twice when you only need to read it once as shown in my previous post

o u mean this
i need it to count the total number of integers

16	      int n = 0;
17	      while(inputFile.hasNextInt())
18	      {
19	         inputFile.nextInt();
20	         n++;
21	      }
22	      System.out.println("Processed " + n + " integers.");
23	      inputFile.close();

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2