Search for strings in file and add selected line values to ArrayList.

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 1948 Views - Last Post: 10 June 2012 - 02:26 PM Rate Topic: -----

#16 xiphias   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 14-February 12

Re: Search for strings in file and add selected line values to ArrayList.

Posted 23 May 2012 - 08:31 PM

I used a BufferedReader wrapped round a scanner. That seem to have solved the problem. Thanks
Was This Post Helpful? 0
  • +
  • -

#17 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Search for strings in file and add selected line values to ArrayList.

Posted 24 May 2012 - 07:48 AM

Quote

if (rf.nextLine().contains("Total")) {


That sort of thing will advance the Scanner, so if it doesn't contain 'Total' (or of course even if it does) you will essentially have 'lost' the line

This post has been edited by g00se: 24 May 2012 - 07:48 AM

Was This Post Helpful? 1
  • +
  • -

#18 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Search for strings in file and add selected line values to ArrayList.

Posted 24 May 2012 - 11:41 AM

I would be inclined to parse it something like


    public void getAtmTotals(File file) {
        final int NUM_ATM_FIELDS = 13;
        final String SEP_PAT = "[\\s/]+";
        Scanner fileScanner = null;
        String line = null;

        try {
            fileScanner = new Scanner(file);

            while (fileScanner.hasNextLine()) {
                line = fileScanner.nextLine();

                String[] atoms = line.split(SEP_PAT); // A bit heavy, but at least safe (make even safer if necessary)

                if (atoms.length == NUM_ATM_FIELDS) {
                    Scanner lineScanner = new Scanner(line);
                    lineScanner.useDelimiter(SEP_PAT);
                    atmUs.add(new Atm(lineScanner.next(), lineScanner.next(),
                            lineScanner.nextInt(), lineScanner.nextFloat(),
                            lineScanner.nextFloat(), lineScanner.nextFloat(),
                            lineScanner.nextInt(), lineScanner.nextFloat(),
                            lineScanner.nextInt(), lineScanner.nextFloat(),
                            lineScanner.nextInt(), lineScanner.nextFloat(),
                            lineScanner.nextInt()));
                } else {
                    // Whatever you need to do here
                }
            } // end while
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileScanner != null) {
                fileScanner.close();
            }
        }
    }



Was This Post Helpful? 1
  • +
  • -

#19 xiphias   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 14-February 12

Re: Search for strings in file and add selected line values to ArrayList.

Posted 26 May 2012 - 07:03 AM

View Postg00se, on 24 May 2012 - 11:41 AM, said:

I would be inclined to parse it something like


Thanks, i think this is a very good approach. The only thing is though, the file provided has similar tables later down in the file with same Atm IDs and same number of columns. I think this is one of the key points of the exercise. By the way this is not homework :) I'm simply doing for fun and learning.
Was This Post Helpful? 0
  • +
  • -

#20 xiphias   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 14-February 12

Re: Search for strings in file and add selected line values to ArrayList.

Posted 06 June 2012 - 03:13 PM

Hey what if i want to also add null and zero values to arraylist for strings (ATM ID) that were not found in file? I have tried using the contains() method, overriding equals() and hashCode() but i've yet to accomplish this. Any suggestions?

Thanks
Was This Post Helpful? 0
  • +
  • -

#21 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Search for strings in file and add selected line values to ArrayList.

Posted 06 June 2012 - 03:22 PM

Well i'd need to have more info to say.
Was This Post Helpful? 0
  • +
  • -

#22 xiphias   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 14-February 12

Re: Search for strings in file and add selected line values to ArrayList.

Posted 06 June 2012 - 11:18 PM

something like the following.

if (atmUs != null)
			{
				for (Atm list : atmList)
				{
					if (!(list.getDescr().equalsIgnoreCase("NULL")))
					{ 
						synchronized (atmUs)
						{
								
								if (!atmUs.contains(list.getTermID()))
								{									
									atmUs.add(new Atm (null, null, list.getAtmID(), null, 0.0f, 0.0f, 0.0f, 0, 0.0f, 0, 0.0f, 0, 0.0f, 0));									
								}
							
						};
					}
				}
				
			}


However with the above code values will be added to the List for every iteration because of the list.getAtmID. This is not what i am trying to do.

Initializing the ArrayList with default values could work given that i can replace them later down.

This post has been edited by xiphias: 06 June 2012 - 11:26 PM

Was This Post Helpful? 0
  • +
  • -

#23 xiphias   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 14-February 12

Re: Search for strings in file and add selected line values to ArrayList.

Posted 06 June 2012 - 11:34 PM

So basically what I'm trying to do is compare two arraylist with different lengths. I'm attempting to compare one of their columns. If an element from arralist1 column1 dont exist in arraylist2 column3 add to arraylist2.
Was This Post Helpful? 0
  • +
  • -

#24 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Search for strings in file and add selected line values to ArrayList.

Posted 07 June 2012 - 03:08 AM

If the List instances are different lengths, how do you know which column is missing?
Was This Post Helpful? 0
  • +
  • -

#25 xiphias   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 14-February 12

Re: Search for strings in file and add selected line values to ArrayList.

Posted 07 June 2012 - 06:11 AM

View Postg00se, on 07 June 2012 - 03:08 AM, said:

If the List instances are different lengths, how do you know which column is missing?

I different size.
Was This Post Helpful? 0
  • +
  • -

#26 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Search for strings in file and add selected line values to ArrayList.

Posted 07 June 2012 - 06:26 AM

Don't understand sorry. Not sure how that answers the question
Was This Post Helpful? 0
  • +
  • -

#27 xiphias   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 14-February 12

Re: Search for strings in file and add selected line values to ArrayList.

Posted 07 June 2012 - 10:52 AM

:) I didn't mean to post the previous. Browser / site was unresponsive. But i did mean size when I said length since we are talking about ArrayLists. The first arraylist instance size is fixed.
The second arraylist size is determined by the number of results found in file. This is why i am trying to implement the contains() method.
Was This Post Helpful? 0
  • +
  • -

#28 xiphias   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 14-February 12

Re: Search for strings in file and add selected line values to ArrayList.

Posted 10 June 2012 - 02:26 PM

I got this working. I had to override the hashcode() and equlas() only for
AtmID.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2