Getting InputMismatchException when reading an int from a file with Scanner - java

I am working on a program which imports a library from a generated file.
The file generates properly and is found by Scanner. The first line has a single int as written by
pw.println(cdarchive.getNumber());
Elsewhere in the code. This part seems to work fine.
This is the error I'm getting:
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.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at no.hib.dat102.IO.readFile(IO.java:26)
at no.hib.dat102.Menu.start(Menu.java:34)
at no.hib.dat102.CdArchiveClient.main(CdArchiveClient.java:10)
The line it refers to is
int libSize = in.nextInt();
This is my method:
public class IO {
static final String DELIMITER = "#";
public static CdArchiveADT readFile(String filename) {
Scanner in = null;
CdArchiveADT cda = null;
try
{
File f = new File(filename+".txt");
in = new Scanner(f);
System.out.println(f);
in.useDelimiter(DELIMITER);
int libSize = in.nextInt();
System.out.println("libSize" + libSize);
cda = new CdArchive(libSize);
for (int i=0; i<libSize;i++) {
int inId = in.nextInt();
String inTitle= in.next();
String inArtist = in.next();
String inLabel = in.next();
String inGenre = in.next();
int inYear = in.nextInt();
in.nextLine();
cda.addCd(new CD(inId, inArtist, inTitle, inYear, inGenre, inLabel));
System.out.println("Closing Scanner (input)");
in.close();
}
}
catch (FileNotFoundException e){
System.out.println("Config file not found!");
e.printStackTrace();
}
return cda;
}
EDIT:
This is the method that writes to the file:
public static void writeFile(CdArchiveADT cdarchive, String filename) throws IOException {
PrintWriter pw = null;
File file = null;
try {
file = new File(filename +".txt");
// Create the file if it does not already exist
file.createNewFile();
// Writing metadata
pw = new PrintWriter(new FileWriter(file, false));
pw.println(cdarchive.getNumber());
// Writing data, if CdArchive is not empty
if (cdarchive.getCdTable()[0] != null) {
for (int i = 0; i<cdarchive.getNumber(); i++ ) {
CD c = cdarchive.getCdTable()[i];
pw.print(c.getId()); pw.print(DELIMITER);
pw.print(c.getTitle()); pw.print(DELIMITER);
pw.print(c.getArtist()); pw.print(DELIMITER);
pw.print(c.getLabel()); pw.print(DELIMITER);
pw.print(c.getGenre()); pw.print(DELIMITER);
pw.print(c.getYear()); pw.println(DELIMITER);
}
}
}
catch (FileNotFoundException e)
{
System.out.println("File not found!");
e.printStackTrace();
}
finally
{
if ( pw != null )
{
System.out.println("Closing PrintWriter");
pw.close();
}
}
}

I got a working example:
public static void main(String[] args) {
// write
String delimiter = "#";
StringWriter stringWriter = new StringWriter();
PrintWriter pw = new PrintWriter(stringWriter);
pw.println(3);
for (int i = 0; i < 3; i++) {
pw.print("id " + i);
pw.print(delimiter);
pw.print("titel " + i);
pw.print(delimiter);
pw.print("artist " + i);
pw.println(delimiter);
}
String theString = stringWriter.toString();
System.out.println(theString);
try {
pw.close();
stringWriter.close();
} catch (IOException e) {
// ignore in example
}
// read
Scanner in = new Scanner(theString);
in.useDelimiter("\\s*#\\s*|\\s*\n\\s*"); // add new line as delimiter aswell
int libSize = in.nextInt();
for (int i = 0; i < libSize; i++) {
String inId = in.next();
String inTitle = in.next();
String inArtist = in.next();
in.nextLine();
System.out.println("read: " + inId + ", " + inTitle + ", " + inArtist);
}
in.close();
}
The point is to add new line to the used delimiters aswell

try to use
static final String DELIMITER = "\\s*#\\s*";
Otherwise any leading or trailing spaces will cause that error.

Related

Java error: method readFile() in class cannot be applied to given types

im a first year computer science student learning java and im trying to read a csv file line by line and convert each row to an object, then create an array out of these objects with each element separated by a comma ",". Program keeps returning unusual error: method readFile() in class cannot be applied to given types. im not sure what to do.
main class:
import java.io.*;
import java.util.*;
public class FlightOperations
{
static String fileName = "LaxData.csv";
public static void main(String[] args)
{
// Parsing a CSV file into Scanner class constructor
Scanner sc = new Scanner(System.in);
int fileLength = 0;
fileLength = getFileCount(fileName);
int again = 0;
Date[] LAXarray = new Date[fileLength];
LAXarray = readFile(fileName, fileLength);
menu(LAXarray, sc);
do
{
try
{
System.out.println("\n\nRun program? (1)YES (2)NO");
again = sc.nextInt();
if(again == 1)
{
menu(LAXarray, sc);
}
else
{
System.exit(1);
}
}
catch (InputMismatchException exception)
{
System.out.println("\nInvalid input");
sc.next();
}
}
while (again != 1 || again != 2);
sc.close(); // closes the scanner
}
readFile class:
public static Date[] readFile(String fileName)
{
FileInputStream fileStream = null;
InputStreamReader Read;
BuffferedReader bufRead;
/* int fileLength = getFileLength(fileName); */
String line;
Date[] LAXarray = new Date[getFileCOunt(fileLength)];
int LAXIndex = 0;
try
{
fileStream = new FileInputStream(fileName);
Read = new InputStreamReader(fileStream);
bufRead = new BufferedReader(Read);
line = bufRead.readLine();
for(int i = 1; i < fileLength; i++)
{
line = bufRead.readLine();
LAXarray[i] = processLine(line);
}
}
catch(IOException errorDetails)
{
if(fileStream != null)
{
try
{
fileStream.close();
}
catch(IOException ex2)
{
}
}
System.out.println("Error in fileProcessing: " + errorDetails.getMessage());
}
return LAXarray;
}

Why my output didn't appear? But when run, it doesn't have any error

package labweek10;
import java.io.*;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
public class arrayTourlist {
public static void main(String[] args) throws FileNotFoundException, IOException
{
try {
// read data from file
FileReader fr = new FileReader("touristData.txt");
BufferedReader br = new BufferedReader(fr);
// write/display data into file
FileWriter fw = new FileWriter("output.txt");
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
// object declaration
Tourist t[] = new Tourist[20];
int i = 0;
String input = null;
while ((input = br.readLine()) != null) {
// create class tokenizer
StringTokenizer st = new StringTokenizer(input, ";");
String touristName = st.nextToken();
int touristID = Integer.parseInt(st.nextToken());
String countryFrom = st.nextToken();
String countryRegion = st.nextToken();
boolean touristType = Boolean.parseBoolean(st.nextToken());
// create object to store data
t[i] = new Tourist(touristName, touristID, countryFrom, countryRegion, touristType);
t[i].toString();
// Display the information of tourists which are in Europe region
for (int d = 0; d < t.length; d++)
if (t[d].getRegion() == "Europe") {
pw.println("TOURIST DATA");
pw.println("------------");
pw.println("Tourist Name : " + touristName);
pw.println("Tourist ID : " + touristID);
pw.println("Country From : " + countryFrom);
pw.println("Country Region : " + countryRegion);
pw.println("Tourist Type : " + touristType);
}
}
// Count and display the number of individual tourists from China.
int countC = 0;
for (int x = 0; x < t.length; x++)
if (t[x].getType() == false && "China".equals(t[x].getCountry())) {
countC++;
}
pw.println("The number of individual tourist(s) from China is " + countC);
br.close();
pw.close();
}
catch (FileNotFoundException e) {
System.out.println("Problem :" + e.getMessage());
} catch (IOException ioe) {
System.out.println("Problem :" + ioe.getMessage());
} catch (NoSuchElementException nsee) {
}
catch (NullPointerException npe) {
}
}
}

how to copy only a part of .CSV based on first column elements with java

copy part like this(from date to date) I am trying to copy only a part of .CSV file based on the first column (Start Date and Time) data looks like (2019-01-28 10:22:00 AM) but the user have to put it like this (2019/01/28 10:22:00)
this is for windows, java opencsv , this is what I found but dont do what I need exaclty :
like this:
int startLine = get value1 from column csv ;
int endLine = get value2 from column csv;
public static void showLines(String fileName, int startLine, int endLine) throws IOException {
String line = null;
int currentLineNo = 1;
// int startLine = 20056;//40930;
// int currentLineNo = 0;
File currentDirectory = new File(new File(".").getAbsolutePath());
String fromPath = currentDirectory.getCanonicalPath() + "\\Target\\part.csv";
PrintWriter pw = null;
pw = new PrintWriter(new FileOutputStream(fromPath), true);
//pw.close();
BufferedReader in = null;
try {
in = new BufferedReader (new FileReader(fileName));
//read to startLine
while(currentLineNo<startLine) {
if (in.readLine()==null) {
// oops, early end of file
throw new IOException("File too small");
}
currentLineNo++;
}
//read until endLine
while(currentLineNo<=endLine) {
line = in.readLine();
if (line==null) {
// here, we'll forgive a short file
// note finally still cleans up
return;
}
System.out.println(line);
currentLineNo++;
pw.println(line);
}
} catch (IOException ex) {
System.out.println("Problem reading file.\n" + ex.getMessage());
}finally {
try { if (in!=null) in.close();
pw.close();
} catch(IOException ignore) {}
}
}
public static void main(String[] args) throws FileNotFoundException {
int startLine = 17 ;
int endLine = 2222;
File currentDirectory = new File(new File(".").getAbsolutePath());
try {
showLines(currentDirectory.getCanonicalPath() + "\\Sources\\concat.csv", startLine, endLine);
} catch (IOException e) {
e.printStackTrace();
}
// pw.println();
}
Common CSV format uses a comma as a delimiter, with quotations used to escape any column entry that uses them within the data. Assuming that your column one data is consistent with the format you posted, and that I wouldn't have to bother with quotations marks therefor, you could read the columns as:
public static void main(String[] args) {
//This is the path to the file you are writing to
String targetPath = "";
//This is the path to the file you are reading from
String inputFilePath = "";
String line = null;
ArrayList<String> lines = new ArrayList<String>();
boolean add = false;
String startLine = "2019/01/28 10:22:00";
String endLine = "2019/01/28 10:30:00";
String addFlagSplit[] = startLine.replace("/", "-").split(" ");
String addFlag = addFlagSplit[0] + " " + addFlagSplit[1];
String endFlagSplit[] = endLine.replace("/", "-").split(" ");
String endFlag = endFlagSplit[0] + " " + endFlagSplit[1];
try(PrintWriter pw = new PrintWriter(new FileOutputStream(targetPath), true)){
try (BufferedReader input = new BufferedReader(new FileReader(inputFilePath))){
while((line = input.readLine()) != null) {
String date = line.split(",")[0];
if(date.contains(addFlag)) {
add = true;
}else if(date.contains(endFlag)) {
break;
}
if(add) {
lines.add(line);
}
}
}
for(String currentLine : lines) {
pw.append(currentLine + "\n");
}
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
File currentDirectory = new File(new File(".").getAbsolutePath());
String targetPath = currentDirectory.getCanonicalPath() + "\\Target\\part.csv";
String inputFilePath = currentDirectory.getCanonicalPath() + "\\Sources\\concat.csv";
String line = null;
ArrayList<String> lines = new ArrayList<String>();
boolean add = false;
String startLine = "2019/01/28 10:22:00";
String endLine = "2019/04/06 10:30:00";
try(PrintWriter pw = new PrintWriter(new FileOutputStream(targetPath), true)){
try (BufferedReader input = new BufferedReader(new FileReader(inputFilePath))){
while((line = input.readLine()) != null) {
String date = line.split(",")[0];
if(date.contains(startLine)) {
add = true;
}else if(date.contains(endLine)) {
break;
}
if(add) {
lines.add(line);
}
}
}
for(String currentLine : lines) {
pw.append(currentLine + "\n");
}
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
}

Exception in thread "main" java.util.NoSuchElementException - no close()

I have this problem I need to resolve in the next 8 hours (max), I read a lot of posts with similar problems, but they always call to remove close(). I don't have it and my problem still exists.
package Kolokwium;
import java.io.*;
import java.util.Scanner;
public class Group{
int availableseats;
int occupiedseats= 0;
public Group() {
try (
PrintWriter writer = new PrintWriter("C:\\Users\\Galaxis\\Desktop\\lesson_name.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
) {
System.out.println("Lesson name: ");
String lesson_nameu = reader.readLine();
System.out.println("Available seats:");
String seats= reader.readLine();
writer.println(lesson_name + " " + seats);
availableseats= Integer.parseInt(seats);
}
catch (IOException ex) {
ex.printStackTrace();
}
}
public void add_student() {
if (occupiedseats < availableseats) {
try (
PrintWriter writer = new PrintWriter("C:\\Users\\Galaxis\\Desktop\\lesson_name.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
) {
System.out.println("Write student's data: ");
String data = reader.readLine();
occupiedseats += 1;
writer.println(data);
}
catch (IOException ex) {
ex.printStackTrace();
}
} else {
System.out.println("No available seats!");
}
}
public void show_list() {
File path = new File("C:\\Users\\Galaxis\\Desktop\\lesson_name.txt");
String[] list;
list = path.list();
for (int i=0; i < list.length; i++)
System.out.println(list[i]);
}
public static void main(String[] args) {
Group group = new Group();
Scanner in = new Scanner(System.in);
System.out.println("MENU " + "1. Add student. " + "2. Show list. ");
int ichoice = in.nextInt();
if(ichoice == 1) {
group.add_student();
}
else if(ichoice == 2) {
group.show_list();
}
else {System.out.println("Wrong choice!");}
}
}
Eclipse give me this message when it comes to "int ichoice = in.nextInt();"
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Kolokwium.Grupa.main(Grupa.java:76)
public void pokaz_liste() {
File path = new File("C:\\Users\\Galaxis\\Desktop\\nazwa_przedmiotu.txt");
String[] list;
list = path.list();
for (int i=0; i < list.length; i++)
System.out.println(list[i]);
}
You are trying to get a list of files from a File. Javadoc states that, if the file instance does not point to a directory, it will return null.
https://docs.oracle.com/javase/7/docs/api/java/io/File.html#list()
And for God's sake, flush and close your streams!
For your exception: The scanner is exhausted. Try this
public Group() {
try (
PrintWriter writer = new PrintWriter("C:\\Users\\Galaxis\\Desktop\\lesson_name.txt");
) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Lesson name: ");
String lesson_nameu = reader.readLine();
System.out.println("Available seats:");
String seats= reader.readLine();
writer.println(lesson_name + " " + seats);
seats2 = Integer.parseInt(seats);
}
catch (IOException ex) {
ex.printStackTrace();
}
}

Make faster a read from file

I'm going to pass my data from MongoDB to Neo4j.
So, I exported my MongoDB documents in .csv. As you can read here I have a problem with the array uniform.
So I wrote a java program to fix this problem.
Here is the .csv exported from MongoDB (note the different about uniform array):
_id,official_name,common_name,country,started_by.day,started_by.month,started_by.year,championship,stadium.name,stadium.capacity,palmares.first_prize,palmares.second_prize,palmares.third_prize,palmares.fourth_prize,average_age,squad_value,foreigners,uniform
0,yaDIXxLAOV,WWYWLqPcYM,QsVwiNmeGl,7,9,1479,oYKGgstIMv,qskcxizCkd,8560,10,25,9,29,16,58,6,"[""first_colour"",""second_colour"",""third_colour""]"
Here is how it must be to import in Neo4j:
_id,official_name,common_name,country,started_by.day,started_by.month,started_by.year,championship,stadium.name,stadium.capacity,palmares.first_prize,palmares.second_prize,palmares.third_prize,palmares.fourth_prize,average_age,squad_value,foreigners,uniform.0,uniform.1,uniform.2
0,yaDIXxLAOV,WWYWLqPcYM,QsVwiNmeGl,7,9,1479,oYKGgstIMv,qskcxizCkd,8560,10,25,9,29,16,58,6,first_colour,second_colour,third_colour
My code works, but I have to convert 500k line of the .csv file and the program it is too much slow(it's still working after 20 minutes :/):
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
public class ConvertireCSV {
public static void main(String[] args) throws IOException {
FileReader f;
f=new FileReader("output.csv");
BufferedReader b;
b=new BufferedReader(f);
String firstLine= b.readLine();
int uniform = firstLine.indexOf("uniform");
firstLine=firstLine.substring(0, uniform);
firstLine = firstLine + "uniform.0,uniform.1,uniform.2\n";
String line="";
String csv="";
while(true) {
line=b.readLine();
if(line==null)
break;
int u = line.indexOf("\"[");
line=line.substring(0, u);
line=line + "first_colour,second_colour,third_colour \n";
csv=csv+line;
}
File file = new File("outputForNeo4j.csv");
if(file.createNewFile()) {
PrintWriter pw = new PrintWriter(file);
pw.println(firstLine + csv);
System.out.println("New file \"outputForNeo4j.csv\" created.");
pw.flush();
pw.close();
}
}
}
How can I make it faster?
Okay some basic ways to improve your code:
Make sure that your variables got the minimal scope required. If you don't need line outside your loop, don't declare it outside your loop.
Concatenation of simple strings is in general slow. Use a StringBuilder to speed things to there.
Why are you buffering the string anyway? Seems like a waste of memory. Just open the output stream to your target file and write the lines to the new file as you process them.
Examples:
I don't think you need a example on the first point.
For the second things could look like this:
...
StringBuilder csv = new StringBuilder();
while(true) {
...
csv.append(line);
}
...
if(file.createNewFile()) {
...
pw.println(firstLine + csv.toString());
...
}
For the third point the rewriting would be a little more extensive:
public static void main(String[] args) throws IOException {
FileReader f;
f=new FileReader("output.csv");
BufferedReader b;
b=new BufferedReader(f);
String firstLine= b.readLine();
int uniform = firstLine.indexOf("uniform");
firstLine=firstLine.substring(0, uniform);
firstLine = firstLine + "uniform.0,uniform.1,uniform.2\n";
File file = new File("outputForNeo4j.csv");
if(!file.createNewFile()) {
// all work would be for nothing! Bailing out.
return;
}
PrintWriter pw = new PrintWriter(file);
pw.print(firstLine);
while(true) {
String line=b.readLine();
if(line==null)
break;
int u = line.indexOf("\"[");
line=line.substring(0, u);
line=line + "first_colour,second_colour,third_colour \n";
pw.print(line);
}
System.out.println("New file \"outputForNeo4j.csv\" created.");
pw.flush();
pw.close();
b.close()
}
csv=csv+line;
string concatenation is expensive operation. I would suggest using bufferedWriter.
something like this:
FileReader f;
f=new FileReader("output.csv");
BufferedReader b;
BufferedWriter out;
b=new BufferedReader(f);
try{
out = new BufferedWriter(new FileWriter("outputForNeo4j.csv"));
} catch(Exception e){
//cannot create file
}
System.out.println("New file \"outputForNeo4j.csv\" created.");
String firstLine= b.readLine();
int uniform = firstLine.indexOf("uniform");
firstLine=firstLine.substring(0, uniform);
firstLine = firstLine + "uniform.0,uniform.1,uniform.2\n";
String line="";
String csv="";
out.write(firstLine);
while(true) {
line=b.readLine();
if(line==null)
break;
int u = line.indexOf("\"[");
line=line.substring(0, u);
line=line + "first_colour,second_colour,third_colour \n";
out.write(line);
}
out.flush();
}
Results :
test0 : Runs: 241 iterations ,avarage milis = 246
test1 : Runs: 249 iterations ,avarage milis = 118
test2 : Runs: 269 iterations ,avarage milis = 5
test3 : Runs: 241 iterations ,avarage milis = 2
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Random;
public class Tester {
private static final String filePath = "c:\\bigFile.txt";
//private static final String filePath = "c:\\bigfileNewLine.txt";
private static final int numOfMethods = 4;
private static final int numOfIter = 1000;
public Tester() throws NoSuchMethodException {
System.out.println("Tester.Tester");
int[] milisArr = new int [numOfMethods];
int[] actualRun = new int [numOfMethods];
Random rnd = new Random(System.currentTimeMillis());
Long startMs = 0l, endMs = 0l;
Method[] method = new Method[numOfMethods];
for (int i = 0; i < numOfMethods; i++)
method[i] = this.getClass().getMethod("test" + i);
int testCount = 0;
while (testCount++ < numOfIter) {
int testMethod = rnd.nextInt(numOfMethods);
Method m = method[testMethod];
try {
System.gc();
startMs = System.currentTimeMillis();
String retval = (String) m.invoke(null);
endMs = System.currentTimeMillis();
} catch (IllegalAccessException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (InvocationTargetException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
milisArr[testMethod] += (endMs - startMs);
actualRun[testMethod]++;
System.out.println("Test name: " + m.getName() + " testCount=" + testCount + " Of " + numOfIter + " iteration, Total time :" + (endMs - startMs) / 1000.0 + " seconds");
}
System.out.println("Test Summery :");
for (int i = 0; i < numOfMethods; i++)
System.out.println("test" + i + " : Runs: " + actualRun[i] + " iterations ,avarage milis = " + milisArr[i]/numOfIter);
}
public static String test0() throws IOException {
InputStream file = getInputStream();
StringBuffer textBuffer = new StringBuffer();
int c;
while ((c = file.read()) != -1)
textBuffer.append((char) c);
file.close();
return textBuffer.toString();
}
public static String test1() throws IOException {
Reader reader = new FileReader(new File(filePath));
BufferedReader br = new BufferedReader(reader);
String line = br.readLine();
String result = line;
while (line != null) {
line = br.readLine();
if (line == null) {
} else {
result = result + "\n" + line;
}
}
br.close();
reader.close();
return result;
}
public static String test2() throws IOException {
byte[] buf = new byte[1024];
int l;
InputStream is = getInputStream();
StringBuffer tmpBuf = new StringBuffer();
while ((l = is.read(buf)) != -1) {
tmpBuf.append(new String(buf, 0, l));
}
is.close();
return tmpBuf.toString();
}
public static String test3() throws IOException {
File source = new File(filePath);
final DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(source)));
final byte[] buffer = new byte[(int) source.length()];
dis.readFully(buffer);
dis.close();
return new String(buffer, "UTF-8");
}
private static InputStream getInputStream() {
try {
return new FileInputStream(filePath);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
try {
new Tester();
} catch (NoSuchMethodException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}

Categories