Exercises Java with Examples-
Practice Exercises Java 4,5 5 302940

CSV converter - Practice Exercises Java


Lesson 8:

File management


Exercise 8.30:

CSV converter


Objetive:

The CSV ("Comma Separated Values") is a exchange format used by many spreadsheets and management systems databases. It is a series of comma-separated values enclosed in quotation marks, although there are variants that do not use quotes, or using semicolon as separator. Often the figures not enclosed in quotes. An example file would be:

"John", "López Pérez," "Alicante", 25
"Antonio", "Pérez López", "Madrid", 27

It should create a program that reads a CSV file as above, with four data blocks (the first 3 are text and the last will be numeric), each of which is in a line, and generate a text file each entry contains a line like this:

John
Pérez López
Alicante
25
Anthony
Pérez López
Madrid
27


Source Code:


package ReaderCSV;
import java.util.*;

public class Main
{
	public static void main(String[] args)
	{
		System.out.println("CSV READER");
		System.out.println("--------------------------------------");
		System.out.print("Enter name of file .csv: ");

		String nameFile = new Scanner(System.in).nextLine();
		java.io.InputStreamReader myfile;
		String cadena;
		int position;

		try
		{
			myfile = new java.io.InputStreamReader(nameFile, System.Text.Encoding.Default);
			String line;
			do
			{
				line = myfile.ReadLine();
				if ((line != null) && (!line.equals("")))
				{
					try
					{
						line = line.replace("\"", "");

						position = line.indexOf(';');
						cadena = line.substring(0, position);
						line = tangible.StringHelper.remove(line, 0, position + 1);
						System.out.println(cadena + "\n");

						position = line.indexOf(';');
						cadena = line.substring(0, position);
						line = tangible.StringHelper.remove(line, 0, position + 1);
						System.out.println(cadena + "\n");

						position = line.indexOf(';');
						cadena = line.substring(0, position);
						line = tangible.StringHelper.remove(line, 0, position + 1);
						System.out.println(cadena + "\n");

						cadena = line.substring(0);
						System.out.println(cadena + "\n");
					}
					catch (RuntimeException e)
					{
						System.out.println("Error !!! " + e.getMessage());
					}
				}
			} while (line != null);
		}
		catch (RuntimeException e)
		{
			System.out.println("Error !!!" + e.getMessage());
		}
	}
}
Exercisey 8.30

Share Now:


Google Ads





Privacy Policy:



Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.

Cookies

This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.