Only close input Scanner once

This commit is contained in:
Will Hedgecock 2021-11-16 21:05:38 -06:00
parent e9133adc2c
commit ea23ccd90e
1 changed files with 8 additions and 9 deletions

View File

@ -2,10 +2,10 @@
* SerialPortTest.java * SerialPortTest.java
* *
* Created on: Feb 27, 2015 * Created on: Feb 27, 2015
* Last Updated on: Feb 20, 2020 * Last Updated on: Nov 16, 2021
* Author: Will Hedgecock * Author: Will Hedgecock
* *
* Copyright (C) 2012-2020 Fazecast, Inc. * Copyright (C) 2012-2021 Fazecast, Inc.
* *
* This file is part of jSerialComm. * This file is part of jSerialComm.
* *
@ -32,7 +32,7 @@ import java.util.Scanner;
* This class provides a test case for the jSerialComm library. * This class provides a test case for the jSerialComm library.
* *
* @author Will Hedgecock <will.hedgecock@gmail.com> * @author Will Hedgecock <will.hedgecock@gmail.com>
* @version 2.6.2 * @version 2.8.0
* @see java.io.InputStream * @see java.io.InputStream
* @see java.io.OutputStream * @see java.io.OutputStream
*/ */
@ -91,20 +91,18 @@ public class SerialPortTest
SerialPort ubxPort; SerialPort ubxPort;
System.out.print("\nChoose your desired serial port or enter -1 to specify a port directly: "); System.out.print("\nChoose your desired serial port or enter -1 to specify a port directly: ");
int serialPortChoice = 0; int serialPortChoice = 0;
Scanner inputScanner = new Scanner(System.in);
try { try {
Scanner inputScanner = new Scanner(System.in);
serialPortChoice = inputScanner.nextInt(); serialPortChoice = inputScanner.nextInt();
inputScanner.close();
} catch (Exception e) {} } catch (Exception e) {}
if (serialPortChoice == -1) if (serialPortChoice == -1)
{ {
String serialPortDescriptor = ""; String serialPortDescriptor = "";
System.out.print("\nSpecify your desired serial port descriptor: "); System.out.print("\nSpecify your desired serial port descriptor: ");
try { try {
Scanner inputScanner = new Scanner(System.in); while (serialPortDescriptor.isEmpty())
serialPortDescriptor = inputScanner.nextLine(); serialPortDescriptor = inputScanner.nextLine();
inputScanner.close(); } catch (Exception e) { e.printStackTrace(); }
} catch (Exception e) {}
ubxPort = SerialPort.getCommPort(serialPortDescriptor); ubxPort = SerialPort.getCommPort(serialPortDescriptor);
} }
else else
@ -281,5 +279,6 @@ public class SerialPortTest
System.out.println("Full Line #" + i + ": " + scanner.nextLine()); System.out.println("Full Line #" + i + ": " + scanner.nextLine());
scanner.close(); scanner.close();
System.out.println("\n\nClosing " + ubxPort.getDescriptivePortName() + ": " + ubxPort.closePort());*/ System.out.println("\n\nClosing " + ubxPort.getDescriptivePortName() + ": " + ubxPort.closePort());*/
inputScanner.close();
} }
} }