Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Having difficulty with ArrayList method ".contains", any feedback greatly appreciated!

  1. #1
    Junior Member BlackStones's Avatar
    Join Date
    Feb 2014
    Posts
    15
    My Mood
    Fine
    Thanks
    1
    Thanked 1 Time in 1 Post

    Red face Having difficulty with ArrayList method ".contains", any feedback greatly appreciated!

    Hello, the objective of my program is to read a file called input_data.txt and to calculate an invoice in which my program will print the results in a formatted text file. I haven't advanced to this step yet because I'm having trouble checking my ArrayList for a certain string. The reason I am searching for "duration" is so that my program will know when the heading is complete.

    Here is the sample input file given:
    account_number start_time_of_call duration
    10011A 21:50 20.2
    10011A 13:23 12.3
    10033C 23:00 34.0
    00101B 10:23 45.1

    Eventually, I must separate the fields and calculate payment prices based on the duration and time of the calls.
    Any suggestions are much appreciated, even conceptual or logical suggestions because I feel I am not going in the right direction.

    Thank you

    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
     
    public class spInputOutput {
     
    	public static PrintWriter outputStream;
     
    	public static void main(String[] args) throws IOException
    	{
    		readData();
    	}
     
    	public static void readData() throws IOException
    	{
    		double monthlyBase = 20;
    		double amountDue = 0;
    		double time = 0.00;
     
    		ArrayList<String> fileCont = new ArrayList<String>();
     
    		double daytimeCall = 0.12;  /*per minute for calls that started between 8:00 AM and 10:00PM, 
    		inclusive $0.05 per minute all other times*/
     
    		/*
    		{
    			System.out.println("Account");
    			System.out.println(monthlyBase);
    			System.out.println(time);
    			System.out.println(daytimeCall);
    		}*/		
     
    		FileReader fr = new FileReader("input_data.txt");
    		BufferedReader br = new BufferedReader(fr);
     
    		String invoice = "invoice.txt";
            try {
    			outputStream = new PrintWriter(invoice);
    		}
            catch (FileNotFoundException e) {
    			e.printStackTrace();
    		}
     
            outputStream.println("Invoice");
            outputStream.println("**********");
            outputStream.println("");
            outputStream.println("Account");	
            outputStream.println("Amount Due");
     
            outputStream.close();
     
    		String text = "";
    		String line = br.readLine();
    		String searchString = "duration";
     
    		while(line != null)
    		{
    			text += line;
    			line = br.readLine();
    			fileCont.add(line);
     
    			//THIS IS WHERE MY PROBLEM IS
                            boolean check = fileCont.contains(searchString);
     
    		      if (check == true) 
    		      {
    		    	  System.out.println("duration found");
    		      }
    		      else
    			  {
    				System.out.println("duration not found");
    			  }
     
    		//System.out.println(text);	
    		}
     
    		System.out.println("Inovice");
     
    	}
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Having difficulty with ArrayList method ".contains", any feedback greatly appreciated!

    Can't you read each line as a String and break them into 3 parts split on the spaces, always knowing the last item is the 'duration?' Something like

    // read each line into a String[] array split on the spaces
    String[] inputLine = br.readLine().split( " " );

  3. #3
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Having difficulty with ArrayList method ".contains", any feedback greatly appreciated!

    If you are committed to using an arraylist for this task, you may want to print out each object to see if it contains what you expect. If an arraylist is optional, GregBrannon's method would work very nicely.

  4. #4
    Junior Member BlackStones's Avatar
    Join Date
    Feb 2014
    Posts
    15
    My Mood
    Fine
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Having difficulty with ArrayList method ".contains", any feedback greatly appreciated!

    Hello,

    Using an ArrayList is not mandatory so I will use GregBannon's suggestion. Will the array work if the file length varies (if I have a text file with 20 account numbers)?

    I used an ArrayList initially because I know arrays have determined lengths.

  5. #5
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Having difficulty with ArrayList method ".contains", any feedback greatly appreciated!

    The method that GregBrannon showed would work with any length as the array is only intended to hold one set of data (one line) at a time, not the entire file's text. So, you split the line into the 3 parts then do something with it, perhaps print it out or w/e you want really, and on the next iteration the values in the array would change to the next line; repeat process until the end of file.

  6. #6
    Junior Member BlackStones's Avatar
    Join Date
    Feb 2014
    Posts
    15
    My Mood
    Fine
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Having difficulty with ArrayList method ".contains", any feedback greatly appreciated!

    Hello KucerakJM,

    I have attempted to implement the array solution and was able to gather the data in a (String) line of text. My problem is that I don't know how to use substring or split to obtain elements in that line of data. I need to retrieve the account number and total amounts (which will be calculated) in arraylists because these values (customers and total amount of total orders) is subject to change. Here is my updated code, no errors.

    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
     
    public class spInputOutput {
     
    	public static PrintWriter outputStream;
     
    	public static void main(String[] args) throws IOException
    	{
    		readData();
    	}
     
    	public static void readData() throws IOException
    	{
    		double monthlyBase = 20;
    		double amountDue = 0;
    		double time = 0.00;
     
    		ArrayList<Double> amtDue = new ArrayList<Double>();
    		ArrayList<String> accts = new ArrayList<String>();
    		ArrayList<String> fileCont = new ArrayList<String>();
     
    		double daytimeCall = 0.12;  /*per minute for calls that started between 8:00 AM and 10:00PM, 
    		inclusive $0.05 per minute all other times*/
     
    		FileReader fr = new FileReader("input_data.txt");
    		BufferedReader br = new BufferedReader(fr);
     
    		String invoice = "invoice.txt";
            try {
    			outputStream = new PrintWriter(invoice);
    		}
            catch (FileNotFoundException e) {
    			e.printStackTrace();
    		}
     
            outputStream.println("Invoice");
            outputStream.println("**********");
            outputStream.println("");
            outputStream.println("Account");
            for(int i=0; i<5; i++)
            {
            	outputStream.println(accts);
            }
            outputStream.println("Amount Due");
            for(int i=0; i<5; i++)
            {
            	outputStream.println("20.20\n");
            }
            outputStream.println("**********");
            outputStream.println("Total");
     
     
            outputStream.close();
     
    		String text = "";
    		String line = br.readLine();
    		String[] a = new String[10];
     
                    //The values for this for loop were used to see if my program would still run
     
    		for(int i = 0; i<a.length ; i++)
    		{
    			a[i] = br.readLine();
    			//a[i].split("");
     
    		}
    		System.out.println(a[1]);
    		System.out.println(a[2]);
     
     
    		while(line != null)
    		{
    			text += line;
    			line = br.readLine();
    			fileCont.add(line);
    		//System.out.println(text);	
    		}
     
    		System.out.println("Invoice File Created");
     
    	}
    }

    My objectives are currently to;

    1. Format the input document so that the account numbers can be obtained and re-presented in the output file
    2. Calculate the total amount with the start time and duration considered (I still must derive this algorithm)
    3. Print these values to the invoice file

    Thanks again for your help so far

Similar Threads

  1. Understanding layers of system logic. Clarity for what "SDK" & "JDK" mean
    By MilkWetGhost in forum Java Theory & Questions
    Replies: 1
    Last Post: October 3rd, 2013, 12:25 PM
  2. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  3. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  4. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  5. "Static method cannot hide instance method from implemented Interface"
    By Gthoma2 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 21st, 2011, 03:03 AM