Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The attached Text file http://programming.msjc.edu/cppii/labs/labsb/lab09/Number

ID: 3598248 • Letter: T

Question

The attached Text file http://programming.msjc.edu/cppii/labs/labsb/lab09/Numbers.txt contains both whole and floating point numbers. Of course they are in text format and not binary. Your task is to read and parse the numbers and separate them into two text files double.txt and integer.txt. Specifics Open the text file and read its contents one line at a time. Determine if the line read from the file is a double or an integer. You are to place the integers in a vector called iNumbers and the doubles in a vector called dNumbers. The vector iNumbers should hold pointers to the Integer class and dNumbers should hold pointers to the Double class. When you add a value to one of the vectors you should use new and call the constructor that takes a string. For example: iNumbers.push_back(new Integer("12.23")); Once you have the file parsed create an overloaded writeNumbers function. One of these functions should take a vector of pointers to the Integer class, the other should take a vector of pointers to the Double class. These functions should write the contents of the vector to the appropriate text file. When you are finished you should have two files one that holds int values, the other that holds double values. Note: You created a toString function and added to your classes. You should use this function to write the string to the text file. Required You must have your name, ID, Date, Assignment number, and a brief description of what the file is in comments at the top of each page. Failure to do this will result in 10 points being deducted off the top of your grade.

Explanation / Answer

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Vector;

public class NumberSeperators {

static BufferedWriter intOutput = null;

static BufferedWriter doubleOutput = null;

public static boolean isInteger(String value) {

try {

System.out.println(value);

Integer.parseInt(value);

return true;

} catch (NumberFormatException e) {

return false;

}

}

public static void writeNumbers(Integer i) throws IOException {

intOutput.write(i+" ");

}

public static void writeNumbers(Double d) throws IOException {

doubleOutput.write(d+" ");;

}

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

try {

File infile = new File("Numbers.txt");

FileReader fileReader = new FileReader(infile);

BufferedReader bufferedReader = new BufferedReader(fileReader);

  

File intfile = new File("integer.txt");

intOutput = new BufferedWriter(new FileWriter(intfile));

  

File doublefile = new File("double.txt");

doubleOutput = new BufferedWriter(new FileWriter(doublefile));

  

  

String line;

double doubleValue;

int intValue;

Vector<Integer> iNumbers = new Vector<Integer>();

Vector<Double> dNumbers = new Vector<Double>();

while ((line = bufferedReader.readLine()) != null) {

if (isInteger(line)) {

intValue = Integer.parseInt(line);

iNumbers.add(intValue);

writeNumbers(intValue);

} else {

doubleValue = Double.parseDouble(line);

dNumbers.add(doubleValue);

writeNumbers(doubleValue);

}

}

fileReader.close();

} catch (IOException e) {

e.printStackTrace();

}

finally {

if ( intOutput != null ) {

intOutput.close();

}

if(doubleOutput!=null){

doubleOutput.close();

}

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote