Гость
Целевая тема:
Создать новую тему:
Автор:
Форумы / Java [игнор отключен] [закрыт для гостей] / Проверка на аргументы / 4 сообщений из 4, страница 1 из 1
24.12.2018, 09:45
    #39752052
nastyaa
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Проверка на аргументы
Я не могу добавить третий аргумент, с помощью которого моя программа читает любую кодировку (но по умолчанию должна быть utf 8). И я не могу сделать проверку на аргументы, чтобы были ограничения у меня будет три аргумента, и чтобы если запросить четыре аргумента то программа должна выдать предупреждение, что можно только использовать три аргумента. И чтобы допустим узнать как работает моя программа в командной строке с помощью кода(к примеру help) узнать как работает моя программа сколько в ней аргументов и как каждая работает

Помогите плиз Вот я написала код

Код: 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.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
 
        public class Main {
        public static void main(String[] args) throws  IOException { 
            
        
            
        System.out.println("argument = " + args[0]);
        System.out.println("argument = " + args[1]);  
        
        
        String inputFilePath = args[0];
        String outputFilePath = args[1];
        
        
        //FileReader fileReader = new FileReader(inputFilePath);
        BufferedReader in = new BufferedReader( new InputStreamReader(new FileInputStream(inputFilePath), "UTF-8"));
        File file = new File(outputFilePath);
        FileOutputStream fileOutputStream = new FileOutputStream(file, false);
        
        Writer writer = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8);
        
        String line;
        StringBuilder result = new StringBuilder();
        
        Map<String, String> dictionary = new Dictionary().getDictionary();
        
        while ((line = in.readLine()) != null) {
            String outLine = convertString(line, dictionary) + System.getProperty("line.separator");
            System.out.println(line);
            System.out.println(outLine);
            result.append(outLine) ;
        }
        
        writer.write(result.toString());
        
        in.close();
        writer.flush();
        writer.close();
        //fileReader.close();        
        }
        
 
    public static String convertString(String str, Map<String, String> dictionary) {
        char[] chars = str.toCharArray();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < chars.length; i++) {
            if (dictionary.containsKey(Character.toString(chars[i]))) {
                stringBuilder.append(dictionary.get(Character.toString(chars[i])));
                continue;
            }
            stringBuilder.append(chars[i]);
        }
        return stringBuilder.toString();
    }
}
 
class Dictionary {
    private Map<String, String> dictionary;
 
    public Dictionary() {
        dictionary = new HashMap<>();
        dictionary = new HashMap<>();
        dictionary.put("а", "a");
        dictionary.put("А", "А");
        dictionary.put("&#1241;", "&#225;");
        dictionary.put("&#1240;", "&#193;");
        dictionary.put("б", "b");
        dictionary.put("Б", "B");
        dictionary.put("д", "d");
        dictionary.put("Д", "D");
        dictionary.put("е", "e");
        dictionary.put("E", "E");
        dictionary.put("ф", "f");
        dictionary.put("Ф", "F");
        dictionary.put("г", "g");
        dictionary.put("Г", "G");
        dictionary.put("&#1171;", "&#501;");
        dictionary.put("&#1170;", "&#500;");        
        dictionary.put("х", "h");
        dictionary.put("Х", "H");
        dictionary.put("h", "&#1211;");
        dictionary.put("&#1210;", "&#1210;");
        dictionary.put("і", "i");
        dictionary.put("І", "І");
        dictionary.put("и", "&#305;");
        dictionary.put("И", "I");
        dictionary.put("й", "i");
        dictionary.put("Й", "I");
        dictionary.put("ж", "j");
        dictionary.put("Ж", "J");
        dictionary.put("к", "k");
        dictionary.put("К", "К");
        dictionary.put("л", "l");
        dictionary.put("Л", "L");
        dictionary.put("м", "m");
        dictionary.put("М", "M");
        dictionary.put("н", "n");
        dictionary.put("Н", "N");
        dictionary.put("&#1187;", "&#324;");
        dictionary.put("&#1186;", "&#323;");
        dictionary.put("о", "o");
        dictionary.put("О", "О");
        dictionary.put("&#1257;", "&#243;");
        dictionary.put("&#1256;", "&#211;");
        dictionary.put("п", "p");
        dictionary.put("П", "P");
        dictionary.put("&#1179;", "q");
        dictionary.put("&#1178;", "Q");
        dictionary.put("р", "r");
        dictionary.put("Р", "R");
        dictionary.put("с", "s");
        dictionary.put("С", "S");
        dictionary.put("ш", "sh");
        dictionary.put("Ш", "Sh");
        dictionary.put("ч", "ch");
        dictionary.put("Ч", "Сh");
        dictionary.put("т", "t");
        dictionary.put("Т", "T");
        dictionary.put("&#1199;", "&#250;");
        dictionary.put("&#1198;", "&#218;");
        dictionary.put("&#1201;", "u");
        dictionary.put("&#1200;", "U");
        dictionary.put("в", "v");
        dictionary.put("В", "V");
        dictionary.put("ы", "y");
        dictionary.put("Ы", "Y");
        dictionary.put("у", "&#253;");
        dictionary.put("У", "&#221;");
        dictionary.put("з", "z");
        dictionary.put("З", "Z");
        dictionary.put("э", "e");
        dictionary.put("Э", "E");
        dictionary.put("ю", "&#305;&#253;");
        dictionary.put("Ю", "I&#253;");
        dictionary.put("я", "&#305;a");
        dictionary.put("Я", "Ia");
        
    }
 
    public Map<String, String> getDictionary() {
        return dictionary;
        
    }
}
...
Рейтинг: 0 / 0
24.12.2018, 12:21
    #39752144
Dmitry.
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Проверка на аргументы
nastyaa,

Код: java
1.
2.
3.
4.
5.
6.
7.
if(args.length==3){
    //код для 3-х аргументов
} else if(args.length==2){
    //код для 2-х аргументов
} else {
    //показываем хелп
}
...
Рейтинг: 0 / 0
24.12.2018, 12:47
    #39752165
nastyaa
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Проверка на аргументы
Dmitry.,

Спасибо вам) а как сделать так чтобы предупреждение было в условии. Допустим в моей программе можно только добавлять не больше трех аргументов, если будет 4 или больше аргументов то должно выйти предупреждение, или если меньше двух аргументов тоже должно выйти предупреждение. И как добавить третий аргумент в код, чтобы через него мы могли задавать кодировку (допустим sp12 или utf8) перед тем как прочитать файл, но по умолчанию должно стоять utf 8
...
Рейтинг: 0 / 0
24.12.2018, 13:52
    #39752225
nastyaa
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Проверка на аргументы
Dmitry.,
Вот так правильно?
Код: java
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
  public static void main(String[] args) throws  IOException {
    	final String DEFAULT_ENCODING = "utf-8";
    	
    	if(args.length < 2 || args.length > 3) {
    		System.out.println("Wrong number of arguments.");
    		System.exit(1);
    	}
    	
    	String inputFilePath = args[0];
    	String outputFilePath = args[1];	
    	String fileEncoding = args.length == 2 ? DEFAULT_ENCODING : args[2];
...
Рейтинг: 0 / 0
Форумы / Java [игнор отключен] [закрыт для гостей] / Проверка на аргументы / 4 сообщений из 4, страница 1 из 1
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]