We are suppose to write an application that reads a file with integers in it, an
ID: 3629599 • Letter: W
Question
We are suppose to write an application that reads a file with integers in it, and creates two new files. One containing even integeres and another containing odd integers. I'm having trouble with my while loop though. After the user puts in the file the program doesn't proceed at all. It seems to be stuck in the while loop or something.
I'm trying to use the while loop to obtain the amount of elements that is in the users file with .hasNext. I told it that as long as the file hasNext increase numElements by 1. This way I could use the variable numElements to declare the length of the array. But this isn't working. Can some one please tell me why this is happening and how i can fix it?
//----------------------------------------------code--------------------------------------------------
import java.util.Scanner;
import java.io.*;
class Hw03
{
//-----------------------------------------------------------------------------
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 numElements = 0;
while (inputFile.hasNext())
numElements++;
inputFile.close();
int[] array = new int[numElements];
for (int index = 0; index < numElements; index++)
array[index] = inputFile.nextInt();
inputFile.close();
for (int index = 0; index < numElements; index++)
System.out.println(""+array[index]+"");
PrintWriter outputFile1 = new PrintWriter("fileName.even");
PrintWriter outputFile2 = new PrintWriter("fileName.odd");
int even = 0;
int odd = 0;
for (int index = 0; index < numElements; index++)
{
if (array[index] % 2 == 0)
{
outputFile1.println(""+array[index]+"");
even++;
}
else
{
outputFile2.println(""+array[index]+"");
odd++;
}
}
outputFile1.close();
outputFile2.close();
int number = numElements - 1;
System.out.println("Processed "+number+" integers. "+even+" even "+odd+" odd");
}
}
Explanation / Answer
You weren't reading the nextInt() (or nextString() or whatever type). Here's a more condensed version of your program
import java.util.Scanner;
import java.io.*;
class Hw03
{
//-----------------------------------------------------------------------------
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 outputFile1 = new PrintWriter("fileName.even");
PrintWriter outputFile2 = new PrintWriter("fileName.odd");
int numElement = 0;
int even = 0;
int odd = 0;
while (inputFile.hasNext()){
numElement=inputFile.nextInt();
System.out.println(numElement);
if (numElement%2==0){
outputFile1.print(numElement);
even++;
} else {
outputFile2.print(numElement);
odd++;
}
}
inputFile.close();
outputFile1.close();
outputFile2.close();
outputFile1.close();
outputFile2.close();
System.out.println("Processed "+(even+odd)+" integers. "+even+" even "+odd+" odd");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.