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) {
        }
    }
}
            Asked
            
        
        
            Active
            
        
            Viewed 51 times
        
    0
            
            
         
    
    
        Federico klez Culloca
        
- 26,308
- 17
- 56
- 95
 
    
    
        hosh
        
- 1
- 
                    and how would you know there is no error? catch (NoSuchElementException nsee) { } catch (NullPointerException npe) { } – Stultuske Jun 18 '21 at 06:14
- 
                    also, you shouldn't use StringTokenizer, use the split method of String instead – Stultuske Jun 18 '21 at 06:15
- 
                    `t[d].getRegion () == "Europe"` [that's not how you compare strings in java](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Federico klez Culloca Jun 18 '21 at 06:15
- 
                    As @Stultuske says, remove the last two catches. Then if you still can't see the problem run your code in the debugger. – tgdavies Jun 18 '21 at 06:47
- 
                    @tgdavies I wouldn't ignore Federico's comment, though – Stultuske Jun 18 '21 at 06:51