0% found this document useful (0 votes)
28 views6 pages

Activity 09: "Enter Town: "

This document describes a program that allows a user to input town names and zip codes, stores them in arrays, and performs a linear search to find matches. It includes: 1) Code to prompt the user for town/zip code input and store in 2D arrays town and ZipCode. 2) Code to combine town and zip code arrays into a single array for searching. 3) A linearSearch method that prompts the user to search by town or zip, gets their input, and loops through the array to find matches, setting a boolean found flag.

Uploaded by

Hasannor Mandi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views6 pages

Activity 09: "Enter Town: "

This document describes a program that allows a user to input town names and zip codes, stores them in arrays, and performs a linear search to find matches. It includes: 1) Code to prompt the user for town/zip code input and store in 2D arrays town and ZipCode. 2) Code to combine town and zip code arrays into a single array for searching. 3) A linearSearch method that prompts the user to search by town or zip, gets their input, and loops through the array to find matches, setting a boolean found flag.

Uploaded by

Hasannor Mandi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

ACTIVITY 09

1. import java.util.*;  
2. import java.lang.System;  
3.   
4. public class linear  
5. {  
6.         public static Scanner in = new Scanner(System.in);  
7.           
8.         public static boolean found = false;  
9.         public static String search;  
10.        public static String searchAgain;  
11.        public static String loc;  
12.        public static int row = 2;  
13.        public static int column = 2;  
14.        public static int x = 0;  
15.        public static int y = 0;  
16.        public static String[][] array;  
17.        public static String[][] town;  
18.        public static int[][] ZipCode;  
19.          
20.    public static void main(String[] args)  
21.    {  
22.          
23.  
24.  
25.        int TempRow = 1;  
26.        int TempColumn = 2;  
27.          
28.          
29.        /* we separate each column between  Town and ZipCode in order the 
user  
30.        can input zipcode without any problem and the output of the inputt
ed will be arrange accordingly*/  
31.        town = new String[TempRow][TempColumn];   
32.        ZipCode = new int[TempRow][TempColumn];  
33.          
34.          
35.        for(int i=0; i<TempRow; i++)  
36.        {  
37.            for(int j=0; j<TempColumn; j++)  
38.            {  
39.                /* this is what we mean that if the user will input name o
f the town and it will add to row[0][0] and row[0][1]*/  
40.                System.out.print("Enter Town: ");   
41.                town[i][j] = in.next();  
42.                  
43.                /* As we all know that zipcode is an interger not characte
r so the user cannot input integer of zipcode and we did not declare strin
g as datatype of the zipcode because its useless that if we dont apply on 
the reality haha */  
44.                try  
45.                {  
ACTIVITY 09

46.                    System.out.print("Enter ZipCode: "); // this will add 
to row[0][0] and row[0][1] because it is separated on line 24  
47.                    ZipCode[i][j] = in.nextInt();  
48.                }  
49.                catch(Exception e)  
50.                {  
51.                    System.out.println();  
52.                    System.out.println(" ---------------|");  
53.                    System.out.println(" |              |");  
54.                    System.out.println(" |              |");  
55.                    System.out.println(" |              |_O");  
56.                    System.out.println(" |               /|\\ ");  
57.                    System.out.println(" |                |");  
58.                    System.out.println(" |              _/ \\_");  
59.                    System.out.println(" |       |                     |")
;  
60.                    System.out.println(" |       |---------------------|")
;  
61.                    System.out.println(" |       |ZipCode is an Integer|
");  
62.                    System.out.println(" |       |---------------------|")
;  
63.                    System.out.println();  
64.                    System.exit(0);  
65.                }  
66.            }  
67.        }             
68.            array();  
69.    }  
70.      
71.    public static void array()  
72.    {  
73.        /*and now we combined the inputted of the user into another row an
d column*/  
74.        array = new String[row][column];   
75.        System.out.println();  
76.          
77.            array[0][0] = town[0][0];  
78.            array[1][0] = town[0][1];  
79.            array[0][1] = Integer.toString(ZipCode[0][0]); //  convert  st
ring to int   
80.            array[1][1] = Integer.toString(ZipCode[0][1]); //  convert  st
ring to int  
81.                      
82.        /* Display the element of the array that we combined and as you ca
n see in the code, instead of having an another 
83.            loop for displaying the array, we output it by manually in ord
er to determine the zipcode of the town*/       
84.        System.out.println("Town: " +array[0][0] + "  Zipcode: " +array[0]
[1]);  
85.        System.out.println("Town: " +array[1][0] + "  Zipcode: " +array[1]
[1]);  
ACTIVITY 09

86.        LinearSearch();  
87.    }  
88.      
89.    public static void LinearSearch()  
90.    {  
91.          
92.        System.out.println();  
93.            System.out.print("Would you like search the element? [y/n]: ")
;  
94.            loc = in.next();  
95.                  
96.                System.out.println();  
97.                if (loc.equalsIgnoreCase("y"))//accepts if the input is y  
98.                {  
99.                do  
100.                 {  
101.                     System.out.println("\t[1] - Town");  
102.                     System.out.println("\t[2] - ZipCode");  
103.                     System.out.println();  
104.                     System.out.println("which element would you like 
to search?");  
105.                     loc = in.next();  
106.                       
107.                     System.out.println();  
108.                     if(loc.equals("1"))//accepts if the input is 1  
109.                     {             
110.                           
111.                         System.out.print("Search town: ");  
112.                         search = in.next();  
113.                         System.out.println();  
114.                           
115.                           
116.                         for (x = 0; x < row; x++)    
117.                         {  
118.                             for(y=0; y<column; y++)  
119.                             {  
120.                                   
121.                                 if (search.equalsIgnoreCase(array[x]
[y])) //accepts if the input are equal to the  element  
122.                                 {  
123.                                     found = true;  
124.                                     found();                         
             
125.                                     break;  
126.                                       
127.                                 }  
128.                                 else  
129.                                 {  
130.                                     found = false;  
131.                                 }  
132.                                   
133.                             }  
ACTIVITY 09

134.                         }  
135.                         if (found == false) //accepts if the input a
re  not equal to the  element  
136.                         {  
137.                             System.out.println("'" + search +"' is n
ot found");  
138.                         }  
139.                           
140.                     }  
141.                     else if(loc.equals("2"))//accepts if the input i
s 2  
142.                     {  
143.                           
144.                         System.out.print("Search Zipcode: ");  
145.                         search = in.next();  
146.                           
147.                         System.out.println();  
148.                           
149.                         for (x = 0; x <row; x++)    
150.                         {  
151.                             for (y = 0; y<column; y++)    
152.                             {  
153.                                   
154.                                 if (search.equalsIgnoreCase(array[x]
[y])) //accepts if the input are equal to the  element  
155.                                 {  
156.                                     found = true;  
157.                                     found();                         
             
158.                                     break;  
159.                                       
160.                                 }  
161.                                 else  
162.                                 {  
163.                                     found = false;  
164.                                 }  
165.                                   
166.                             }  
167.                         }  
168.                           
169.                         if (found == false) //accepts if the input a
re  not equal to the  element  
170.                         {  
171.                             System.out.println("'" + search +"' is n
ot found");  
172.                         }  
173.                           
174.                     }  
175.                   
176.                     else //accepts if the input is not in the choice
s  
177.                     {  
ACTIVITY 09

178.                         System.out.println();  
179.                         System.out.println(" ---------------|");  
180.                         System.out.println(" |              |");  
181.                         System.out.println(" |              |");  
182.                         System.out.println(" |              |_O");  
183.                         System.out.println(" |               /|\\ ")
;  
184.                         System.out.println(" |                |");  
185.                         System.out.println(" |              _/ \\_")
;  
186.                         System.out.println(" |       |               
      |");  
187.                         System.out.println(" |       |--------------
-------|");  
188.                         System.out.println(" |       |   Invalid Ele
ment   |");  
189.                         System.out.println(" |       |--------------
-------|");  
190.                         System.out.println();  
191.                         System.exit(0);  
192.                     }  
193.                       
194.                     System.out.println();  
195.                     System.out.print("Would you like search the elem
ent again? [y/n]: ");  
196.                     searchAgain = in.next();  
197.                 }while(searchAgain.equalsIgnoreCase("y"));  
198.                 }  
199.               
200.                 else //accepts if the input is not y  
201.                 {  
202.                     System.out.println();  
203.                     System.out.println(" ---------------|");  
204.                     System.out.println(" |              |");  
205.                     System.out.println(" |              |");  
206.                     System.out.println(" |              |_O");  
207.                     System.out.println(" |               /|\\ ");  
208.                     System.out.println(" |                |");  
209.                     System.out.println(" |              _/ \\_");  
210.                     System.out.println(" |       |                   
  |");  
211.                     System.out.println(" |       |------------------
---|");  
212.                     System.out.println(" |       |     Program exit  
  |");  
213.                     System.out.println(" |       |------------------
---|");  
214.                     System.out.println();  
215.                     System.exit(0);  
216.                 }  
217.     }  
218.     public static void found()  
ACTIVITY 09

219.     {  
220.         System.out.println("'" + search +"' is Found");  
221.         System.out.println();  
222.         if (search.equalsIgnoreCase(array[0][1]))           //accept
s if the input are equal to the  element  
223.         {  
224.             System.out.println("Town: " +array[0][0] + "  Zipcode: " 
+array[0][1]);  
225.         }  
226.         else if(search.equalsIgnoreCase(array[1][1])) {  
227.             System.out.println("Town: " +array[1][0] + "  Zipcode: " 
+array[1][1]);  
228.         }  
229.         else if(search.equalsIgnoreCase(array[0][0])) {  
230.             System.out.println("Town: " +array[0][0] + "  Zipcode: " 
+array[0][1]);  
231.               
232.         }  
233.         else if(search.equalsIgnoreCase(array[1][0])) {  
234.             System.out.println("Town: " +array[1][0] + "  Zipcode: " 
+array[1][1]);  
235.   
236.         }  
237.           
238.         LinearSearch();  
239.     }  
240.   
241. }  

You might also like