(For this I need to use Arrays - not Array lists. Snipping code to make it easier to follow. Renaming variables and constants to label so others who read this later understand what I am doing better.)
At the class level I have my arrays defined like so:
private static final int CONSTANT = 20;
public static String[] arrayName = new String[CONSTANT - 1];
and in the main method I am pointing to the method like so:
readArrays("filenName.txt", arrayName);
and a line like that points to the following complete method which is where the error occurs:
public static void readArrays(String codeFile,
String[] Arrays)
{
try ( Scanner fin = new Scanner ( new File(codeFile) ); )
{
for (int i = 0; i < CONSTANT - 1; i++)
{
Arrays[i] = fin.next();
}
}
catch (FileNotFoundException e)
{
System.err.println( "Read Error: " + e.getMessage() );
}
It shows no errors in NetBeans, but in the console window the exact error message is:
run:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1371)
at mp06.Main.readArrays(Main.java:47)
at mp06.Main.main(Main.java:33)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
What is causing this error? Ultimately, my goal is to write the entire contents of multiple text files to multiple arrays using the same method and without using any other approach.
try ( Scanner fin = new Scanner ( new File(codeFile) ); )
{
for (int i = 0; i < CONSTANT - 1 && fin.hasNext(); i++)
{
Arrays[i] = fin.next();
}
}
catch (FileNotFoundException e)
{
System.err.println( "Read Error: " + e.getMessage() );
}
You also need to check Scanner has next.
In Java8, you can use stream to read file:
List<String> contents = Files.lines(Paths.get("filenName.txt")).collect(Collectors.toList());
The below code might help you.
There are couple of things, you have to check whether next element is exists in scanner before you call next() and the other one is the constant "20", it will end up with Array Index Bounds exception if the size exceeds 20.
Adding them in the list and converting them to an array will resolve this. Try the below code.
List<String> list = new ArrayList<String>();
while (fin.hasNext()) {
list.add(fin.next());
}
String[] array = new String[list.size()];
list.toArray(array);
for (String str: array) {
System.out.println(str);
}
Related
I am supposed to read strings from a text file and sort them (alphabetically).
The text files include string names like Tory Warren, Guy Cowan. I am able to read all the strings (names) from the file. However, they are all unsorted.I have tried to use collections.sort. Unfortunately, it wouldn't work. Can you please help me with that one?
Here is my error trace:
java.lang.Error: Unresolved compilation problems:
The method sortNames(String) in the type ProcessNames is not applicable for the arguments (ArrayList<String>)
And my code:
public ArrayList<String> sortNames(String filename) throws Exception{
File file = new File(filename);
ArrayList<String> names = new ArrayList<String>();
Scanner scan = new Scanner(file);
while (scan.hasNext()) {
names.add(scan.nextLine());
Collections.sort(names);
}
for (int i = 0; i < names.size(); i++) {
System.out.println(names.get(i));
}
scan.close();
Collections.sort(names);
return names;
}
My Test Unit:
void testSortNames() {
Collections.sort(namesMatchFile1);
assertArrayEquals(namesMatchFile1.toArray(), processNames.sortNames(namesReadFile1).toArray());
Collections.sort(namesMatchFile2);
assertArrayEquals(namesMatchFile2.toArray(), processNames.sortNames(namesReadFile2).toArray());
Collections.sort(namesMatchFile3);
assertArrayEquals(namesMatchFile3.toArray(), processNames.sortNames(namesReadFile3).toArray());
Collections.sort(namesMatchFile4);
assertArrayEquals(namesMatchFile4.toArray(), processNames.sortNames(namesReadFile4).toArray());
I am new to Java and literally just started a few weeks ago! So please be kind.
Thank you loads for your help.
Your code is fine. You aren't calling the method correctly.
I made a local file at /tmp/test.txt containing this:
300
200
400
500
100
Then I copied your sortNames() method exactly as you posted it and ran it with the following (where Scratch is the name of the class I used):
try {
new Scratch().sortNames("/tmp/test.txt");
} catch (Exception e) {
e.printStackTrace();
}
Your code correctly sorted and printed the contents of the file:
100
200
300
400
500
So I start with a textfile; this textfile at the start contains a number 3 (amount of woningen) 3 Woningen. I can read them with a Scanner without errors. I can also add a Woning without errors. If a Woning is added, the number on top of the file is incremented with 1.
Problem is as follows:
If a Woning has been added, the file has changed. The number is now 4, and a Woning has been added. However, if I want to read all the Woningen, Java returns an exception on the 4th Woning. So for some reason I can't read the 4th Woning.
What can be the cause of this? Do I have to save the file somehow after changes have been made to the file? Or something else has to be done?
Help is greatly appreciated :)!
EDIT
Note: Woning is the dutch word for House.
Code to read the file:
public static Portefeuille read (String infile) {
try {
Scanner sc = new Scanner (new File(infile));
ArrayList<Woning> wlijst = new ArrayList<Woning>();
Portefeuille p = new Portefeuille();
int woningen = sc.nextInt();
int i = 0;
while (i < woningen) {
sc.nextLine();
String tag = sc.nextLine();
wlijst.add(Woning.read(sc));
wlijst.get(i).setTag(tag);
//System.out.println(wlijst.get(i).getTag());
//System.out.println(wlijst.toString());
p.voegToe(wlijst.get(i));
i++;
}
sc.close();
return p;
}
catch (Exception e) {
System.out.println("Portefeuille: Exception is caught");
Portefeuille p = new Portefeuille();
return p;
}
}
Code to write to the file:
public static void writeToFile (Portefeuille port, int woningen) {
try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("Makelaar.txt", false)))) {
woningen = woningen + 1;
out.println(woningen);
ArrayList<Woning> wlijst = new ArrayList<Woning>();
wlijst = port.woninglijst;
for (int i = 0; i < wlijst.size(); i++) {
if (wlijst.get(i) instanceof KoopWoning) {
KoopWoning kw = (KoopWoning) wlijst.get(i);
KoopWoning.writeToFileK(kw, out);
}
else {
HuurWoning hw = (HuurWoning) wlijst.get(i);
HuurWoning.writeToFileH(hw, out);
}
}
out.close();
}
catch (Exception e) {
System.out.println(e.getMessage());
System.out.println("writeToFile: Exception Caught");
}
}
File looks like this:
3
TE KOOP:
Emmalaan 23
3051JC Rotterdam
7 kamers
koopprijs 300000
energiepeil C
VERKOCHT:
Emmalaan 25
3051JC Rotterdam
5 kamers
vraagprijs 280000
energiepeil A
TE HUUR:
Javastraat 88
4078KB Eindhoven
3 kamers
huurprijs 500
I dont know what woningen is. But as far as I know, after adding the data to file, close the file and then try to read the data in file. This definition is always stuck in my mind that one should close the file after you've the finished your process. I can only guess whether you've done two java class for reading and writing or in a same class of writing and reading methods. Hereafter provide some codes so that all can atleast understand on what you're asking.
I am writing a function to take a text file and count how many lines it has while outputting the lines to an array of strings. Doing this I have several exceptions I need to look out for. The class function has several variables that should have a scope throughout the function but when I write a value to the function inside of an exception, the return statement cannot find it. I've moved the declaration around and nothing helps
The value returned "h5Files" "Might not have been initialized" Since I don't know how long the array will be I cannot initialize it to a certain length. I do this within the code and I need a way to tell the return statement that I now have a values
Here is the code
public String[] ReadScanlist(String fileIn){
int i;
String directory ="c:\\data\\"; // "\" is an illegal character
System.out.println(directory);
int linereader = 0;
String h5Files[];
File fileToRead = new File(directory + fileIn);
System.out.println(fileToRead);
try {
FileInputStream fin = new FileInputStream(fileToRead); // open this file
}
catch(FileNotFoundException exc) {
System.out.println("File Not Found");
}
try{
//read bytes until EOF is detected
do {
FileReader fr = new FileReader(fileToRead);// Need to convert to reader
LineNumberReader lineToRead = new LineNumberReader(fr); // Use line number reader class
//
while (lineToRead.readLine() != null){
linereader++;
}
linereader = 0;
lineToRead.setLineNumber(0); //reset line number
h5Files = new String[linereader];
while (lineToRead.readLine() != null){
h5Files[linereader] = lineToRead.readLine(); // deposit string into array
linereader++;
}
return h5Files;
}
while(i !=-1); // When i = -1 the end of the file has been reached
}
catch(IOException exc) {
System.out.println("Error reading file.");
}
try{
FileInputStream fin = new FileInputStream(fileToRead);
fin.close(); // close the file
}
catch(IOException exc) {
System.out.println("Error Closing File");
}
return h5Files;
}
Your code is very very odd. For example these two blocks make no sense:
try {
FileInputStream fin = new FileInputStream(fileToRead); // open this file
}
catch(FileNotFoundException exc) {
System.out.println("File Not Found");
}
try{
FileInputStream fin = new FileInputStream(fileToRead);
fin.close(); // close the file
}
catch(IOException exc) {
System.out.println("Error Closing File");
}
I don't know what you think they do, but besides the first one leaking memory, they do nothing at all. The comments are more worrying, they suggest that you need to do more reading on IO in Java.
Deleting those blocks and tidying the code a (moving declarations, formatting) gives this:
public String[] ReadScanlist(String fileIn) {
String directory = "c:\\data\\";
String h5Files[];
File fileToRead = new File(directory + fileIn);
try {
int i = 0;
do {
FileReader fr = new FileReader(fileToRead);
LineNumberReader lineToRead = new LineNumberReader(fr);
int linereader = 0;
while (lineToRead.readLine() != null) {
linereader++;
}
linereader = 0;
lineToRead.setLineNumber(0);
h5Files = new String[linereader];
while (lineToRead.readLine() != null) {
h5Files[linereader] = lineToRead.readLine();
linereader++;
}
return h5Files;
} while (i != -1);
} catch (IOException exc) {
System.out.println("Error reading file.");
}
return h5Files;
}
My first bone of contention is the File related code. First, File abstracts from the underlying OS, so using / is absolutely fine. Second, there is a reason File has a File, String constructor, this code should read:
File directory = new File("c:/data");
File fileToRead = new File(directory, fileIn);
But it should really be using the new Path API anyway (see below).
So, you declare h5Files[]. You then proceed to read the whole file to count the lines. You then assign h5Files[] to an array of the correct size. Finally you fill the array.
If you have an error anywhere before you assign h5Files[] you have not initialised it and therefore cannot return it. This is what the compiler is telling you.
I don't know what i does in this code, it is assigned to 0 at the top and then never reassigned. This is an infinite loop.
So, you need to rethink your logic. I would recommend throwing an IOException if you cannot read the file. Never return null - this is an anti-pattern and leads to all those thousands of null checks in your code. If you never return null you will never have to check for it.
May I suggest the following alternative code:
If you are on Java 7:
public String[] ReadScanlist(String fileIn) throws IOException {
final Path root = Paths.get("C:/data");
final List<String> lines = Files.readAllLines(root.resolve(fileIn), StandardCharsets.UTF_8);
return lines.toArray(new String[lines.size()]);
}
Or, if you have Java 8:
public String[] ReadScanlist(String fileIn) throws IOException {
final Path root = Paths.get("C:/data");
try (final Stream<String> lines = Files.lines(root.resolve(fileIn), StandardCharsets.UTF_8)) {
return lines.toArray(String[]::new);
}
}
Since I don't know how long the array will be I cannot initialize it
to a certain length.
I don't think an array is the correct solution for you then - not to say it can't be done, but you would be re-inventing the wheel.
I would suggest you use a LinkedList instead, something like:
LinkedList<String> h5Files = new LinkedList<>();
h5Files.add(lineToRead.readLine());
Alternatively you could re-invent the wheel by setting the array to an arbritary value, say 10, and then re-size it whenever it gets full, something like this:
h5Files = new String[10];
if (linereader = h5Files.size())
{
String[] temp = h5Files;
h5Files = new String[2 * linereader];
for (int i = 0; i < linereader; i++)
{
h5Files[i] = temp[i];
}
}
Either one of these solutions would allow you to initialize the array (or array alternative) in a safe constructor, prior to your try block, such that you can access it if any exceptions are thrown
Here is your problem. Please take a look on digested version of your code with my comments.
String h5Files[]; // here you define the variable. It still is not initialized.
try{
..................
do {
h5Files = new String[linereader]; // here you initialize the variable
} while(i !=-1); // When i = -1 the end of the file has been reached
..................
catch(IOException exc) {
// if you are here the variable is still not initialized
System.out.println("Error reading file.");
}
// you continue reading file even if exception was thrown while opening the file
I think that now the problem is clearer. You try to open the file and count lines. If you succeed you create array. If not (i.e. when exception is thrown) you catch the exception but still continue reading the file. But in this case you array is not initialized.
Now how to fix this?
Actually if you failed to read the file first time you cannot continue. This may happen for example if file does not exist. So, you should either return when first exception is thrown or just do not catch it at all. Indeed there is nothing to do with the file if exception was thrown at any phase. Exception is not return code. This is the reason that exceptions exist.
So, just do not catch exceptions at all. Declare your method as throws IOException and remove all try/catch blocks.
I've got some Java code that runs quite the expected way, but it's taking some amount of time -some seconds- even if the job is just looping through an array.
The input file is a Fasta file as shown in the image below. The file I'm using is 2.9Mo, and there are some other Fasta file that can take up to 20Mo.
And in the code im trying to loop through it by bunches of threes, e.g: AGC TTT TCA ... etc The code has no functional sens for now but what I want is to append each Amino Acid to it's equivalent bunch of Bases. Example :
AGC - Ser / CUG Leu / ... etc
So what's wrong with the code ? and Is there any way to do it better ? Any optimization ? Looping through the whole String is taking some time, maybe just seconds, but need to find a better way to do it.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class fasta {
public static void main(String[] args) throws IOException {
File fastaFile;
FileReader fastaReader;
BufferedReader fastaBuffer = null;
StringBuilder fastaString = new StringBuilder();
try {
fastaFile = new File("res/NC_017108.fna");
fastaReader = new FileReader(fastaFile);
fastaBuffer = new BufferedReader(fastaReader);
String fastaDescription = fastaBuffer.readLine();
String line = fastaBuffer.readLine();
while (line != null) {
fastaString.append(line);
line = fastaBuffer.readLine();
}
System.out.println(fastaDescription);
System.out.println();
String currentFastaAcid;
for (int i = 0; i < fastaString.length(); i+=3) {
currentFastaAcid = fastaString.toString().substring(i, i + 3);
System.out.println(currentFastaAcid);
}
} catch (NullPointerException e) {
System.out.println(e.getMessage());
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
} finally {
fastaBuffer.close();
}
}
}
currentFastaAcid = fastaString.toString().substring(i, i + 3);
Please replace with
currentFastaAcid = fastaString.substring(i, i + 3);
toString method of StringBuilder create new instance of String object every time you call it. It still contain a copy of all your large string. If you call substring directly from StringBuilder it will return a small copy of substring.
Also remove System.out.println if you don't really need it.
The big factor here is you are doing the call to substring over a new String each time.
Instead, use substring directly over the stringbuilder
for (int i = 0; i < fastaString.length(); i+=3){
currentFastaAcid = fastaString.substring(i, i + 3);
System.out.println(currentFastaAcid);
}
Also, instead of print the currentFastaAcid each time, save it into a list and print this list at the end
List<String> acids = new LinkedList<String>();
for (int i = 0; i < fastaString.length(); i+=3){
currentFastaAcid = fastaString.substring(i, i + 3);
acids.add(currentFastaAcid);
}
System.out.println(acids.toString());
Your main problem besides the debug output surely is, that you are creating a new String with your completely read data from the file in each iteration of your loop:
currentFastaAcid = fastaString.toString().substring(i, i + 3);
fastaString.toString() will give the same result in each iteration and therefore is redundant. Get it outside the loop and you will surely save some seconds runtime.
Apart from suggested optimization in the serial code, I will go for parallel processing to reduce time further. If you have really big file, you can divide the work of reading file and processing read-lines, in separate threads. That way, when one thread is busy reading nextline from large file, other thread can process read-lines and print them on console.
If you remove the
System.out.println(currentFastaAcid);
line in the for loop, you will gain quite decent time.
I'm writing a little program to analyze some data I have and this code, which worked yesterday, is no longer working.
For the life of me, I can't tell why. To my eye, everything is as it should be. I've tried re-syncing the folder on my computer with my GitHub Repo and still was receiving the same error. Maybe a second pair of eyes could help me out?
The text file being read from can be found here.
Here are the methods referred to by the stack trace:
public static void main(String[] args) throws IOException{
FileManagementMethods fmm = new FileManagementMethods();
fmm.runProgram();
}
void runProgram() throws IOException{
boolean doesFileExist = doesFileExist();
if(doesFileExist){
int numLines = getNumberOfLines();
String[] linesFromFile = getLines(numLines);
WeatherAnalysisMethods wam = new WeatherAnalysisMethods();
wam.parseFileAverageTemp(linesFromFile);
wam.parseFileAverageHumidity(linesFromFile);
wam.predictNextTemperature(linesFromFile);
} else{
try {
throw new IOException("Could not find log.txt in default directory");
} catch (IOException e) {
e.printStackTrace();
}
}
}
void parseFileAverageHumidity(String[] linesFromFile) throws IOException{
int[] humiditiesFromFile = new int[linesFromFile.length];
humiditiesFromFile = getHumiditiesFromFile(linesFromFile.length);
int averageFromFile = 0;
for(int i = 0; i < humiditiesFromFile.length; i++){
averageFromFile += humiditiesFromFile[i];
}
averageFromFile = averageFromFile / humiditiesFromFile.length;
String outString = "Average humidity for whole file = " + averageFromFile;
FileManagementMethods fmm = new FileManagementMethods();
fmm.saveAnalyzedData(outString);
}
int[] getHumiditiesFromFile(int numLines){
int[] humiditiesFromFile = new int[numLines];
FileManagementMethods fmm = new FileManagementMethods();
String[] lines = fmm.getLines(numLines);
int i = 0;
while(1 < numLines){
String[] lineDivides = lines[i].split(",");
String tempString = lineDivides[4];
humiditiesFromFile[i] = Integer.parseInt(tempString);
i++;
}
return humiditiesFromFile;
}
Line 51:
String tempString = lineDivides[4];
And here is the stack trace:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at org.weatheralert.analysis.WeatherAnalysisMethods.getHumiditiesFromFile(WeatherAnalysisMethods.java:51)
at org.weatheralert.analysis.WeatherAnalysisMethods.parseFileAverageHumidity(WeatherAnalysisMethods.java:22)
at org.weatheralert.analysis.FileManagementMethods.runProgram(FileManagementMethods.java:22)
at org.weatheralert.analysis.Main.main(Main.java:9)
If you guys need more information, don't hesitate to ask.
The problem is here, inside your getHumiditiesFromFile method:
while(1 < numLines){
It should be
while (i < numLines){
Since inside this loop you're calling humiditiesFromFile[i].
As Foo Bar User noted in comment, the error may be here:
String tempString = lineDivides[4];
It would be better to make sure there are at least 4 items in that array.
Besides that, the error noted in the section above (assuming it's not a typo) could also throw this Exception for being in an infinite loop.
You've hardcoded a 4 here without checking to see if it exists:
String tempString = lineDivides[4];
You will need to do some data validation beforehand. You're assuming that the line you read has at least three commas. Either change the code to check, or validate the file.
So, it was a silly issue.... JSoup had a couple bad connections and didn't save some of the data I needed it to save. So it didn't recognize some of the lines of data.
Once I fixed the actual data, it runs without issue again.
I apologize for wasting your time.