Trouble with importing files - java

ZIPCODE, CITY, STATE, LATITUDE, LONGITUDE
ZIPCODE, CITY, STATE, LATITUDE, LONGITUDE
I'm trying to make this able to open a text file with addresses formatted as such, Create a loop that instantiates a new ZipCode object with the five perameters in order, and then adds that object to ArrayList myZips.
I have a feeling that at least my delimiters are wrong.
public void readZipCodeData(String filename){
Scanner inFS = null;
FileInputStream fileByteStream = null;
try{
// open the File and set delimiters
fileByteStream = new FileInputStream(filename);
inFS = new Scanner(fileByteStream);
inFS.useDelimiter(", *");
// continue while there is more data to read
while(inFS.hasNext()) {
// read five data elements
int zipCode = inFS.nextInt();
String city = inFS.next();
String state = inFS.next();
double latitude = inFS.nextDouble();
double longitude = inFS.nextDouble();
ZipCode z1 = new ZipCode(zipCode, city, state, latitude, longitude);
myZips.add(z1);
}
fileByteStream.close();
// Could not find file
}catch(FileNotFoundException error1) {
System.out.println("Failed to read the data file: " + filename);
// error while reading the file
}catch(IOException error2) {
System.out.println("Oops! Error related to: " + filename);
}
}
Everytime I try running it as is it gives me a
java.util.InputMismatchException:
null (in java.util.Scanner) error on the double longitude line. Any ideas?

I'm not familiar with Scanner for input, but rather BufferedReader. I find it simple to use, and I hope this solution works for you:
Charset charset = Charset.forName("US-ASCII");
try (BufferedReader reader = Files.newBufferedReader(filename, charset)) {
String line = null;
while ((line = reader.readLine()) != null) {
// THIS IS THE CODE FOR EVERY LINE
String[] data = line.split(", ");
int zipCode = Integer.parseInt(data[0]);
String city = data[1];
String state = data[2];
double latitude = Double.parseDouble(data[3]);
double longitude = Double.parseDouble(data[4]);
ZipCode z1 = new ZipCode(zipCode, city, state, latitude, longitude);
myZips.add(z1);
}
} catch (IOException x) {
System.err.format("IOException: %s%n", x);
} catch (NumberFormatException x) {
System.err.format("NumberFormatException: %s%n", x);
}
In this example, I read a whole line with BufferedReader.readLine(), and manually parse it using String.split() and Integer.parseInt() / Double.parseDouble(). It's more intuitive, and it works!
See working example here.
Because of the comment below, I guess I cannot suggest the answer above. However, there are two potential problems I see:
You might have not put in a double in the input file. Simple, honest mistake.
Or, by this SO answer, your locale may be set wrong. In some places, they use a , for decimal point instead of . Try switching these around.

Try something like this. Instead of parsing one field at a time, grab the whole line, turn it into an array of String using your delimiter , then parse to int/double. Scanner.nextLine() grabs the whole line for you.
try{
// open the File and set delimiters
fileByteStream = new FileInputStream(filename);
inFS = new Scanner(fileByteStream);
// read five data elements
String[] data = inFS.nextLine().split(", ");
int zipCode = Integer.parseInt(data[0]);
String city = data[1];
String state = data[2];
double latitude = Double.parseDouble(data[3]);
double longitude = Double.parseDouble(data[4]);
ZipCode z1 = new ZipCode(zipCode, city, state, latitude, longitude);
myZips.add(z1);

Related

How can I take a String with a name and two values and separate it into a String containing the name and 2 doubles containing the values?

I've written a class for a program designed to help manage a volleyball team's roster. The roster is contained in a .dat file and the players are written as follows:
Rachael Adams 3.36 1.93
My issue arises when I try to separate this string into the proper data types (the name being a string, then the first and second values being doubles for the stats).
public Roster(String filename) {
players = new ArrayList<Player>();
try {
FileReader fr = new FileReader(filename);
BufferedReader inFile = new BufferedReader (fr);
String line = inFile.readLine();
Scanner scan = new Scanner(line);
while(line != null) {
String firstName = scan.next();
String lastName = scan.next();
double attackStat = scan.nextDouble();
double blockStat = scan.nextDouble();
String name = firstName + " " + lastName;
Player newPlayer = new Player(name, attackStat, blockStat);
players.add(newPlayer);
line = inFile.readLine();
}
scan.close();
inFile.close();
} catch (IOException e) {
System.out.println(e);
}
}
The program throws this exception when a Roster object is created
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
at Roster.<init>(Roster.java:30)
at Assignment08.openRosterFile(Assignment08.java:59)
at Assignment08.main(Assignment08.java:18)
I am newer to Java and still facing a learning curve, so if there is more information needed then please let me know.
If at all possible, I would greatly appreciate an explanation as to what I did wrong rather than just a solution. Thank you very much.
I always find it easier to split the line:
String[] columns = line.split(" (?=\\d)";
String name = columns[0];
double attackStat = Double.parseDouble(columns[1]);
double blockStat = Double.parseDouble(columns[2]);
This works by splitting on a space, but only when the next char is a digit via the look ahead (?=\d).
This automatically caters for any number of words in the name.

How to remove a row from a csv file if that row contains a certain data in java

I have a csv file that basically mimics a database and my goal is to remove a row from that csv if the csv file contains that username input I provide
the current csv file is:
Jack chan,customer,jack#yorku.ca,jack12,3144134414,13 Arboretum,user2
Donald tusk,customer,donald#yorku.ca,donald1,1213141114,14 Arboretum,user3
tom jack,customer,tom11#yahoo.com,tom44,131344122,14 wells st,user34
jack,parking officer,12rfw#hmail.com,jack,12131131134,12ddcscs,peo1
jewel khan,parking officer,jkhan#hotmail.com,jwel12,2131412141,12 wliis str,peo2
shane li,parking officer,shane#gmail.com,shaneli,1343513414,13 mac st,peo33
james chang,parking officer,james15#gmail.com,james12,31452434114,13 chang st,peo77
my objective is to remove the row of say Shane li using his username "shaneli" and not causing any change to other data. but the current code I have is not causing the file's other data to change
the expected output csv file is row with shaneli gets deleted with other rows remaining intact:
Jack chan,customer,jack#yorku.ca,jack12,3144134414,13 Arboretum,user2
Donald tusk,customer,donald#yorku.ca,donald1,1213141114,14 Arboretum,user3
tom jack,customer,tom11#yahoo.com,tom44,131344122,14 wells st,user34
jack,parking officer,12rfw#hmail.com,jack,12131131134,12ddcscs,peo1
jewel khan,parking officer,jkhan#hotmail.com,jwel12,2131412141,12 wliis str,peo2
james chang,parking officer,james15#gmail.com,james12,31452434114,13 chang st,peo77
this is the code java code I have and I need a java solution:
private static String userPath = "/CSVs/database.csv";
public void removeUser(String name,String userType,String email,String userName,String phoneNumber,String address,String password) {
// FIX THIS
String tmpFile = "tmp.csv";
// String target1 = ""; String target2 = ""; String target3 = ""; String target4 = ""; String target5 = "";String target6 = "";String target7 = "";
String target = "";
File oldFile = new File(userPath);
File newFile = new File(tmpFile);
System.out.println(userName);
try {
FileWriter fw = new FileWriter(tmpFile, true);
BufferedWriter bfw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bfw);
x = new Scanner(new File(userPath));
x.useDelimiter("[,\n]");
while (x.hasNext()) {
target = x.next();
if (!target.equals(userName)) {
pw.printf("%s,%s,%s,%s,%s,%s,%s\n", name, userType,email,userName,phoneNumber,address,password);
// pw.println(target + "," + target + "," + target + "," + target + "," + target + "," + target + "," + target);
}
}
x.close();
pw.flush();
pw.close();
oldFile.delete();
File dmp = new File(userPath);
newFile.renameTo(dmp);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
Please advice
Thanks in advance !!
Solution
The way I've come up with is to do the following:
Create a new file
If the username is not equal, add line, otherwise skip it
Just as we've listed out our steps, we can create a function to do each one.
Code
1) Creating a new file
private void createFile(){
try {
File myObj = new File("CSVs/tmpFile.csv");
} catch (IOException e) {
e.printStackTrace();
}
}
We can then create the file which will be stored at the desired file path and stored as tmpFile.csv.
2) If the username are not equal, add line
private void addDataContents(String userNameToDelete){
try{
String userPath = "CSVs/database.csv";
BufferedReader csvReader = new BufferedReader(new FileReader("CSVs/database.csv"));
String row;
FileWriter myWriter = new FileWriter("CSVs/tmpFile.csv");
while (((row = csvReader.readLine()) != null)){
String[] line = row.split(",");
if (!line[3].equals(userNameToDelete)){
myWriter.write(row + "\n");
}
}
myWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
We then read through the contents of database.csv. We read every line one by one and split the line up by commas as it is a CSV file ( Comma Separated Values ). As the username will always be stored in the 3rd index, we can compare the username we wish to delete with the value stored at the index. If they are not the same, we can go ahead and write the line to our new file. If they are the same, our loop will just continue onto the next line.
Final Notes
I hope everything is easy to read and understandable.
You need to delete the whole row containing specific data from a CSV file. The Java code will be rather long if you try to use the high-level language to do this. It is very simple to accomplish the task in SPL, an open-source Java package. You just need one line of code, as shown below:
A
1
>file("tmp.csv").export#c(file("database.csv").import#wc().select(~(4)!=userNameToDelete))
SPL offers JDBC driver to be invoked by Java. Just store the above SPL script as removeUser.splx and invoke it in Java in the same way you call a stored procedure:
…
Class.forName("com.esproc.jdbc.InternalDriver");
con= DriverManager.getConnection("jdbc:esproc:local://");
st = con.prepareCall("call removeUser(?)");
st.setObject(1,"shaneli");
st.execute();
…

Editing existing txt file in java

I have class Account which have username, fullName, password, id and points.
All accounts are saved in a file.I have many accounts in my file, not just one. This is example of one account in text file.
Miljan9602 Rakita Miljan miljan123 1463433398614 0.0
username, full name, password, id and points
Now, for example if i want to change points for my username. First thing i would do is go through all lines in file and compare all usernames, if i find equal username. I would change point's. This is my idea how to do it. Just dont know how to edit it in file.
public void edit(String username, double points)
{
File f = new File("Accounts.txt");
// file doesnt exist, return from method
if(!f.exists())
return;
Scanner sc = null;
try
{
sc = new Scanner(f);
while(sc.hasNextLine())
{
String line = sc.nextLine(); // Take whole line
String split[] = line.split(" "); // Split it so i can check username
if(split[0].equals(username))
{
String change = Double.toString(points); // Make string from double
split[5] = change; // on fifth index are points
/* My question is now how to edit file and to replace my new points
* with old points ?
* Miljan9602 Rakita Miljan miljan123 1463433398614 0.0 <- Need to change this 0.0 with split[4];
*/
}
}
}catch(Exception e)
{
e.printStackTrace();
}finally{
// finally will always close file
sc.close();
}
You could use the Apache's Commons IO library. Everything you'll need, and more, can be found there. Also, here is the GitHub mirror of Commons IO. Worth a look through.
{
File f = new File("Accounts.txt");
FileWriter fw = new FileWriter(f);
// file doesnt exist, return from method
if(!f.exists())
return;
Scanner sc = null;
try
{
sc = new Scanner(f);
while(sc.hasNextLine())
{
String line = sc.nextLine(); // Take whole line
String split[] = line.split(" "); // Split it so i can check username
if(split[0].equals(username))
{
String change = Double.toString(points); // Make string from double
split[5] = change; // on fifth index are points
/* My question is now how to edit file and to replace my new points
* with old points ?
* Miljan9602 Rakita Miljan miljan123 1463433398614 0.0 <- Need to change this 0.0 with split[4];
*/
for(int i = 0; i < spit.length(); i++{
fw.write(split[i] + " ");
}
System.getProperty("line.separator");
}
}
}catch(Exception e)
{
e.printStackTrace();
}finally{
// finally will always close file
sc.close();
fw.close();
}
This should work
As one has to write the entire read text back to the file system, use Files.readAllLines().
Path path = Paths.get(".../Accounts.txt");
Charset charset = StandardCharsets.UTF_8;
List<String> lines = new ArrayList<>();
if (Files.exists()) {
Files.readAllLines(path, charset);
for (int i = 0; i < lines.size(); ++i) {
String split[] = lines.get(i).split(" ");
if (split[0].equals(username)) {
String change = String.valueOf(points);
split[5] = change; // on fifth index are points
StringBuilder sb = new StringBuilder();
for (String value : split) {
if (sb.length() != 0) {
sb.append(' ');
}
sb.append(value);
}
lines.set(i, sb.toString()); // Changes the line.
Files.write(path, lines, charset);
break; // leave loop
}
}
}
More explained
To alter a single line of a text file, one in principle has to load the entire text and after altering the line, safe it entirely.
The reason is that the file can shrink or grow, depending on the line changes.
Even with some twists this is not optimal.
Files.readAllLines is a nice method for that. One might also change the format:
Fixed length records (lines) allow a RandomAccessFile. However a text risks being manually edited so the file gets corrupted, and one also has limited field lengths.
The .properties format allows access with the Properties class. Requirement is a key, and a format key = value. Also the text has some escaping (\).
One could keep Accounts.txt in core, say in a class Accounts, representing all as a Map from user name to Account.
class Account {
public final String userName; // Unmodifiable key
public String password;
...
}
class Accounts {
private Map<String, Account> accountsByUserName = new HashMap<>();
public void loadAccounts() throws IOException { ... }
public void saveAccounts() throws IOException { ... }
public Optional<Account> getAccountByUserName(String userName) { ... }
public void deleteAccountByUserName(String userName) { ... }
public void createAccount(Account account) throws AlreadyExistsException { ... }
}

Pulling values from an CSV file in java

I am having some trouble pulling values from a CSV file for android app that I am working on. The CSV file takes the following format:
Acton Town (District),Acton Town (Piccadilly),2.00
Aldgate (Circle),Aldgate (Metropolitan),4.00
Aldgate East (District),Aldgate East (Hammersmith And City),2.00
I am trying to import it into my java class using the following method:
public ArrayList<Connection> importConnections(){
try
{
//gets the lines.txt file from assets
in = this.getAssets().open("connections.csv");
Scanner scan = new Scanner(in);
TextView lineData = (TextView)findViewById(R.id.displayLine);
String connection = null;
String startingStation = null;
String endingStation = null;
Float duration = 0;
do
{
connection = scan.nextLine();
String delimiter = ",\n";
StringTokenizer tokens = new StringTokenizer(connection, delimiter);
startingStation = tokens.nextToken();
endingStation = tokens.nextToken();
duration = Float.parseFloat(tokens.nextToken());
connections.add(startStation, endStation, duration);
}
while(scan.hasNext());
//for testing purposes
lineData.setText(endingStation);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return connections;
}
If I run this, the app will crash and I can't figure out why. If I set lineData.setText(endingStation) as lineData.setText(connection), it will display the entire row e.g:
Acton Town (District),Acton Town (Piccadilly),2.00
If I comment out the
duration = Float.parseFloat(tokens.nextToken());
it will run the method. Can anyone find any fault with this method?
You could try using OpenCSV http://opencsv.sourceforge.net/
It is pretty simple to use and returns an array of each row.
CSVReader reader = new CSVReader(new FileReader(<filepath>));
String[] temp;
int NUM = #; //number of rows in csv, or insert code to read through file until null.
for(int i = 0; i < NUM; i++)
{
temp = reader.readNext(); //read next line into temp
}
System.out.println(temp[0]); //Acton Town (District)
System.out.println(temp[1]); //Acton Town (Piccadilly)
System.out.println(temp[2]); //2.00 (string)
reader.close();
Like I said, it is easy to use and prevents you from having to parse out the string on your own.

Text file parsing using java, suggestions needed on which one to use

I can successfully read text file using InputFileStream and Scanner classes. It's very easy but I need to do something more complex than that. A little background about my project first.. I have a device with sensors, and I'm using logger that will log every 10sec data from sensors to a text file. Every 10 sec its a new line of data. So what I want is when I read a file is to grab each separate sensor data into an array. For example:
velocity altitude latitude longitude
22 250 46.123245 122.539283
25 252 46.123422 122.534223
So I need to grab altitude data (250, 252) into an array alt[]; and so forth vel[], lat[], long[]...
Then the last line of the text file will different info, just a single line. It will have the date, distance travelled, timeElapsed..
So after doing a little research I came across InputStream, Reader, StreamTokenizer and Scanner class. My question is which one would you recommend for my case? Is it possible to do what I need to do in my case? and will it be able to check what the last line of the file is so it can grab the date, distance and etc.. Thank you!
Reader + String.split()
String line;
String[] values;
BufferedReader reader = new BufferedReader(new FileReader(args[0]));
List<Integer> velocity = new ArrayList<Integer>();
List<Integer> altitude = new ArrayList<Integer>();
List<Float> latitude = new ArrayList<Float>();
List<Float> longitude = new ArrayList<Float>();
while (null != (line = reader.readLine())) {
values = line.split(" ");
if (4 == values.length) {
velocity.add(Integer.parseInt(values[0]));
altitude.add(Integer.parseInt(values[1]));
latitude.add(Float.parseFloat(values[2]));
longitude.add(Float.parseFloat(values[3]));
} else {
break;
}
}
If you need arrays not list:
velocity.toArray();
As far I undestand data lines has 4 items and last line has 3 items (date, distance, elapsed time)
I would use Scanner. Take a look at the examples here. Another option for you to use BufferedReader to read a line and then have parse method to parse that line into the tokens you want.
Also you might find this thread to be useful.
Very quick code base on the link above. The inputs array has your file data tokens.
public static void main(String[] args) {
BufferedReader in=null;
List<Integer> velocityList = new ArrayList<Integer>();
List<Integer> altitudeList = new ArrayList<Integer>();
List<Double> latitudeList = new ArrayList<Double>();
List<Double> longitudeList = new ArrayList<Double>();
try {
File file = new File("D:\\test.txt");
FileReader reader = new FileReader(file);
in = new BufferedReader(reader);
String string;
String [] inputs;
while ((string = in.readLine()) != null) {
inputs = string.split("\\s");
//here is where we copy the data from the file to the data stucture
if(inputs!=null && inputs.length==4){
velocityList.add(Integer.parseInt(inputs[0]));
altitudeList.add(Integer.parseInt(inputs[1]));
latitudeList.add(Double.parseDouble(inputs[2]));
longitudeList.add(Double.parseDouble(inputs[3]));
}
}
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
if(in!=null){
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
//here are the arrays you want!!!
Integer [] velocities = (Integer[]) velocityList.toArray();
Integer [] altitiudes = (Integer[]) altitudeList.toArray();
Double [] longitudes = (Double[]) longitudeList.toArray();
Double [] latitudes = (Double[]) latitudeList.toArray();
}
As your data is relatively simple, BufferedReader and StringTokenizer should do the trick. You'll have to read ahead by one line to detect when there are no more lines left.
Your code could be something like this
BufferedReader reader = new BufferedReader( new FileReader( "your text file" ) );
String line = null;
String previousLine = null;
while ( ( line = reader.readLine() ) != null ) {
if ( previousLine != null ) {
//tokenize and store elements of previousLine
}
previousLine = line;
}
// last line read will be in previousLine at this point so you can process it separately
But how you process the line itself is really up to you, you can use Scanner if you're feeling more comfortable with it.

Categories

Resources