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...
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 demonstrates...
Assignment Purpose The purpose of this lab is to write a well-commented java program that demonstrates the use of loops, and generation of random integers. Instructions You are taking some time off from your paint business and currently are on vacation in Bahamas. You decide to write a Java program that generates 10 random numbers between 1 and 20 (all integers). You cannot use arrays (even if you know what they are) to store these numbers. It then picks up...
module 6 discussed the relationship between marginal cost and average cost. Specifically it stated that when...
module 6 discussed the relationship between marginal cost and average cost. Specifically it stated that when marginal cost is above average cost, average cost rises. Describe why this is the case? What happens when marginal cost is below average cost?
IN JAVA!!! In this project, you will use radix.txt as the input file, and output the...
IN JAVA!!! In this project, you will use radix.txt as the input file, and output the integers SORTED USING RADIX SORT. You may assume all your input consists of integers <=9999. Your main program will input the integers and put them into a QUEUE. It will then pass this queue to a method called radixSort which will sort the numbers in the queue, passing the sorted queue back to main. The main program will then call another method to print...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT