Question

In: Computer Science

I/O Lab Java Purpose To practice the input and output concepts discussed in this module. Specifically,...

I/O Lab Java

Purpose

To practice the input and output concepts discussed in this module. Specifically, reading from and writing to local files, and formatting output.

Instructions

Read in file input.csv and generate a cleanly formatted table in output.txt. See the samples below.

input.csv

name,ID,salary,years experience
foo,1,13890,12
bar,2,2342,3
baz,3,99999,24

output.txt

Name | ID | Salary | Years experience
-----+----+--------+-----------------
Foo  | 1  |  13890 | 12
Bar  | 2  |   2342 | 3
Baz  | 3  |  99999 | 24

Solutions

Expert Solution

Hi,

Please find the code below .

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.util.Scanner;
import java.io.RandomAccessFile;
class Main
{

public static void main(String[] args) throws FileNotFoundException
{
Scanner scanner = new Scanner(new File("data.csv"));
// we are using scanner class to read the csv file.
scanner.useDelimiter(",");
FileWriter writer = null;
String tobeWrittenToTextFile="";
while(scanner.hasNext())
{
tobeWrittenToTextFile += scanner.next()+"|";
// Everything read from file is appended to a string.which is used later to output to a text file.
}
System.out.println(tobeWrittenToTextFile);
//Till here we have read the input csv file.
scanner.close();
// 2.Now lets open a file called output.txt and write to it the read string.
try
{
writer = new FileWriter("output.txt");
writer.write(tobeWrittenToTextFile);
writer.close();
}
catch(Exception e)
{
System.out.println(e);
  
}
  
}
}

output and input files attached:(If you dont need line after column headings you can remove that)

output.txt:

Thanks,

kindly upvote.


Related Solutions

JAVA PROGRAMMING Objectives JAVA Practice incremental development. Practice getting input from and printing output for the...
JAVA PROGRAMMING Objectives JAVA Practice incremental development. Practice getting input from and printing output for the user. Write a program with conditional statements. Write a program with loops. Specifications Think of three countries you’d like to travel to and look up the conversion rate between dollars and the money used in those countries. (See http://www.ratesfx.com/ rates/rate-converter.html for conversion rates.) Write a program that will convert money from dollars into each of those three types of currency, or from one of...
Summary In this lab, you add the input and output statements to a partially completed Java...
Summary In this lab, you add the input and output statements to a partially completed Java program. When completed, the user should be able to enter a year, a month, and a day to determine if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31. Instructions Notice that variables have been declared for you. Write the simulated housekeeping() method...
Writing a Modular Program in Java In this lab, you add the input and output statements...
Writing a Modular Program in Java In this lab, you add the input and output statements to a partially completed Java program. When completed, the user should be able to enter a year, a month, and a day to determine if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31. Instructions Notice that variables have been declared for you....
The purpose of this assignment is to test your familiarity with Java I/O statements and if-else statements.
ObjectiveThe purpose of this assignment is to test your familiarity with Java I/O statements andif-else statements. This assignment also tests your understanding of the basics of Javaprogramming and execution, like top-down control flows, translating the logical solutionto a problem into code, and integrating the new concepts you just learned with theolder concepts, including data types and variables, declaration, initialization andassignments, arithmetic operators, etc.Please submit your source code (i.e. your java file) on Canvas under HW1.ProblemIt’s the year 2030 and H&R...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods.(Need Comment, Write by Java Code) Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use and re-use of methods with input validation. Instructions It is quite interesting that most of us are likely to be able to read and comprehend words, even if the alphabets of these words are scrambled (two of them) given the fact that the first and last alphabets remain the same. For example, “I dn'ot gvie a dman for a man taht...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods. Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by n Elements that are rotated...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of two dimensional arrays, input validation, and methods. (Write by Java Code, Need Comment) Instructions A theater seating chart is implemented as a two-dimensional array of ticket prices, like this: Seat Ticket Price 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods. Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by n Elements that are rotated...
Course: Computer Architecture (Theme: Input-Output) A computer consists of a processor and an I/O device D...
Course: Computer Architecture (Theme: Input-Output) A computer consists of a processor and an I/O device D connected to main memory M via a shared bus with a data bus width of one word. The processor can execute a max of 2 MIPS. An average instruction requires 5 machine cycles, 3 of which use the memory bus. A memory read/write operation uses 1 m/c cycle. Suppose that the processor is continuously executing “background” programs that require 96% of the instruction rate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT