hi guys, good eveningn, ive been trying for two days now and i haven had no luck with this, im trying to compare 3 arraylists to filter by town, and age and show all the peoples names specifically from that town, heres my code any help would be appreciated.
heres my code:
import java.io.*;
import java.util.*;
public class read
{
ArrayList<String> Person = new ArrayList<String>();
ArrayList<String> Age = new ArrayList<String>();
ArrayList<String> Town = new ArrayList<String>();
public void readline()
{
try
{
Scanner input = new Scanner (System.in);
String textdoc = null;
//System.out.print( "¿Cual es el nombre del archivo a leer? debera finalizarlo escribiendo extension .txt \n" );
System.out.print( "what is the name of the textfile? use extension .txt \n" );
textdoc = input.next();
FileInputStream stream = new FileInputStream(textdoc);
DataInputStream in = new DataInputStream(stream);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = reader.readLine();
while(line != null)
{
Person.add(line);
line = reader.readLine();
}
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
//Show Persons name by Town.
public void showbytown()
{
Scanner input = new Scanner (System.in);
String textdoc = "";
System.out.println(" choose the town to print all the names");
textdoc = input.next();
int i=0;
for( String textdocs : Person )
{
if((textdocs) .equalsIgnoreCase(textdoc))
{
int nuevoindice = i-1;
System.out.println(Person.get(nuevoindice));
}
i++;
}
}
// show persons name by age interval.
public void showbyage()
{
Scanner input = new Scanner (System.in);
String textdoc = "";
String textdoc3="";
System.out.println("enter minimun age interval");
textdoc3 = input.next();
System.out.println("enter max age interval");
textdoc = input.next();
int set = Integer.parseInt(textdoc);
int j = Integer.parseInt(textdoc3);
while(j<=set)
{
String textdoc2 = Integer.toString(j);
Age.add(textdoc2);
j++;
}
for (int i=0;i<Person.size();i++)
{
for (int k=0;k<Age.size(); k++)
{
if(Person.get(i).equals(Age.get(k)))
{
int nuevoindice = i-2;
System.out.println(Person.get(nuevoindice));
}
}
}
}
//Show Persons name by Town and age interval THIS IS THE METHOD IM HAVING TROUBLE WITH
public void showbyagetown()
{
Scanner input = new Scanner (System.in);
String textdoc = "";
String textdoc3="";
String textdoc4="";
System.out.println("Enter the town you would like to show names");
textdoc4 = input.next();
System.out.println("enter minimun age");
textdoc3 = input.next();
System.out.println("enter max age");
textdoc = input.next();
int set = Integer.parseInt(textdoc);
int j = Integer.parseInt(textdoc3);
Age.clear();
while(j<=set)
{
String textdoc2 = Integer.toString(j);
Age.add(textdoc2);
j++;
}
Town.add(textdoc4);
for (int i=0;i<Person.size();i++)
{
for (int k=0;k<Age.size(); k++)
{
for (int l=0;l<Town.size(); l++)
{
if((Person.get(i)).equals((Age.get(k)))&&(Person.get(i)).equals((Town.get(l))))
{
int nuevoindice = i-2;
System.out.println(Person.get(nuevoindice));
}
}
}
}
/*
for (int i=0;i<Person.size();i++)
{
for (int k=0;k<Age.size(); k++)
{
if(Person.get(i).equals(Age.get(k)).equalsIgnoreCase(textdoc4))
{
int nuevoindice = i-2;
System.out.println(Person.get(nuevoindice));
}
}
}
*/
}
public static void main(String[] args)
{
read fileLBL = new read();
fileLBL.readline();
fileLBL.showbytown();
fileLBL.showbyage();
fileLBL.showbyagetown();
}
}