Question

Please Explain the following question in detail so that I can understand how to solve it:...

Please Explain the following question in detail so that I can understand how to solve it:

Assume that you want to use both an input file called "myinput.txt" and an output file called "myoutput.txt" which are located in a folder called "mydata" on the g: drive.

In the program below, fill in all the missing details so that you can read a series of values (one by one) from the input file into the variable "num". Continue to read values and process them until you have processed the value 64(not 64 items, but an item whose value is 64). At the end you may print the total number of values read from the file to the output file. To do this you may have to declare additional variables, include statements to control reading from and writing to files, ect...

Program:
import java.util.Scanner;
import java.io.*;
public class Problem2 {
   public static void main(String[] args) throws IOException
   {
       int num;

       do{
       //read a value into num - you must show how you accomplish this.

       }while
      

   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1


/**
* The java program that opens a input file called myinput.txt file and read the
* values from file . Then check if the value read from the file is not 64, then
* write the value to the myoutput.txt file. The process of reading and writing
* continues until the number read from file not 64. The program also counts
* the number of values read from file . Print the number of values read from file
* to console output.
*
* */
//Problem2.java
import java.util.Scanner;
import java.io.*;
public class Problem2 {
   public static void main(String[] args) throws IOException
   {
       int num;
      
       //Set input and output file paths
       //Note : D is drive name, If G drive available, then replace D with G letter
       String inputFileName="D:\\myinput.txt";
       String outputFileName="D:\\myoutput.txt";

       Scanner fileReader=null;
       PrintWriter writer=null;
       int count=0;

       final int SENTINAL=64;
       try
       {
           //Open Scanner object to read input file
           fileReader=new Scanner(new File(inputFileName));
           //Create an instance of PrintWriter object to write to output file
           writer=new PrintWriter(new File(outputFileName));
          
           //read values until num is not 64
           do
           {
               //read data from file
               num=fileReader.nextInt();
               //check if num is not 64 then increment the count by 1
               //write the value of num to output file
               if(num!=SENTINAL)
               {
                   count++;
                   writer.printf("%3d",num);
               }

           }while(num!=SENTINAL);
           //Print the count on console output
           System.out.println("Total numbers read from file :"+count);

       }
       catch(FileNotFoundException exp)
       {
           System.out.println(exp.getMessage());
       }
       finally
       {
           //close input and output objecs
           fileReader.close();
           writer.close();
       }                   
   }//end of main method
}//end of class

Sample Input file :

myinput.txt

25 35 45 55 75 85 64

Output of the program:

Total numbers read from file :6

myoutput.txt

25 35 45 55 75 85

Add a comment
Know the answer?
Add Answer to:
Please Explain the following question in detail so that I can understand how to solve it:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • c <- 100) (d !- e/8)); ut.println("cond-" + cond); if ((30> a + (int)Math.sqrt(b) d-a+b; else...

    c <- 100) (d !- e/8)); ut.println("cond-" + cond); if ((30> a + (int)Math.sqrt(b) d-a+b; else d-b-c System.out.println("d-"+d); 10) (e 10> a/ d)) 2. (10 pts) Assume that you want to use both an input file callled "myinput.txt" and an output file called "myoutput.txt" which are located in a folder called "mydata" on the g: drive. In the program below, fill in all the missing details so that you can read a series of values (one by one) from the...

  • Hello, Could you please input validate this code so that the code prints an error message...

    Hello, Could you please input validate this code so that the code prints an error message if the user enters in floating point numbers or characters or ANYTHING but VALID ints? And then could you please post a picture of the output testing it to make sure it works? * Write a method called evenNumbers that accepts a Scanner * reading input from a file with a series of integers, and * report various statistics about the integers to the...

  • In Python Please! 16.42 Lab 13B: Palindromes with Files Overview This is a demonstration of reading...

    In Python Please! 16.42 Lab 13B: Palindromes with Files Overview This is a demonstration of reading and writing files. Objectives Be able to read from an input file, perform string manipulation on each line of the file, and write to an output file. Provided input file: A single input file named myinput.txt is provided that contains a few lines of text. bob sees over the moon never odd or even statistics dr awkward Provided output file: A single output file...

  • 1. Import file ReadingData.zip into NetBeans. Also, please download the input.txt file and store it into...

    1. Import file ReadingData.zip into NetBeans. Also, please download the input.txt file and store it into an appropriate folder in your drive. a) Go through the codes then run the file and write the output with screenshot (please make sure that you change the location of the file) (20 points) b) Write additional Java code that will show the average of all numbers in input.txt file (10 points) import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.io.*; 1- * @author mdkabir...

  • Create a method based program to find if a number is prime and then print all...

    Create a method based program to find if a number is prime and then print all the prime numbers from 1 through 500 Method Name: isPrime(int num) and returns a boolean Use for loop to capture all the prime numbers (1 through 500) Create a file to add the list of prime numbers Working Files: public class IsPrimeMethod {    public static void main(String[] args)    {       String input;        // To hold keyboard input       String message;      // Message...

  • Lab Description Sort all words by comparing the length of each word. The word with the...

    Lab Description Sort all words by comparing the length of each word. The word with the smallest length would come first. If you have more than one word with the same length, that group would be sorted alphabetically Input: The data file contains a list of words. The first line in the data file is an integer that represents the number of data sets to follow Output: Output the complete list of words in order by length. Sample Data 10...

  • import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {              ...

    import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {               int total = 0;               int num = 0;               File myFile = null;               Scanner inputFile = null;               myFile = new File("inFile.txt");               inputFile = new Scanner(myFile);               while (inputFile.hasNext()) {                      num = inputFile.nextInt();                      total += num;               }               System.out.println("The total value is " + total);        } } /* In the first program, the Scanner may throw an...

  • can some one help me solve this and explain it please Input Format A line indicating...

    can some one help me solve this and explain it please Input Format A line indicating the size of the array the array on the next line Constraints n < 10000 Output Format One line printing the array in order input (given to you) One line printing the array, followed by its maximum value Sample Input 0 5 1 3 5 7 9 Sample Output 0 1 3 5 7 9 9 7 5 3 1 9 Contest ends in...

  • How to solve this problem by using reverse String and Binary search? Read the input one...

    How to solve this problem by using reverse String and Binary search? Read the input one line at a time and output the current line if and only if it is not a suffix of some previous line. For example, if the some line is "0xdeadbeef" and some subsequent line is "beef", then the subsequent line should not be output. import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.TreeSet;...

  • In java. You are provided an abstract class called MyAbstract. You can only modify the writeFile...

    In java. You are provided an abstract class called MyAbstract. You can only modify the writeFile () class. You are provided another class called Finally you are provided the file you must read from in the MyAbstract class. You will need to extend MyAbstract to MyTestClass. You should have the following outputs. The content of myData employee avg salary number of employees File that was created in myFile() Everything I was provided is listed already. l. Servers ary 1 35...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT