4 Replies - 2706 Views - Last Post: 22 April 2013 - 02:05 PM Rate Topic: -----

#1 ctroop4ever   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 13-February 13

Putting data file into Array List

Posted 22 April 2013 - 08:22 AM

I am having trouble putting a data file into an array list, the data file looks like:
Carlow
Cavan
Clare
Cork
Donegal
Dublin
Galway
Kerry

I want to make a pop up box that lets the user select a county from the list, so I can use it further in my program, the error is on the line
 selectedCountey = (String) JOptionPane.showInputDialog(null, "Choose a county","Input", 1, null, options, options[0]); 


This is my codes can someone please give me a clue what is wrong?

import javax.swing.JOptionPane;
import java.util.*;
import java.io.*;
public class countiesIntoArrayList
{
	public static void main (String [ ]args) throws IOException 
	{
		String uInput = (String) JOptionPane.showInputDialog(null, "Enter the property ID");
		Scanner in = new Scanner (new File("properties.txt"));
		String aLineFromFile = "";
                	
				while (in.hasNext())
				{
				
					aLineFromFile = in.next();
					//Check if property exists
					
					if (!in.hasNext()) 
					JOptionPane.showMessageDialog(null, "This property ID does not exist.");
					
					else
					{
						ArrayList<ArrayList<String>> counties = new ArrayList<ArrayList<String>>();
						counties.add(new ArrayList<String>());
						counties.add(new ArrayList<String>());
						String filename = ("counties.txt");
						String options[];
						String [] fileItem;
						String position = "";
						String selectedCounty;
						int positionOfSelectedCounty, selectedCountyNumber;
						File inputFile = new File(filename);
						if (inputFile.exists())
						{
							Scanner fileReader = new Scanner(inputFile);
							
							while(fileReader.hasNext())
							{
								fileItem = (fileReader.nextLine()).split(",");
								counties.get(0).add(fileItem[0]);  
								counties.get(1).add(fileItem[1]);  
							}
							 
							fileReader.close();
							// create 1D array and populate it with player names
							
							options = new String[counties.get(1).size()];
						
							for (int item = 0; item < counties.get(1).size(); item++)
						
								if (!(counties.get(1).get(item)).equals("#"))
								options[item] = counties.get(1).get(item);
							// present list of player names to end user
							// user can slect item and click on OK or click Canel
								selectedCountey = (String) JOptionPane.showInputDialog(null, "Choose a county","Input", 1, null, options, options[0]);

						if (selectedCounty != null)
						{
							positionOfSelectedCounty = counties.get(1).indexOf(selectedCounty);
						    position = (counties.get(0).get(positionOfSelectedCounty));
						}
							
					System.out.println(positionOfSelectedCounty);
				}
					
			}					
		}
	}
}


Sorry wrong error, fixed that one, new error is on line
position = (counties.get(0).get(positionOfSelectedCounty));


Is This A Good Question/Topic? 0
  • +

Replies To: Putting data file into Array List

#2 Luckless   User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Putting data file into Array List

Posted 22 April 2013 - 08:49 AM

Please tell us the exact error you are receiving so we can better assist you as stated in the guidelines. Thanks!
Was This Post Helpful? 0
  • +
  • -

#3 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3550
  • View blog
  • Posts: 16,187
  • Joined: 20-September 08

Re: Putting data file into Array List

Posted 22 April 2013 - 09:07 AM

http://technojeeves..../74-string-list

http://technojeeves....ava-7-and-above

Using first:

	StringList sl = new StringList();
	sl.read("counties.txt");
	String[] options = new String[sl.size()];
	for(int i = 0;i < options.length;i++) {	
	    options[i] = sl.get(i);
	}
String selectedCounty = (String) JOptionPane.showInputDialog(null, "Choose a county","Input", 1, null, options, options[0]); 


This post has been edited by g00se: 22 April 2013 - 09:18 AM
Reason for edit:: code

Was This Post Helpful? 0
  • +
  • -

#4 ctroop4ever   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 13-February 13

Re: Putting data file into Array List

Posted 22 April 2013 - 01:49 PM

I have fixed my code up a bit, i can create the list now and display it but it goes into an infinate loop, how do I stop this?
import javax.swing.JOptionPane;
import java.util.*;
import java.io.*;
public class countiesIntoArrayList
{
	public static void main (String [ ]args) throws IOException 
	{
		String uInput = (String) JOptionPane.showInputDialog(null, "Enter the property ID");
		Scanner in = new Scanner (new File("properties.txt"));
		String aLineFromFile = "";
        
				while (in.hasNext())
				{
				
					aLineFromFile = in.next();
					//Check if property exists
				
				
					if (!in.hasNext()) 
					JOptionPane.showMessageDialog(null, "This property ID does not exist.");
				
					else
					{
						ArrayList<ArrayList<String>> counties = new ArrayList<ArrayList<String>>();
						counties.add(new ArrayList<String>());
						counties.add(new ArrayList<String>());
						    
						String filename = "counties.txt";
								
						String options[];
						   		
						String [] fileItem;
						
						String selectedCounty;
								
						int positionOfSelectedCounty, selectedCountyNumber;
								
						File inputFile = new File(filename);
								
						if (inputFile.exists())
						{
							Scanner fileReader = new Scanner(inputFile);
						        
							while(fileReader.hasNext())
							{
								fileItem = (fileReader.nextLine()).split(",");
								counties.get(0).add(fileItem[0]);  
								counties.get(1).add(fileItem[1]);  
							} 
							fileReader.close();
							 	  
						// create array list and populate it with counties   
						       
						options = new String[counties.get(1).size()];        
						       
						for (int index = 0; index < counties.get(1).size(); index++)
						         
						options[index] = counties.get(1).get(index);
						       
						// present list of counties to end user
						
						selectedCounty = (String) JOptionPane.showInputDialog
						(null, "Pick the county it is located in","Input", 1, null, options, options[0]);
						     
						
							 
						}
					}	 
				}
			
	}
}



Was This Post Helpful? 0
  • +
  • -

#5 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1577
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: Putting data file into Array List

Posted 22 April 2013 - 02:05 PM

Or simply use the build in functionality found in java 1.7 :)
		try {
			List<String> countries = Files.readAllLines(Paths.get("counties.txtt"), StandardCharsets.UTF_8);
		}
		catch (IOException e) {
			e.printStackTrace();
		}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1