i have a .csv file contain some name as below:
William Shakespeare
charlie chaplin
david Copperfield
and i'm supposed to write a java program that will turn the input above into the output below:
WS
cc
dC
but i still couldn't get the output that i want, i wanna ask is there anything wrong with my code below?
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.io.*;
public class tryname {
public static void main(String[] args) {
tryname obj = new tryname();
obj.run();
}
public void run() {
BufferedReader reader = null;
char tempname;
//int point =0;
String Fullname= "", Initials = "" ;
String cvsSplitBy = "\n";
try {
File csvfile = new File ("Sample2.csv");
reader = new BufferedReader(new FileReader(csvfile));
String line = "";
//String s;
while ((line = reader.readLine()) != null) {
// use comma as separator
String[] name = line.split(cvsSplitBy);
Fullname = name[0];
for(int i = 0; i < Fullname.length(); i++){
//Initials = name [i];
tempname= Fullname.charAt(i);
if(tempname == ' ')
{
//Fullname += name.charAt(i);
Initials += Fullname.charAt(i+1);
//point= i+1;
}
}
System.out.println(Initials);
}