0
package labweek10;

import java.io.*;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;

public class arrayTourlist {

    public static void main(String[] args) throws FileNotFoundException, IOException
    {
        try {
            // read data from file
            FileReader fr = new FileReader("touristData.txt");
            BufferedReader br = new BufferedReader(fr);

            // write/display data into file
            FileWriter fw = new FileWriter("output.txt");
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter pw = new PrintWriter(bw);

            // object declaration
            Tourist t[] = new Tourist[20];

            int i = 0;
            String input = null;

            while ((input = br.readLine()) != null) {
                // create class tokenizer
                StringTokenizer st = new StringTokenizer(input, ";");

                String touristName = st.nextToken();
                int touristID = Integer.parseInt(st.nextToken());
                String countryFrom = st.nextToken();
                String countryRegion = st.nextToken();
                boolean touristType = Boolean.parseBoolean(st.nextToken());

                // create object to store data
                t[i] = new Tourist(touristName, touristID, countryFrom, countryRegion, touristType);

                t[i].toString();

                // Display the information of tourists which are in Europe region
                for (int d = 0; d < t.length; d++)

                    if (t[d].getRegion() == "Europe") {
                        pw.println("TOURIST DATA");
                        pw.println("------------");
                        pw.println("Tourist Name : " + touristName);
                        pw.println("Tourist ID : " + touristID);
                        pw.println("Country From : " + countryFrom);
                        pw.println("Country Region : " + countryRegion);
                        pw.println("Tourist Type : " + touristType);
                    }

            }

            // Count and display the number of individual tourists from China.
            int countC = 0;
            for (int x = 0; x < t.length; x++)

                if (t[x].getType() == false && "China".equals(t[x].getCountry())) {
                    countC++;
                }
            pw.println("The number of individual tourist(s) from China is " + countC);

            br.close();
            pw.close();
        }

        catch (FileNotFoundException e) {
            System.out.println("Problem :" + e.getMessage());
        } catch (IOException ioe) {
            System.out.println("Problem :" + ioe.getMessage());
        } catch (NoSuchElementException nsee) {
        }

        catch (NullPointerException npe) {
        }
    }
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
hosh
  • 1

0 Answers0