2019.01.21 예제) 데이터 입력 및 데이터 검색(Map,List)

2019. 1. 22. 09:22JAVA

#1 Location.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.example;
 
import java.util.ArrayList;
import java.util.List;
 
public class Location {
    static List<String> list = new ArrayList<String>();
    private String City;
    private double ww;
    private double jj;
    
    public Location(String City,double ww, double jj){
        super();
        this.City= City;
        this.ww = ww;
        this.jj = jj;
        
        
    }
 
    public String getCity() {
        return City;
    }
 
    public void setCity(String city) {
        City = city;
    }
 
    public double getWw() {
        return ww;
    }
 
    public void setWw(double ww) {
        this.ww = ww;
    }
 
    public double getJj() {
        return jj;
    }
 
    public void setJj(double jj) {
        this.jj = jj;
    }
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append(City).append(" ").append(ww).append(" ").append(jj);
        return builder.toString();
    }
    
}
 
cs





#2 listLocationManager.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.example;
 
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;
 
public class listLocationManager {
 
    public static void main(String[] args) {
        ArrayList<Location> list = new ArrayList<Location>();
        int count = 1;
        while (true) {
            System.out.println("도시이름,경도,위도를 입력하세요");
            Scanner scanner = new Scanner(System.in);
            String str = scanner.nextLine();
 
            StringTokenizer str1 = new StringTokenizer(str, ",");
            String a = str1.nextToken().trim();
            double b = Double.parseDouble(str1.nextToken().trim());
            double c = Double.parseDouble(str1.nextToken().trim());
 
            Location location = new Location(a, b, c);
            list.add(location);
            count++;
            if (count == 5) {
                break;
            }
        } // while
        for (int i = 0; i < list.size(); i++) {
            Location location = list.get(i);
            System.out.println(location);
        } // for
 
        while (true) {
            int num = 0;
            System.out.print("도시 이름 >> ");
            Scanner scanner2 = new Scanner(System.in);
            String str2 = scanner2.nextLine();
            if (str2.equals("그만")) {
                System.out.println("프로그램 종료");
                break;
            }
 
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getCity().equals(str2)) {
                    System.out.println(list.get(i));
                    break;
                }else {
                    num++;
                }
                if( num == 4) {
                    System.out.println(str2+"가 없습니다.");
                }
            }
 
        }
    }
}
 
cs



#3 MapLocationManager.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.example;
 
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;
 
 
public class MapLocaionManager {
 
    public static void main(String[] args) {
        HashMap<String, Location> map= new HashMap<>();
        boolean run= true;
        int count= 1;
        while(run) {
        System.out.println("도시이름,경도,위도를 입력하세요");
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        
        StringTokenizer str1 = new  StringTokenizer(str,",");
        String a = str1.nextToken().trim();
        double b = Double.parseDouble(str1.nextToken().trim());
        double c = Double.parseDouble(str1.nextToken().trim());
        
        Location location = new Location(a, b, c);
        map.put(a, location);
        count++;
        if(count==5) {run=false;}        }
        
        Set<String> keySet = map.keySet();
        Iterator<String> iter = keySet.iterator();
        while (iter.hasNext()) {
            String key = iter.next();
            Location location = map.get(key);
            System.out.println(location);
            
        }
        
        
        run = true;
        while(run) {
            System.out.println("도시검색>>");
            Scanner scanner = new Scanner(System.in);
            String str2 = scanner.nextLine();
            if(str2.equals("그만")) {
                run = false;
                System.out.println("프로그램 종료");
            }
            if(map.containsKey(str2)) {
                System.out.println(map.get(str2));
            }else if(map.containsKey(str2)==false){
                System.out.println(str2+"가 없습니다.");
            }
        }
 
    }
 
}
 
cs




<출력>


'JAVA' 카테고리의 다른 글

2019.01.30 JAVA에서 HTML문서 만들기  (0) 2019.01.30
2018.01.25 JDBC: 데이터베이스 연결  (0) 2019.01.25
2019.01.21 배치처리  (0) 2019.01.22
2019.01.18 파일 읽어오기  (0) 2019.01.18
2019.01.15 JavaFX에서 키보드 동작(액션)  (1) 2019.01.15