Getting an error from save to file : Exception in thread “main” java.util.InputMismatchExceptionHow do I create a Java string from the contents of a file?How to get an enum value from a string value in Java?How do I save a String to a text file using Java?Java Inventory Program problemsCan't execute jar- file: “no main manifest attribute”How to check if current thread is not main threadwhy spill failure happens for Custom Data Type in HadoopMethod override returns nullStringBuilder() vs StringBuilder(null) vs StringBuilder(“”)Exception in thread “main” java.util.InputMismatchException error
Help in drawing resonance structures in case of polybasic acids
Reorder a matrix, twice
Why does my browser attempt to download pages from http://clhs.lisp.se instead of viewing them normally?
New road bike: alloy dual pivot brakes work poorly
Why was it decided in 1956 to abolish the spelling чорт (devil) in favor of чёрт?
Are fuzzy sets appreciated by OR community?
Iterating over &Vec<T> and Vec<&T>
Do wheelchair-accessible aircraft exist?
Is it acceptable to say that a reviewer's concern is not going to be addressed because then the paper would be too long?
Why is volatility skew/smile for long term options flatter compare to short term options?
Subverting the emotional woman and stoic man trope
I am not a pleasant sight
How can I indicate the first and the last reference number written in a page of the bibliography in the header of the page?
What secular civic space would pioneers build for small frontier towns?
Beyond Futuristic Technology for an Alien Warship?
How to stop the death waves in my city?
Why does C++ have 'Undefined Behaviour' and other languages like C# or Java don't?
Why is a road bike faster than a city bike with the same effort? How much faster it can be?
What is the white pattern on trim wheel for?
Windows 10 deletes lots of tiny files super slowly. Anything that can be done to speed it up?
What does it mean by "my days-of-the-week underwear only go to Thursday" in this context?
Comma Code - Automate the Boring Stuff with Python
Does the app TikTok violate trademark?
Top off gas with old oil, is that bad?
Getting an error from save to file : Exception in thread “main” java.util.InputMismatchException
How do I create a Java string from the contents of a file?How to get an enum value from a string value in Java?How do I save a String to a text file using Java?Java Inventory Program problemsCan't execute jar- file: “no main manifest attribute”How to check if current thread is not main threadwhy spill failure happens for Custom Data Type in HadoopMethod override returns nullStringBuilder() vs StringBuilder(null) vs StringBuilder(“”)Exception in thread “main” java.util.InputMismatchException error
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm getting an error when trying to run this program (editing & deleting content in csv file).
Error:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at project.AnimalManager.loadFromFile(AnimalManager.java:88)
at project.Test.main(Test.java:10)
Main App
package project;
public class Test
public static void main(String[] ages)
//Load file
AnimalManager aMgr = new AnimalManager();
aMgr.loadFromFile("AnimalDetails.txt");
// try
// Animals anim = aMgr.getAnimalById("48331827032019");
// aMgr.deleteAnimal(anim);
// catch (IllegalArgumentException exc)
// System.out.println(exc);
//
System.out.println("Edit Animal:");
boolean edited = aMgr.editAnimal("48331827032019",5,"German","200","Huskies","Huskies","n","n",1000.0,"John"); //By ID
if (edited)
System.out.println("Animal has been edited successfully.");
else
System.out.println("Animal not found (test failed).");
Animal Manager
package project;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class AnimalManager
private final ArrayList<Animals> animalList;
public AnimalManager()
this.animalList = new ArrayList<>();
public boolean addAnimal(Animals a)
if (a == null)
throw new IllegalArgumentException("Animal argument is null");
if(animalList.contains(a))
return false;
animalList.add(a);
return true;
public void deleteAnimal (Animals a)
if (a == null)
throw new IllegalArgumentException("Animal argument is null");
animalList.remove(a);
public Animals getAnimalById(String ID)
for (Animals a : this.animalList)
if (a.getID().equals(ID))
return a;
return null;
public boolean editAnimal(String ID, int age,
String breed, String breedPurity, String motherBreed, String fatherBreed, String medicalHistory, String identification, double price, String owner)
//Load from file
public void loadFromFile(String filename)
try
Scanner sc = new Scanner(new File(filename));
sc.useDelimiter("[,rn]+");
//animal details: id,age,breed,purity of breed,mother breed,father breed,medical history, identification, price, owner
while(sc.hasNext())
String ID = sc.next();
int age = sc.nextInt();
String breed = sc.next();
String breedPurity = sc.next();
String motherBreed = sc.next();
String fatherBreed = sc.next();
String medicalHistory = sc.next();
String identification = sc.next();
double price = sc.nextDouble();
String owner = sc.next();
animalList.add(new Animals(ID, age, breed, breedPurity, motherBreed, fatherBreed, medicalHistory, identification, price, owner ));
sc.close();
catch (IOException e)
System.out.println("Exception thrown. " + e);
public void saveToFile(String filename)
StringBuilder output = new StringBuilder(); // To store all passengers info.
for (Animals p : animalList) // Loop through the passengers to store
// every passenger as the line of strings to add them to the file.
output.append(p.getID()).append(",").append(p.getAge()).append(",").append(p.getBreed()).append(",")
.append(p.getMother()).append(",").append(p.getFather()).append(",").append(p.getMedical()).append(",")
.append(p.getIdenti()).append(",").append(p.getPrice()).append(",").append(p.getOwner()).append("rn");
try (FileWriter fw = new FileWriter(new File(filename)))
fw.write(output.toString());
catch (Exception e)
System.out.println("Passengers cannot be saved: " + e);
public String toString()
// use String if more comfortable with it - StringBuilding faster for concat
// than (immutable) String
StringBuilder strBuilder = new StringBuilder();
for (Animals p : this.animalList)
strBuilder.append(p.toString()).append("n");
return strBuilder.toString();
The idea here is to provide the specific animal ID which you want to edit and write down the new information to replace the old one. I already have the save to file method and I think thats where the issue is coming from.
CSV
0,2,AmercianShorthair,100,AmercianShorthair,AmercianShorthair,y,y,900.0,Ann
3,4,GermanShepherd,100,GermanShepherd,GermanShepherd,no,yes,600.0,Dave
6,3,Poodle,100,Poodle,Poodle,yes,no,300.0,Dianna
456,4,Azawakh,50,Unknown,Azawakh,no,no,300.0,April
25041019042018,1,Vizsla,50,Vizsla,TreeingTennesseeBrindle,no,yes,500.0,Lex
3271,1,Beagle,50,Beagle,Unknown,no,no,200.0,Blanton
48331827032019,33,sheperd,50,50,50,no,yes,300.0,Mike
java
|
show 2 more comments
I'm getting an error when trying to run this program (editing & deleting content in csv file).
Error:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at project.AnimalManager.loadFromFile(AnimalManager.java:88)
at project.Test.main(Test.java:10)
Main App
package project;
public class Test
public static void main(String[] ages)
//Load file
AnimalManager aMgr = new AnimalManager();
aMgr.loadFromFile("AnimalDetails.txt");
// try
// Animals anim = aMgr.getAnimalById("48331827032019");
// aMgr.deleteAnimal(anim);
// catch (IllegalArgumentException exc)
// System.out.println(exc);
//
System.out.println("Edit Animal:");
boolean edited = aMgr.editAnimal("48331827032019",5,"German","200","Huskies","Huskies","n","n",1000.0,"John"); //By ID
if (edited)
System.out.println("Animal has been edited successfully.");
else
System.out.println("Animal not found (test failed).");
Animal Manager
package project;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class AnimalManager
private final ArrayList<Animals> animalList;
public AnimalManager()
this.animalList = new ArrayList<>();
public boolean addAnimal(Animals a)
if (a == null)
throw new IllegalArgumentException("Animal argument is null");
if(animalList.contains(a))
return false;
animalList.add(a);
return true;
public void deleteAnimal (Animals a)
if (a == null)
throw new IllegalArgumentException("Animal argument is null");
animalList.remove(a);
public Animals getAnimalById(String ID)
for (Animals a : this.animalList)
if (a.getID().equals(ID))
return a;
return null;
public boolean editAnimal(String ID, int age,
String breed, String breedPurity, String motherBreed, String fatherBreed, String medicalHistory, String identification, double price, String owner)
//Load from file
public void loadFromFile(String filename)
try
Scanner sc = new Scanner(new File(filename));
sc.useDelimiter("[,rn]+");
//animal details: id,age,breed,purity of breed,mother breed,father breed,medical history, identification, price, owner
while(sc.hasNext())
String ID = sc.next();
int age = sc.nextInt();
String breed = sc.next();
String breedPurity = sc.next();
String motherBreed = sc.next();
String fatherBreed = sc.next();
String medicalHistory = sc.next();
String identification = sc.next();
double price = sc.nextDouble();
String owner = sc.next();
animalList.add(new Animals(ID, age, breed, breedPurity, motherBreed, fatherBreed, medicalHistory, identification, price, owner ));
sc.close();
catch (IOException e)
System.out.println("Exception thrown. " + e);
public void saveToFile(String filename)
StringBuilder output = new StringBuilder(); // To store all passengers info.
for (Animals p : animalList) // Loop through the passengers to store
// every passenger as the line of strings to add them to the file.
output.append(p.getID()).append(",").append(p.getAge()).append(",").append(p.getBreed()).append(",")
.append(p.getMother()).append(",").append(p.getFather()).append(",").append(p.getMedical()).append(",")
.append(p.getIdenti()).append(",").append(p.getPrice()).append(",").append(p.getOwner()).append("rn");
try (FileWriter fw = new FileWriter(new File(filename)))
fw.write(output.toString());
catch (Exception e)
System.out.println("Passengers cannot be saved: " + e);
public String toString()
// use String if more comfortable with it - StringBuilding faster for concat
// than (immutable) String
StringBuilder strBuilder = new StringBuilder();
for (Animals p : this.animalList)
strBuilder.append(p.toString()).append("n");
return strBuilder.toString();
The idea here is to provide the specific animal ID which you want to edit and write down the new information to replace the old one. I already have the save to file method and I think thats where the issue is coming from.
CSV
0,2,AmercianShorthair,100,AmercianShorthair,AmercianShorthair,y,y,900.0,Ann
3,4,GermanShepherd,100,GermanShepherd,GermanShepherd,no,yes,600.0,Dave
6,3,Poodle,100,Poodle,Poodle,yes,no,300.0,Dianna
456,4,Azawakh,50,Unknown,Azawakh,no,no,300.0,April
25041019042018,1,Vizsla,50,Vizsla,TreeingTennesseeBrindle,no,yes,500.0,Lex
3271,1,Beagle,50,Beagle,Unknown,no,no,200.0,Blanton
48331827032019,33,sheperd,50,50,50,no,yes,300.0,Mike
java
where is line 88 in Animal
– JustAFellowCoder
Mar 28 at 18:58
double price = sc.nextDouble();
– rwd
Mar 28 at 19:00
You get this exception when the input data of your application doesn't meet its specification. You need to take a look at your input data and fix it.
– MikeJRamsey56
Mar 28 at 19:04
instead of double price = sc.nextDouble(), do String price = sc.next(); and print it immedialty after. So you can see what it is.
– JustAFellowCoder
Mar 28 at 19:06
changing the price type to String?
– rwd
Mar 28 at 19:06
|
show 2 more comments
I'm getting an error when trying to run this program (editing & deleting content in csv file).
Error:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at project.AnimalManager.loadFromFile(AnimalManager.java:88)
at project.Test.main(Test.java:10)
Main App
package project;
public class Test
public static void main(String[] ages)
//Load file
AnimalManager aMgr = new AnimalManager();
aMgr.loadFromFile("AnimalDetails.txt");
// try
// Animals anim = aMgr.getAnimalById("48331827032019");
// aMgr.deleteAnimal(anim);
// catch (IllegalArgumentException exc)
// System.out.println(exc);
//
System.out.println("Edit Animal:");
boolean edited = aMgr.editAnimal("48331827032019",5,"German","200","Huskies","Huskies","n","n",1000.0,"John"); //By ID
if (edited)
System.out.println("Animal has been edited successfully.");
else
System.out.println("Animal not found (test failed).");
Animal Manager
package project;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class AnimalManager
private final ArrayList<Animals> animalList;
public AnimalManager()
this.animalList = new ArrayList<>();
public boolean addAnimal(Animals a)
if (a == null)
throw new IllegalArgumentException("Animal argument is null");
if(animalList.contains(a))
return false;
animalList.add(a);
return true;
public void deleteAnimal (Animals a)
if (a == null)
throw new IllegalArgumentException("Animal argument is null");
animalList.remove(a);
public Animals getAnimalById(String ID)
for (Animals a : this.animalList)
if (a.getID().equals(ID))
return a;
return null;
public boolean editAnimal(String ID, int age,
String breed, String breedPurity, String motherBreed, String fatherBreed, String medicalHistory, String identification, double price, String owner)
//Load from file
public void loadFromFile(String filename)
try
Scanner sc = new Scanner(new File(filename));
sc.useDelimiter("[,rn]+");
//animal details: id,age,breed,purity of breed,mother breed,father breed,medical history, identification, price, owner
while(sc.hasNext())
String ID = sc.next();
int age = sc.nextInt();
String breed = sc.next();
String breedPurity = sc.next();
String motherBreed = sc.next();
String fatherBreed = sc.next();
String medicalHistory = sc.next();
String identification = sc.next();
double price = sc.nextDouble();
String owner = sc.next();
animalList.add(new Animals(ID, age, breed, breedPurity, motherBreed, fatherBreed, medicalHistory, identification, price, owner ));
sc.close();
catch (IOException e)
System.out.println("Exception thrown. " + e);
public void saveToFile(String filename)
StringBuilder output = new StringBuilder(); // To store all passengers info.
for (Animals p : animalList) // Loop through the passengers to store
// every passenger as the line of strings to add them to the file.
output.append(p.getID()).append(",").append(p.getAge()).append(",").append(p.getBreed()).append(",")
.append(p.getMother()).append(",").append(p.getFather()).append(",").append(p.getMedical()).append(",")
.append(p.getIdenti()).append(",").append(p.getPrice()).append(",").append(p.getOwner()).append("rn");
try (FileWriter fw = new FileWriter(new File(filename)))
fw.write(output.toString());
catch (Exception e)
System.out.println("Passengers cannot be saved: " + e);
public String toString()
// use String if more comfortable with it - StringBuilding faster for concat
// than (immutable) String
StringBuilder strBuilder = new StringBuilder();
for (Animals p : this.animalList)
strBuilder.append(p.toString()).append("n");
return strBuilder.toString();
The idea here is to provide the specific animal ID which you want to edit and write down the new information to replace the old one. I already have the save to file method and I think thats where the issue is coming from.
CSV
0,2,AmercianShorthair,100,AmercianShorthair,AmercianShorthair,y,y,900.0,Ann
3,4,GermanShepherd,100,GermanShepherd,GermanShepherd,no,yes,600.0,Dave
6,3,Poodle,100,Poodle,Poodle,yes,no,300.0,Dianna
456,4,Azawakh,50,Unknown,Azawakh,no,no,300.0,April
25041019042018,1,Vizsla,50,Vizsla,TreeingTennesseeBrindle,no,yes,500.0,Lex
3271,1,Beagle,50,Beagle,Unknown,no,no,200.0,Blanton
48331827032019,33,sheperd,50,50,50,no,yes,300.0,Mike
java
I'm getting an error when trying to run this program (editing & deleting content in csv file).
Error:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at project.AnimalManager.loadFromFile(AnimalManager.java:88)
at project.Test.main(Test.java:10)
Main App
package project;
public class Test
public static void main(String[] ages)
//Load file
AnimalManager aMgr = new AnimalManager();
aMgr.loadFromFile("AnimalDetails.txt");
// try
// Animals anim = aMgr.getAnimalById("48331827032019");
// aMgr.deleteAnimal(anim);
// catch (IllegalArgumentException exc)
// System.out.println(exc);
//
System.out.println("Edit Animal:");
boolean edited = aMgr.editAnimal("48331827032019",5,"German","200","Huskies","Huskies","n","n",1000.0,"John"); //By ID
if (edited)
System.out.println("Animal has been edited successfully.");
else
System.out.println("Animal not found (test failed).");
Animal Manager
package project;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class AnimalManager
private final ArrayList<Animals> animalList;
public AnimalManager()
this.animalList = new ArrayList<>();
public boolean addAnimal(Animals a)
if (a == null)
throw new IllegalArgumentException("Animal argument is null");
if(animalList.contains(a))
return false;
animalList.add(a);
return true;
public void deleteAnimal (Animals a)
if (a == null)
throw new IllegalArgumentException("Animal argument is null");
animalList.remove(a);
public Animals getAnimalById(String ID)
for (Animals a : this.animalList)
if (a.getID().equals(ID))
return a;
return null;
public boolean editAnimal(String ID, int age,
String breed, String breedPurity, String motherBreed, String fatherBreed, String medicalHistory, String identification, double price, String owner)
//Load from file
public void loadFromFile(String filename)
try
Scanner sc = new Scanner(new File(filename));
sc.useDelimiter("[,rn]+");
//animal details: id,age,breed,purity of breed,mother breed,father breed,medical history, identification, price, owner
while(sc.hasNext())
String ID = sc.next();
int age = sc.nextInt();
String breed = sc.next();
String breedPurity = sc.next();
String motherBreed = sc.next();
String fatherBreed = sc.next();
String medicalHistory = sc.next();
String identification = sc.next();
double price = sc.nextDouble();
String owner = sc.next();
animalList.add(new Animals(ID, age, breed, breedPurity, motherBreed, fatherBreed, medicalHistory, identification, price, owner ));
sc.close();
catch (IOException e)
System.out.println("Exception thrown. " + e);
public void saveToFile(String filename)
StringBuilder output = new StringBuilder(); // To store all passengers info.
for (Animals p : animalList) // Loop through the passengers to store
// every passenger as the line of strings to add them to the file.
output.append(p.getID()).append(",").append(p.getAge()).append(",").append(p.getBreed()).append(",")
.append(p.getMother()).append(",").append(p.getFather()).append(",").append(p.getMedical()).append(",")
.append(p.getIdenti()).append(",").append(p.getPrice()).append(",").append(p.getOwner()).append("rn");
try (FileWriter fw = new FileWriter(new File(filename)))
fw.write(output.toString());
catch (Exception e)
System.out.println("Passengers cannot be saved: " + e);
public String toString()
// use String if more comfortable with it - StringBuilding faster for concat
// than (immutable) String
StringBuilder strBuilder = new StringBuilder();
for (Animals p : this.animalList)
strBuilder.append(p.toString()).append("n");
return strBuilder.toString();
The idea here is to provide the specific animal ID which you want to edit and write down the new information to replace the old one. I already have the save to file method and I think thats where the issue is coming from.
CSV
0,2,AmercianShorthair,100,AmercianShorthair,AmercianShorthair,y,y,900.0,Ann
3,4,GermanShepherd,100,GermanShepherd,GermanShepherd,no,yes,600.0,Dave
6,3,Poodle,100,Poodle,Poodle,yes,no,300.0,Dianna
456,4,Azawakh,50,Unknown,Azawakh,no,no,300.0,April
25041019042018,1,Vizsla,50,Vizsla,TreeingTennesseeBrindle,no,yes,500.0,Lex
3271,1,Beagle,50,Beagle,Unknown,no,no,200.0,Blanton
48331827032019,33,sheperd,50,50,50,no,yes,300.0,Mike
java
java
asked Mar 28 at 18:41
rwdrwd
826 bronze badges
826 bronze badges
where is line 88 in Animal
– JustAFellowCoder
Mar 28 at 18:58
double price = sc.nextDouble();
– rwd
Mar 28 at 19:00
You get this exception when the input data of your application doesn't meet its specification. You need to take a look at your input data and fix it.
– MikeJRamsey56
Mar 28 at 19:04
instead of double price = sc.nextDouble(), do String price = sc.next(); and print it immedialty after. So you can see what it is.
– JustAFellowCoder
Mar 28 at 19:06
changing the price type to String?
– rwd
Mar 28 at 19:06
|
show 2 more comments
where is line 88 in Animal
– JustAFellowCoder
Mar 28 at 18:58
double price = sc.nextDouble();
– rwd
Mar 28 at 19:00
You get this exception when the input data of your application doesn't meet its specification. You need to take a look at your input data and fix it.
– MikeJRamsey56
Mar 28 at 19:04
instead of double price = sc.nextDouble(), do String price = sc.next(); and print it immedialty after. So you can see what it is.
– JustAFellowCoder
Mar 28 at 19:06
changing the price type to String?
– rwd
Mar 28 at 19:06
where is line 88 in Animal
– JustAFellowCoder
Mar 28 at 18:58
where is line 88 in Animal
– JustAFellowCoder
Mar 28 at 18:58
double price = sc.nextDouble();
– rwd
Mar 28 at 19:00
double price = sc.nextDouble();
– rwd
Mar 28 at 19:00
You get this exception when the input data of your application doesn't meet its specification. You need to take a look at your input data and fix it.
– MikeJRamsey56
Mar 28 at 19:04
You get this exception when the input data of your application doesn't meet its specification. You need to take a look at your input data and fix it.
– MikeJRamsey56
Mar 28 at 19:04
instead of double price = sc.nextDouble(), do String price = sc.next(); and print it immedialty after. So you can see what it is.
– JustAFellowCoder
Mar 28 at 19:06
instead of double price = sc.nextDouble(), do String price = sc.next(); and print it immedialty after. So you can see what it is.
– JustAFellowCoder
Mar 28 at 19:06
changing the price type to String?
– rwd
Mar 28 at 19:06
changing the price type to String?
– rwd
Mar 28 at 19:06
|
show 2 more comments
2 Answers
2
active
oldest
votes
The CSV should contain commas at the end like so:
0,2,AmercianShorthair,100,AmercianShorthair,AmercianShorthair,y,y,900.0,Ann,
3,4,GermanShepherd,100,GermanShepherd,GermanShepherd,no,yes,600.0,Dave,
6,3,Poodle,100,Poodle,Poodle,yes,no,300.0,Dianna,
456,4,Azawakh,50,Unknown,Azawakh,no,no,300.0,April,
25041019042018,1,Vizsla,50,Vizsla,TreeingTennesseeBrindle,no,yes,500.0,Lex,
3271,1,Beagle,50,Beagle,Unknown,no,no,200.0,Blanton,
48331827032019,33,sheperd,50,50,50,no,yes,300.0,Mike
Notice the commas at the end of the lines. What is happening, is it is going to the next line immediately and combining the name with the number at the beginning. For example, "Ann3,4" because linebreaks aren't delimiters in this case.
Also in the CSV, you misspelled Shepherd.
Since the delimiter is not new lines, it is the equivalant of this:
0,2,AmercianShorthair,100,AmercianShorthair,AmercianShorthair,y,y,900.0,Ann3,4,GermanShepherd,100,GermanShepherd,GermanShepherd,no,yes,600.0,Dave6,3,Poodle,100,Poodle,Poodle,yes,no,300.0,Dianna456,4,Azawakh,50,Unknown,Azawakh,no,no,300.0,April25041019042018,1,Vizsla,50,Vizsla,TreeingTennesseeBrindle,no,yes,500.0,Lex3271,1,Beagle,50,Beagle,Unknown,no,no,200.0,Blanton48331827032019,33,sheperd,50,50,50,no,yes,300.0,Mike
Notice how it says 900.0, Ann3, 4, etc. it shifts it all by one so you aren't actually reading in 600.0 as the double, you are reading in Dave as the double.
Edit the code like so:
for (Animals p : animalList) // Loop through the passengers to store
// every passenger as the line of strings to add them to the file.
output.append(p.getID()).append(",").append(p.getAge()).append(",").append(p.getBreed()).append(",")
.append(p.getMother()).append(",").append(p.getFather()).append(",").append(p.getMedical()).append(",")
.append(p.getIdenti()).append(",").append(p.getPrice()).append(",").append(p.getOwner()).append(",").append("rn"); //add another append between getOwner and rn
Note this is where you are adding animals to the CSV. Here, for each animal, you now append a comma at the end of each line as well. The ed is between appending getOwner and appending the rn.
why is the comma needed at the end of each line?
– rwd
Mar 28 at 19:09
Because there is not delimiter set for the carriage return or newline. You arent reading it as lines. It is as if it is all in one line.
– JustAFellowCoder
Mar 28 at 19:10
hmm so what should i do here?
– rwd
Mar 28 at 19:12
@renwid put commas at the end of the lines...
– JustAFellowCoder
Mar 28 at 19:14
I deleted the content of the csv file and populate it again with new ones and the program does work. However when I tried to run it again the same error occured
– rwd
Mar 28 at 19:15
|
show 6 more comments
@JustAFellowCoder has told you a solution to your problem but in my opinion using a CVS file for that is not the best idea.
How about using java.util.Properties?
It is very simple and will fix all your problems, you can just do the following:
//Your file path
Path path = Paths.get("Animal.properties");
//Create a new properties and store it in a file
Properties properties = new Properties();
//your id
int id = 1;
//Everything in properties must be saved as Strings
properties.setProperty("Id", String.valueOf(id));
properties.store(Files.newOutputStream(path), null);
//new properties
properties = new Properties();
properties.load(Files.newInputStream(path));
System.out.println("Id: " + properties.getProperty("Id"));
And you will get a "Animal.properties" file which can be easily restored and a console printing "Id: 1".
add a comment
|
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55404764%2fgetting-an-error-from-save-to-file-exception-in-thread-main-java-util-inputm%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The CSV should contain commas at the end like so:
0,2,AmercianShorthair,100,AmercianShorthair,AmercianShorthair,y,y,900.0,Ann,
3,4,GermanShepherd,100,GermanShepherd,GermanShepherd,no,yes,600.0,Dave,
6,3,Poodle,100,Poodle,Poodle,yes,no,300.0,Dianna,
456,4,Azawakh,50,Unknown,Azawakh,no,no,300.0,April,
25041019042018,1,Vizsla,50,Vizsla,TreeingTennesseeBrindle,no,yes,500.0,Lex,
3271,1,Beagle,50,Beagle,Unknown,no,no,200.0,Blanton,
48331827032019,33,sheperd,50,50,50,no,yes,300.0,Mike
Notice the commas at the end of the lines. What is happening, is it is going to the next line immediately and combining the name with the number at the beginning. For example, "Ann3,4" because linebreaks aren't delimiters in this case.
Also in the CSV, you misspelled Shepherd.
Since the delimiter is not new lines, it is the equivalant of this:
0,2,AmercianShorthair,100,AmercianShorthair,AmercianShorthair,y,y,900.0,Ann3,4,GermanShepherd,100,GermanShepherd,GermanShepherd,no,yes,600.0,Dave6,3,Poodle,100,Poodle,Poodle,yes,no,300.0,Dianna456,4,Azawakh,50,Unknown,Azawakh,no,no,300.0,April25041019042018,1,Vizsla,50,Vizsla,TreeingTennesseeBrindle,no,yes,500.0,Lex3271,1,Beagle,50,Beagle,Unknown,no,no,200.0,Blanton48331827032019,33,sheperd,50,50,50,no,yes,300.0,Mike
Notice how it says 900.0, Ann3, 4, etc. it shifts it all by one so you aren't actually reading in 600.0 as the double, you are reading in Dave as the double.
Edit the code like so:
for (Animals p : animalList) // Loop through the passengers to store
// every passenger as the line of strings to add them to the file.
output.append(p.getID()).append(",").append(p.getAge()).append(",").append(p.getBreed()).append(",")
.append(p.getMother()).append(",").append(p.getFather()).append(",").append(p.getMedical()).append(",")
.append(p.getIdenti()).append(",").append(p.getPrice()).append(",").append(p.getOwner()).append(",").append("rn"); //add another append between getOwner and rn
Note this is where you are adding animals to the CSV. Here, for each animal, you now append a comma at the end of each line as well. The ed is between appending getOwner and appending the rn.
why is the comma needed at the end of each line?
– rwd
Mar 28 at 19:09
Because there is not delimiter set for the carriage return or newline. You arent reading it as lines. It is as if it is all in one line.
– JustAFellowCoder
Mar 28 at 19:10
hmm so what should i do here?
– rwd
Mar 28 at 19:12
@renwid put commas at the end of the lines...
– JustAFellowCoder
Mar 28 at 19:14
I deleted the content of the csv file and populate it again with new ones and the program does work. However when I tried to run it again the same error occured
– rwd
Mar 28 at 19:15
|
show 6 more comments
The CSV should contain commas at the end like so:
0,2,AmercianShorthair,100,AmercianShorthair,AmercianShorthair,y,y,900.0,Ann,
3,4,GermanShepherd,100,GermanShepherd,GermanShepherd,no,yes,600.0,Dave,
6,3,Poodle,100,Poodle,Poodle,yes,no,300.0,Dianna,
456,4,Azawakh,50,Unknown,Azawakh,no,no,300.0,April,
25041019042018,1,Vizsla,50,Vizsla,TreeingTennesseeBrindle,no,yes,500.0,Lex,
3271,1,Beagle,50,Beagle,Unknown,no,no,200.0,Blanton,
48331827032019,33,sheperd,50,50,50,no,yes,300.0,Mike
Notice the commas at the end of the lines. What is happening, is it is going to the next line immediately and combining the name with the number at the beginning. For example, "Ann3,4" because linebreaks aren't delimiters in this case.
Also in the CSV, you misspelled Shepherd.
Since the delimiter is not new lines, it is the equivalant of this:
0,2,AmercianShorthair,100,AmercianShorthair,AmercianShorthair,y,y,900.0,Ann3,4,GermanShepherd,100,GermanShepherd,GermanShepherd,no,yes,600.0,Dave6,3,Poodle,100,Poodle,Poodle,yes,no,300.0,Dianna456,4,Azawakh,50,Unknown,Azawakh,no,no,300.0,April25041019042018,1,Vizsla,50,Vizsla,TreeingTennesseeBrindle,no,yes,500.0,Lex3271,1,Beagle,50,Beagle,Unknown,no,no,200.0,Blanton48331827032019,33,sheperd,50,50,50,no,yes,300.0,Mike
Notice how it says 900.0, Ann3, 4, etc. it shifts it all by one so you aren't actually reading in 600.0 as the double, you are reading in Dave as the double.
Edit the code like so:
for (Animals p : animalList) // Loop through the passengers to store
// every passenger as the line of strings to add them to the file.
output.append(p.getID()).append(",").append(p.getAge()).append(",").append(p.getBreed()).append(",")
.append(p.getMother()).append(",").append(p.getFather()).append(",").append(p.getMedical()).append(",")
.append(p.getIdenti()).append(",").append(p.getPrice()).append(",").append(p.getOwner()).append(",").append("rn"); //add another append between getOwner and rn
Note this is where you are adding animals to the CSV. Here, for each animal, you now append a comma at the end of each line as well. The ed is between appending getOwner and appending the rn.
why is the comma needed at the end of each line?
– rwd
Mar 28 at 19:09
Because there is not delimiter set for the carriage return or newline. You arent reading it as lines. It is as if it is all in one line.
– JustAFellowCoder
Mar 28 at 19:10
hmm so what should i do here?
– rwd
Mar 28 at 19:12
@renwid put commas at the end of the lines...
– JustAFellowCoder
Mar 28 at 19:14
I deleted the content of the csv file and populate it again with new ones and the program does work. However when I tried to run it again the same error occured
– rwd
Mar 28 at 19:15
|
show 6 more comments
The CSV should contain commas at the end like so:
0,2,AmercianShorthair,100,AmercianShorthair,AmercianShorthair,y,y,900.0,Ann,
3,4,GermanShepherd,100,GermanShepherd,GermanShepherd,no,yes,600.0,Dave,
6,3,Poodle,100,Poodle,Poodle,yes,no,300.0,Dianna,
456,4,Azawakh,50,Unknown,Azawakh,no,no,300.0,April,
25041019042018,1,Vizsla,50,Vizsla,TreeingTennesseeBrindle,no,yes,500.0,Lex,
3271,1,Beagle,50,Beagle,Unknown,no,no,200.0,Blanton,
48331827032019,33,sheperd,50,50,50,no,yes,300.0,Mike
Notice the commas at the end of the lines. What is happening, is it is going to the next line immediately and combining the name with the number at the beginning. For example, "Ann3,4" because linebreaks aren't delimiters in this case.
Also in the CSV, you misspelled Shepherd.
Since the delimiter is not new lines, it is the equivalant of this:
0,2,AmercianShorthair,100,AmercianShorthair,AmercianShorthair,y,y,900.0,Ann3,4,GermanShepherd,100,GermanShepherd,GermanShepherd,no,yes,600.0,Dave6,3,Poodle,100,Poodle,Poodle,yes,no,300.0,Dianna456,4,Azawakh,50,Unknown,Azawakh,no,no,300.0,April25041019042018,1,Vizsla,50,Vizsla,TreeingTennesseeBrindle,no,yes,500.0,Lex3271,1,Beagle,50,Beagle,Unknown,no,no,200.0,Blanton48331827032019,33,sheperd,50,50,50,no,yes,300.0,Mike
Notice how it says 900.0, Ann3, 4, etc. it shifts it all by one so you aren't actually reading in 600.0 as the double, you are reading in Dave as the double.
Edit the code like so:
for (Animals p : animalList) // Loop through the passengers to store
// every passenger as the line of strings to add them to the file.
output.append(p.getID()).append(",").append(p.getAge()).append(",").append(p.getBreed()).append(",")
.append(p.getMother()).append(",").append(p.getFather()).append(",").append(p.getMedical()).append(",")
.append(p.getIdenti()).append(",").append(p.getPrice()).append(",").append(p.getOwner()).append(",").append("rn"); //add another append between getOwner and rn
Note this is where you are adding animals to the CSV. Here, for each animal, you now append a comma at the end of each line as well. The ed is between appending getOwner and appending the rn.
The CSV should contain commas at the end like so:
0,2,AmercianShorthair,100,AmercianShorthair,AmercianShorthair,y,y,900.0,Ann,
3,4,GermanShepherd,100,GermanShepherd,GermanShepherd,no,yes,600.0,Dave,
6,3,Poodle,100,Poodle,Poodle,yes,no,300.0,Dianna,
456,4,Azawakh,50,Unknown,Azawakh,no,no,300.0,April,
25041019042018,1,Vizsla,50,Vizsla,TreeingTennesseeBrindle,no,yes,500.0,Lex,
3271,1,Beagle,50,Beagle,Unknown,no,no,200.0,Blanton,
48331827032019,33,sheperd,50,50,50,no,yes,300.0,Mike
Notice the commas at the end of the lines. What is happening, is it is going to the next line immediately and combining the name with the number at the beginning. For example, "Ann3,4" because linebreaks aren't delimiters in this case.
Also in the CSV, you misspelled Shepherd.
Since the delimiter is not new lines, it is the equivalant of this:
0,2,AmercianShorthair,100,AmercianShorthair,AmercianShorthair,y,y,900.0,Ann3,4,GermanShepherd,100,GermanShepherd,GermanShepherd,no,yes,600.0,Dave6,3,Poodle,100,Poodle,Poodle,yes,no,300.0,Dianna456,4,Azawakh,50,Unknown,Azawakh,no,no,300.0,April25041019042018,1,Vizsla,50,Vizsla,TreeingTennesseeBrindle,no,yes,500.0,Lex3271,1,Beagle,50,Beagle,Unknown,no,no,200.0,Blanton48331827032019,33,sheperd,50,50,50,no,yes,300.0,Mike
Notice how it says 900.0, Ann3, 4, etc. it shifts it all by one so you aren't actually reading in 600.0 as the double, you are reading in Dave as the double.
Edit the code like so:
for (Animals p : animalList) // Loop through the passengers to store
// every passenger as the line of strings to add them to the file.
output.append(p.getID()).append(",").append(p.getAge()).append(",").append(p.getBreed()).append(",")
.append(p.getMother()).append(",").append(p.getFather()).append(",").append(p.getMedical()).append(",")
.append(p.getIdenti()).append(",").append(p.getPrice()).append(",").append(p.getOwner()).append(",").append("rn"); //add another append between getOwner and rn
Note this is where you are adding animals to the CSV. Here, for each animal, you now append a comma at the end of each line as well. The ed is between appending getOwner and appending the rn.
edited Mar 28 at 19:23
answered Mar 28 at 19:07
JustAFellowCoderJustAFellowCoder
23810 bronze badges
23810 bronze badges
why is the comma needed at the end of each line?
– rwd
Mar 28 at 19:09
Because there is not delimiter set for the carriage return or newline. You arent reading it as lines. It is as if it is all in one line.
– JustAFellowCoder
Mar 28 at 19:10
hmm so what should i do here?
– rwd
Mar 28 at 19:12
@renwid put commas at the end of the lines...
– JustAFellowCoder
Mar 28 at 19:14
I deleted the content of the csv file and populate it again with new ones and the program does work. However when I tried to run it again the same error occured
– rwd
Mar 28 at 19:15
|
show 6 more comments
why is the comma needed at the end of each line?
– rwd
Mar 28 at 19:09
Because there is not delimiter set for the carriage return or newline. You arent reading it as lines. It is as if it is all in one line.
– JustAFellowCoder
Mar 28 at 19:10
hmm so what should i do here?
– rwd
Mar 28 at 19:12
@renwid put commas at the end of the lines...
– JustAFellowCoder
Mar 28 at 19:14
I deleted the content of the csv file and populate it again with new ones and the program does work. However when I tried to run it again the same error occured
– rwd
Mar 28 at 19:15
why is the comma needed at the end of each line?
– rwd
Mar 28 at 19:09
why is the comma needed at the end of each line?
– rwd
Mar 28 at 19:09
Because there is not delimiter set for the carriage return or newline. You arent reading it as lines. It is as if it is all in one line.
– JustAFellowCoder
Mar 28 at 19:10
Because there is not delimiter set for the carriage return or newline. You arent reading it as lines. It is as if it is all in one line.
– JustAFellowCoder
Mar 28 at 19:10
hmm so what should i do here?
– rwd
Mar 28 at 19:12
hmm so what should i do here?
– rwd
Mar 28 at 19:12
@renwid put commas at the end of the lines...
– JustAFellowCoder
Mar 28 at 19:14
@renwid put commas at the end of the lines...
– JustAFellowCoder
Mar 28 at 19:14
I deleted the content of the csv file and populate it again with new ones and the program does work. However when I tried to run it again the same error occured
– rwd
Mar 28 at 19:15
I deleted the content of the csv file and populate it again with new ones and the program does work. However when I tried to run it again the same error occured
– rwd
Mar 28 at 19:15
|
show 6 more comments
@JustAFellowCoder has told you a solution to your problem but in my opinion using a CVS file for that is not the best idea.
How about using java.util.Properties?
It is very simple and will fix all your problems, you can just do the following:
//Your file path
Path path = Paths.get("Animal.properties");
//Create a new properties and store it in a file
Properties properties = new Properties();
//your id
int id = 1;
//Everything in properties must be saved as Strings
properties.setProperty("Id", String.valueOf(id));
properties.store(Files.newOutputStream(path), null);
//new properties
properties = new Properties();
properties.load(Files.newInputStream(path));
System.out.println("Id: " + properties.getProperty("Id"));
And you will get a "Animal.properties" file which can be easily restored and a console printing "Id: 1".
add a comment
|
@JustAFellowCoder has told you a solution to your problem but in my opinion using a CVS file for that is not the best idea.
How about using java.util.Properties?
It is very simple and will fix all your problems, you can just do the following:
//Your file path
Path path = Paths.get("Animal.properties");
//Create a new properties and store it in a file
Properties properties = new Properties();
//your id
int id = 1;
//Everything in properties must be saved as Strings
properties.setProperty("Id", String.valueOf(id));
properties.store(Files.newOutputStream(path), null);
//new properties
properties = new Properties();
properties.load(Files.newInputStream(path));
System.out.println("Id: " + properties.getProperty("Id"));
And you will get a "Animal.properties" file which can be easily restored and a console printing "Id: 1".
add a comment
|
@JustAFellowCoder has told you a solution to your problem but in my opinion using a CVS file for that is not the best idea.
How about using java.util.Properties?
It is very simple and will fix all your problems, you can just do the following:
//Your file path
Path path = Paths.get("Animal.properties");
//Create a new properties and store it in a file
Properties properties = new Properties();
//your id
int id = 1;
//Everything in properties must be saved as Strings
properties.setProperty("Id", String.valueOf(id));
properties.store(Files.newOutputStream(path), null);
//new properties
properties = new Properties();
properties.load(Files.newInputStream(path));
System.out.println("Id: " + properties.getProperty("Id"));
And you will get a "Animal.properties" file which can be easily restored and a console printing "Id: 1".
@JustAFellowCoder has told you a solution to your problem but in my opinion using a CVS file for that is not the best idea.
How about using java.util.Properties?
It is very simple and will fix all your problems, you can just do the following:
//Your file path
Path path = Paths.get("Animal.properties");
//Create a new properties and store it in a file
Properties properties = new Properties();
//your id
int id = 1;
//Everything in properties must be saved as Strings
properties.setProperty("Id", String.valueOf(id));
properties.store(Files.newOutputStream(path), null);
//new properties
properties = new Properties();
properties.load(Files.newInputStream(path));
System.out.println("Id: " + properties.getProperty("Id"));
And you will get a "Animal.properties" file which can be easily restored and a console printing "Id: 1".
answered Mar 28 at 19:25
OughtToPrevailOughtToPrevail
3501 silver badge9 bronze badges
3501 silver badge9 bronze badges
add a comment
|
add a comment
|
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55404764%2fgetting-an-error-from-save-to-file-exception-in-thread-main-java-util-inputm%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
where is line 88 in Animal
– JustAFellowCoder
Mar 28 at 18:58
double price = sc.nextDouble();
– rwd
Mar 28 at 19:00
You get this exception when the input data of your application doesn't meet its specification. You need to take a look at your input data and fix it.
– MikeJRamsey56
Mar 28 at 19:04
instead of double price = sc.nextDouble(), do String price = sc.next(); and print it immedialty after. So you can see what it is.
– JustAFellowCoder
Mar 28 at 19:06
changing the price type to String?
– rwd
Mar 28 at 19:06