Я пытаюсь сделать программу кодирования / декодирования, и я сталкиваюсь со всеми видами исключений здесь!
проблемы, возникающие из-за нескольких / одного сканера / с:
Прежде чем перейти, я хотел бы сказать, что это не дубликат, и яЯ искал множество проблем на StackOverFlow такого рода, и ни одна из них мне не очень помогла. Подобные проблемы, на которые я смотрел: link1 link2
Обратите внимание, что желаемые конечные результаты похожи на результаты первой попытки, но с несколько более чистой обработкой исключенийи закрытые сканеры.
ПЕРВАЯ ПОПЫТКА
Теперь эта программа дает мне желаемые результаты, но это плохое программирование, чтобы иметь два сканера и один из них (метод вводасканер) никогда не закрывается:
public static void main(String[] args) {
Scanner sc=new Scanner (System.in);
int choice = 0;
do {
System.out.println("This program to encode or decode a byte array " +
"\n (o_O) Choices are: " +
"\n 1: Press 1 to enter the encode mode" +
"\n 2: Press 2 to enter the decode mode" +
"\n 3: Press 3 to Exit!");
try {
//it has to be parseInt because if you used sc.nextInt() the program will go nuts even with try catch.
choice=Integer.parseInt(sc.next());
//choice=sc.nextInt();
/*Question: why when i use this with the existing try catch i the program work for ever but when i use Integer.parseInt(sc.nextLine())
* the program would normally ask for another value?
*/
} catch (InputMismatchException | NumberFormatException e) {
System.out.println("invalid type or format!");
} catch (NoSuchElementException e) {
System.out.println("no such");
//break; if i uncomment this the programm will work For Ever
}
switch(choice){
case 1 :
System.out.println("entering the encode mode!");
countAndEncode( input() );
break;
case 2 :
countAndDecode( input() );
break;
case 3 :
System.out.println("exiting...");
break;
default :
System.out.println("please enter a valid option and valid format!");
}
} while (choice!=3);
sc.close();
}
public static byte [] input() {
//arrayList because we dont know the size of the array its like StringBuilder
//ArrayList<Byte> inArray = new ArrayList<Byte>();
//according to StackOverflow using ArrayList to store bytes is inefficient
Scanner inScanner=new Scanner (System.in);
ByteArrayOutputStream inArray= new ByteArrayOutputStream();
System.out.println("enter a sequence of ints please! ");
System.out.println("non-int will terminate the input!");
while (inScanner.hasNext()) {
byte i;
try {
i = inScanner.nextByte();
inArray.write(i);
} catch (InputMismatchException e) {
System.out.println("input terminated!");
break;
}
}
//System.out.println(Arrays.toString(inArray.toByteArray()));
//inScanner.close();
return inArray.toByteArray();
}
ВЫХОД ПЕРВОЙ ПОПЫТКИ:
This is a program to encode or decode bytes based on RLE ALgorithm
(o_O) Choices are:
1: Press 1 to enter the encode mode
2: Press 2 to enter the decode mode
3: Press 3 to Exit!
1
entering the encode mode!
enter a sequence of bytes please!
non-int will terminate the input!
1
1
3
e
input terminated!
[1, 1, 3]
the encoded list is [-1, 1, 2, 3]
This is a program to encode or decode bytes based on RLE ALgorithm
(o_O) Choices are:
1: Press 1 to enter the encode mode
2: Press 2 to enter the decode mode
3: Press 3 to Exit!
At it goes forever without errors.
ВТОРАЯ ПОПЫТКА
т. то, что я сделал после того, как один из вас, ребята, предложил посмотреть на эту проблему ссылка это:
Теперь я не закрыл входной сканер и дал способ ввода сканеру какпараметр:
public static void main(String[] args) {
Scanner sc=new Scanner (System.in);
int choice = 0;
do {
System.out.println("This is a program to encode or decode bytes based on RLE ALgorithm" +
"\n (o_O) Choices are: " +
"\n 1: Press 1 to enter the encode mode" +
"\n 2: Press 2 to enter the decode mode" +
"\n 3: Press 3 to Exit!");
try {
//it has to be parseInt because if you used sc.nextInt() the program will go nuts even with try catch.
choice=Integer.parseInt(sc.next());
//choice=sc.nextInt();
/*Question: why when i use this with the existing try catch i the program work for ever but when i use Integer.parseInt(sc.nextLine())
* the program would normally ask for another value?
*/
} catch (InputMismatchException | NumberFormatException e) {
System.out.println("invalid type or format!");
} catch (NoSuchElementException e) {
System.out.println("no such");//TODO SOLVE IT PLEASE ITS DRIVING ME CRAZYYYYYYYYYYY!!!!!!!
break;
}
switch(choice){
case 1 :
System.out.println("entering the encode mode!");
countAndEncode( input(sc) );
break;
case 2 :
//countAndDecode( input(sc) );
break;
case 3 :
System.out.println("exiting...");
break;
default :
System.out.println("please enter a valid option and valid format!");
}
} while (choice!=3);
sc.close();
}
/**
* with this method user will be able to give the desired sequence of bytes.
* @return a byte array to be encoded.
*/
public static byte [] input(Scanner inScanner) {
//arrayList because we dont know the size of the array its like StringBuilder
//ArrayList<Byte> inArray = new ArrayList<Byte>();
//according to StackOverflow using ArrayList to store bytes is inefficient
//Scanner inScanner=new Scanner (System.in);
ByteArrayOutputStream inArray= new ByteArrayOutputStream();
System.out.println("enter a sequence of bytes please! ");
System.out.println("non-int will terminate the input!");
while (inScanner.hasNext()) {//TODO THIS MIGHT BE THE REASON FOR THE above "SUCH"
byte i;
try {
i = inScanner.nextByte();
inArray.write(i);
} catch (InputMismatchException e) {
System.out.println("input terminated!");
break;
}
}
System.out.println(Arrays.toString(inArray.toByteArray()));
//inScanner.close(); dont close it because it cant be re-opened
return inArray.toByteArray();
}
Это не дает мне желаемых результатов:
После выбора одного из них для кодирования и получения закодированных байтов я застрянунавсегда в режиме кодирования, и условие InputMismatchException | NumberFormatException
будет активировано, поэтому я не могу получить шанс выбрать новый вход!
Это программа для кодирования или декодирования байтов на основе алгоритма RLE (o_O). : 1: нажмите 1 для входа в режим кодирования 2: нажмите 2 для входа в режим декодированияде 3: нажмите 3, чтобы выйти! 1 вход в режим кодирования! введите последовательность байтов, пожалуйста! non-int прекратит ввод! 1 е ввод прекращен! 1 закодированный список: 1 Это программа для кодирования или декодирования байтов на основе алгоритма RLE (o_O). Варианты: 1: нажмите 1, чтобы войти в режим кодирования 2: нажмите 2для входа в режим декодирования 3: нажмите 3 для выхода! неверный тип или формат! вход в режим кодирования! введите последовательность байтов, пожалуйста! non-int прекратит ввод!
ПРИМЕЧАНИЯ:
- 1.комментарий
sc.close()
в основном вызвал ту же ошибку, что и выше .. - 2. то, что перемещение сканера над основным и объявление его в качестве глобальной статической переменной сделало то же самое, что и результаты, вышедшие из строя выше.
ТРЕТЬЯ ПОПЫТКА
Теперь я оставил оба закрытых сканера, и это активировало NoSuchElementException
в основном. Посмотрите:
public static void main(String[] args) {
Scanner sc=new Scanner (System.in);
int choice = 0;
do {
System.out.println("This is a program to encode or decode bytes based on RLE ALgorithm" +
"\n (o_O) Choices are: " +
"\n 1: Press 1 to enter the encode mode" +
"\n 2: Press 2 to enter the decode mode" +
"\n 3: Press 3 to Exit!");
try {
//it has to be parseInt because if you used sc.nextInt() the program will go nuts even with try catch.
choice=Integer.parseInt(sc.next());
//choice=sc.nextInt();
/*Question: why when i use this with the existing try catch i the program work for ever but when i use Integer.parseInt(sc.nextLine())
* the program would normally ask for another value?
*/
} catch (InputMismatchException | NumberFormatException e) {
System.out.println("invalid type or format!");
} catch (NoSuchElementException e) {
System.out.println("no such");//TODO SOLVE IT PLEASE ITS DRIVING ME CRAZYYYYYYYYYYY!!!!!!!
break;
}
switch(choice){
case 1 :
System.out.println("entering the encode mode!");
countAndEncode( input() );
break;
case 2 :
//countAndDecode( input() );
break;
case 3 :
System.out.println("exiting...");
break;
default :
System.out.println("please enter a valid option and valid format!");
}
} while (choice!=3);
sc.close();
}
/**
* with this method user will be able to give the desired sequence of bytes.
* @return a byte array to be encoded.
* @throws IOException
*/
public static byte [] input() {
//arrayList because we dont know the size of the array its like StringBuilder
//ArrayList<Byte> inArray = new ArrayList<Byte>();
//according to StackOverflow using ArrayList to store bytes is inefficient
Scanner inScanner=new Scanner (System.in);
ByteArrayOutputStream inArray= new ByteArrayOutputStream();
System.out.println("enter a sequence of bytes please! ");
System.out.println("non-int will terminate the input!");
while (inScanner.hasNext()) {//TODO THIS MIGHT BE THE REASON FOR THE above "SUCH"
byte i;
try {
i = inScanner.nextByte();
inArray.write(i);
} catch (InputMismatchException e) {
System.out.println("input terminated!");
break;
}
}
System.out.println(Arrays.toString(inArray.toByteArray()));
inScanner.close();
return inArray.toByteArray();
}
в этой попытке я, по крайней мере, мог бы знать, что вызывает NoSuchElementException
подпрыгнуть, и я думаю, что это потому, что закрытие одного сканера закроет поток ввода для всего кода. (поправьте меня, если я ошибаюсь!)
ВЫВОД ДЛЯ ТРЕТЬЕЙ ПОПЫТКИ:
This is a program to encode or decode bytes based on RLE ALgorithm
(o_O) Choices are:
1: Press 1 to enter the encode mode
2: Press 2 to enter the decode mode
3: Press 3 to Exit!
1
entering the encode mode!
enter a sequence of bytes please!
non-int will terminate the input!
-1
-1
e
input terminated!
[-1, -1]
the encoded list is [-1, -1, -1, -1]
This is a program to encode or decode bytes based on RLE ALgorithm
(o_O) Choices are:
1: Press 1 to enter the encode mode
2: Press 2 to enter the decode mode
3: Press 3 to Exit!
no such
РЕШЕНИЕ ДЛЯ ОБСУЖДЕНИЯ @ Villat
Прежде всего большое спасибо вам, мужик, за помощь и вложение времени и усилий. Теперь у меня небольшой вопрос по поводу этих строк:
if(sc.hasNextInt()) choice=sc.nextInt();
else {
sc.next();
continue;
}
error = false;
- Итак, позвольте мне увидеть, правильно ли я понял, эти строки играют роль меры предосторожности, и, пожалуйста, исправьте меня, если я ошибаюсь! чтобы исключение всплыло правильно.
Так не достаточно ли написать следующее исключение блоков try-catch
, потому что NoSuchElementException
не имеет шансов появиться, и InputMismatchException
обрабатываетсяи предотвращается блоком else:
while (error){
if(sc.hasNextInt()) choice=sc.nextInt();
else {
sc.next();
continue;
}
error = false;
}
Просто для обучения недобросовестных целей, если бы я хотел обработать эту ошибку с помощью блока try-catch
, вы бы посчитали ее чистой и невосприимчивой к исключениям, если бы я написал ее так: (отказ от NumberFormatException
)
- так что Демонстрация Handle variant
вашего ответа будет ли это так?
while (error){
try {
choice=sc.nextInt();
error = false;
} catch (InputMismatchException /*| NumberFormatException*/ e) {
error = false;
//System.out.println("invalid type or format!");
sc.next();
continue;
}
}