I am trying to read from 45 pages that are all the same (except for the part im reading of course) and write them in a list of line lists.
I wrote this code so far:
public static ArrayList<ArrayList<String>> linesWeNeed(){
ArrayList<ArrayList<String>> returnListList = new ArrayList<ArrayList<String>>();
for(int i = 1; i<=45; i++){
int pageNum=(i*20)-20;
System.out.println("PageNum"+pageNum);
URL url=null;
try {
url = new URL("http://tq.mot.gov.il/index.php?option=com_moofaq&iotype=w&view=category&lang_ovrrde=ENG&id=3&Itemid=29&limitstart="+pageNum);
} catch (MalformedURLException e) {
System.out.println("Oh uh!");
}
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
} catch (IOException e) {
System.out.println("FATAL ERROR");
e.printStackTrace();
}
ArrayList<String> lineCodes = new ArrayList<String>();
ArrayList<String> linesWeNeed = new ArrayList<String>();
String line;
try {
readLines:
while((line=in.readLine())!=null){
if(line.contains("</tr></table></div></div></li></ul></div></div><br /></div>")){
break readLines;
}
lineCodes.add(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int o = 1; o>=lineCodes.size(); o++){
String readLine = lineCodes.get(o-1);
if(o>=297){
linesWeNeed.add(readLine);
}
}
returnListList.add(linesWeNeed);
}
return returnListList;
}
There are no errors, but for some reason every list in the arraylist the methods return is empty. Why?
Thanks in advance!
for(int o = 1; o>=lineCodes.size(); o++){
Your loop condition is back to front. Try <=.
for(int o = 1; o>=lineCodes.size(); o++){
Double-check the loop condition there; seems like that block is never going to execute...
Related
I have the following code with an ArrayList that is filled in other Java class that is a Thread (this is always filled, I have checked every time), then I have in the Main class the problematic block while(true) and the issue is that never ends if I comment or delete the line System.out.println(IDS.size());
Although are Threads in charge of complete the information I need, I cant show the results until all of them are finished. This is the reason of while(true) block.
public static ArrayList<String> IDS = new ArrayList<String>();
//this arraylist is filled in other classes correctly (each Thread add a element -> 10 in total)
//here is the problem
while (true) {
//if I comment the next system.out.println line
//the loop never ends and never breaks
System.out.println(IDS.size());
if(IDS.size()==10) {
break;
}
}
//when the array is filled with the 10 elements, I show all the info
for (int k = 0; k < impresoras.size(); k++) {
System.out.println(impresoras.get(k).ID);
}
I don´t know why this is happening, can someone helps?
Thanks in advance.
Finally I use the join methods for the Threads to avoid the while true loop.
So only I have to call the next method and then do the System.out.println of my items, that will be printed when all Threads ends their work.
public static ArrayList<MyThread> threadList = new ArrayList<MyThread>();
public static void openFileAndCreateThreads(String fileName) {
String line = null;
FileReader fileReader = null;
BufferedReader bufferedReader = null;
try {
fileReader = new FileReader(fileName);
bufferedReader = new BufferedReader(fileReader);
while ((line = bufferedReader.readLine()) != null) {
String[] line_parts = line.split(";");
MyThread t= new MyThread(line_parts[0].trim(), line_parts[1], line_parts[2]);
threadList.add(t);
t.start();
}
for (int j=0;j<threadList.size();j++) {
try {
threadList.get(j).join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
bufferedReader.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
bufferedReader.close();
} catch (Exception ex) {
}
}
}
I want to create a multidimensional array and pass it as a parameter in a method and then fill the arrayList with elements and return the new version of the arrayList to be able to use that array in different classes but I get java.lang.NoSuchMethodError: I think the problem is about the way i return the array. I searched but I could not find . How can I do it correctly?
here is my code;
public test{
public static List<List<String>> 2Darray=new ArrayList<List<String>>(); // TE ERROR IN THIS LINE
public List<List<String>> fillArray(List<List<String>> array){
BufferedReader in = null;
ArrayList<String> row = new ArrayList<String>();
try {
in = new BufferedReader(new FileReader("sampleFile.txt"));
String read = null;
while ((read = in.readLine()) != null) {
String[] splited = read.split("\\s+");
for(int i=0; i<splited.length ; i++){
row.add(splited[i]);
}
array.add(row);
}
} catch (IOException e) {
System.out.println("There was a problem: " + e);
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
}
return array;
}
A little tinkering (just getting it to compile) results in this which seems not to have a problem. Perhaps your issue is elsewhere.
public List<List<String>> fillArray(List<List<String>> array) {
BufferedReader in = null;
ArrayList<String> row = new ArrayList<String>();
try {
in = new BufferedReader(new FileReader("sampleFile.txt"));
String read = null;
while ((read = in.readLine()) != null) {
String[] splited = read.split("\\s+");
for (int i = 0; i < splited.length; i++) {
row.add(splited[i]);
}
array.add(row);
}
} catch (IOException e) {
System.out.println("There was a problem: " + e);
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return array;
}
BTW: You should really use try with resources - it is much clearer.
Modified your code a bit so that it compiled, and replaced the reading from a text file to reading a string. There were several issues, but it seems to work. Give it a try.
The main problems I noticed were mismatching curly braces, and starting a variable name with a number.
import java.util.*;
import java.io.*;
public class Main {
public static List<List<String>> array2D = new ArrayList<List<String>>();
public List<List<String>> fillArray(List<List<String>> array) {
BufferedReader in = null;
ArrayList<String> row = new ArrayList<String>();
try {
String str = "Some test text";
InputStream is = new ByteArrayInputStream(str.getBytes());
//in = new BufferedReader(new FileReader("sampleFile.txt"));
in = new BufferedReader(new InputStreamReader(is));
String read = null;
while ((read = in.readLine()) != null) {
String[] splited = read.split("\\s+");
for(int i=0; i<splited.length ; i++) {
row.add(splited[i]);
}
array.add(row);
}
}
catch (IOException e) {
System.out.println("There was a problem: " + e);
e.printStackTrace();
}
finally {
try {
in.close();
}
catch (IOException e) {
}
}
return array;
}
public static void main(String[] args) {
Main main = new Main();
List<List<String>> test = main.fillArray(array2D);
for(int i = 0; i < test.size(); i++) {
for(int j = 0; j < test.get(i).size(); j++) {
System.out.println(test.get(i).get(j));
}
}
}
}
I'm trying to read a .txt file called Heights.txt, which contains a string of numbers, each separated by a ":". The method produces one error that I can't seem to figure out.
It says that "the method must return a result of type int[]", at the very first line of this code.
I don't understand why it says this, as integerHeightDataPoints should be an integer array at that point, and should be able to be returned to a int[] method?
public static int[] readFile(){
BufferedReader br = null;
String dataPoints;
try {
br = new BufferedReader(new FileReader("Path\\Heights.txt"));
}
catch(IOException e) {
System.out.println("Please enter data first");
System.exit(0);
}
try {
while((dataPoints = br.readLine()) != null) {
if (dataPoints.contains(":")) {
String[] heightDataPoints = dataPoints.split(":");
int[] integerHeightDataPoints = new int[heightDataPoints.length];
for (int i = 0; i < integerHeightDataPoints.length; i++) {
integerHeightDataPoints[i] = Integer.parseInt(heightDataPoints[i]);
}
return integerHeightDataPoints;
}
}
}
catch (IOException e) {
System.out.println("Error reading file");
e.printStackTrace();
}
}
It's because you don't return anything in second IOException case or (as #Exception_al mentioned) when while never triggers.
public static int[] readFile() {
BufferedReader br = null;
String dataPoints;
try {
br = new BufferedReader(new FileReader("/tmp/file1"));
} catch (IOException e) {
System.out.println("Please enter data first");
System.exit(0);
}
int[] integerHeightDataPoints = new int[0];
try {
while ((dataPoints = br.readLine()) != null) {
if (dataPoints.contains(":")) {
String[] heightDataPoints = dataPoints.split(":");
integerHeightDataPoints = new int[heightDataPoints.length];
for (int i = 0; i < integerHeightDataPoints.length; i++) {
integerHeightDataPoints[i] = Integer.parseInt(heightDataPoints[i]);
}
return integerHeightDataPoints;
}
}
} catch (IOException e) {
System.out.println("Error reading file");
e.printStackTrace();
}
return integerHeightDataPoints;
}
This question already has answers here:
Read file contents into an ArrayList
(2 answers)
Closed 6 years ago.
I am attempting to read the flat file and transfer its contents to ArrayList.By the code is not working
flat file
BEG
SN:M7254168
VER:1.10
HC 00002 4077 215
D 4080006441610001
D 8475190354020001
END
public class BigFlatFileProcess {
private static Logger logger = Logger.getLogger(BigFlatFileProcess.class);
public ArrayList<Item> process(String sile) {
ArrayList<Item> huge = new ArrayList<Item>();
ArrayList<Integer> nlist = getLineNo(sile);
try{
for(int i=0; i<=nlist.size();i++){
System.out.println(nlist.get(i)+" "+nlist.get(i+1));
logger.info("File contents are ");
ArrayList<Item> pcom = showLines(sile, nlist.get(i)-1,nlist.get(i+1)-2);
System.out.println(" Number of items in this bin "+pcom.size());
for(int j=0;j<pcom.size();j++){
logger.info("from bigflatfileprocess"+pcom.get(j).toString());
huge.add(pcom.get(j));
}
System.out.println("STARTING ..");
}
}catch(Exception e){
logger.info(" Exception occured in file processing that can be safely ignored");
}
logger.info(" Processed the AML flat file successfully ");
return huge;
}
public ArrayList<Item> showLines( String fileName, int startLine, int endLine){
ArrayList<Item> preList = new ArrayList<Item>();
int currentLineNo = 0;
String line = null;
try(BufferedReader bReader = new BufferedReader(new FileReader(fileName)) ){
Item pre = new Item();
while(currentLineNo<startLine){
if(bReader.readLine()==null){
throw new IOException("File too small");
}
currentLineNo++;
}
for( ;currentLineNo <=endLine;currentLineNo++ ){
line = bReader.readLine();
if(line==null){
return null;
}
if(line.startsWith("H")){
String bnum = line.substring(17,20);
pre.setBin(bnum);
String cnum = line.substring(12,16);
pre.setCycNumber(cnum);
}
if(line.startsWith("D")){
String upcNum = line.substring(4, 16);
pre.setUpcCode(upcNum);
String qty = line.substring(16,20);
pre.setQuantity(Integer.parseInt(qty));
logger.info(line);
logger.info(" The List Bin :"+pre.getBin()+" upcCode: "+pre.getUpcCode()+" Quantity: "+pre.getQuantity()+" cycle = "+pre.getCycNumber());
}
preList.add(pre);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return preList;
}
public ArrayList<Integer> getLineNo(String fileName){
ArrayList<Integer> hlist = new ArrayList<Integer>();
try(LineNumberReader lReader = new LineNumberReader(new FileReader(fileName)) ){
String line;
while((line = lReader.readLine()) != null){
if(line.startsWith("H")||line.startsWith("END")){
int i = lReader.getLineNumber();
logger.info("Line numbers "+i);
hlist.add(i);
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return hlist;
}
}
The log is shown in this picture
I cant understand why my for loop is adding the last record to array list.
Any suggestions will be deeply appreciated
From my observation, ur for loop is missing an int variable declaration to define the starting point for the iteration. Second, ur condition states less than or equal to. U should replacing that with a less than < symbol alone. That leave out the last variable. Hope this helps.
Part of my homework.
I have written a method to split all words to ArrayList. Words are taken from all files in given project directory.
Unfortunately sometimes lines are skipped... and I wish to find the bug. Please help.
To specify: files are of 7 "words" separated with tabs in each line.
public class TravelData {
static List<String> tour = new ArrayList<String>(); //lista zlokalizowana według nagłówka wiersza
public TravelData(File dataDir) {
String currentDirPath = new File(dataDir.toString()).getAbsolutePath();
File currentDir = new File(currentDirPath);
File[] listOfFiles = currentDir.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
try {
Scanner s = new Scanner(new File(listOfFiles[i].toString()));
while (s.hasNextLine()){
ArrayList<String> line = new ArrayList<String>();
for(String value: s.nextLine().split("\t"))
{
line.add(value);
}
lineConverter(line, dbDate); //do something with grabbed data
}
s.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
//[...]
}
I've personally not used Scanners much, so I can't immediately spot the issue. But here is some old code using buffered file input stream that I've added your specific bits to:
public TravelData(File dataDir) {
File[] listOfFiles = dataDir.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
InputStream inputStream = null;
BufferedReader buffReader = null;
try {
inputStream = new FileInputStream(listOfFiles[i]);
buffReader = new BufferedReader(new InputStreamReader(inputStream));
String fileLine = buffReader.readLine();
while(fileLine != null) {
ArrayList<String> line = new ArrayList<String>();
for(String value: fileLine.split("\t")) {
line.add(value);
}
lineConverter(line, dbDate);
fileLine = buffReader.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(buffReader != null) try { buffReader.close(); } catch (IOException e) { }
if(inputStream != null) try { inputStream.close(); } catch (IOException e) { }
}
}
}
}