NoSuchelementException und vorzeitiger Abschluss des ScannersJava

Java-Forum
Anonymous
 NoSuchelementException und vorzeitiger Abschluss des Scanners

Post by Anonymous »

Ich habe ein Programm erstellt, das 5 Ganzzahlen liest und seine Polarität (positiv oder negativ), seinen Typ (ungerade oder sogar) ausgibt. Verwendet scanner.nextline (), um die Eingabe als Zeichenfolge zu lesen und mit .Split ("\ s+") zu teilen und jeden Eintrag in eine Ganzzahl in a for Loop formulierte. Für Fehlerbehandlungen, als ich versuchte, einen nicht integeren Eingang wie ein Zeichen einzuführen. Der Scanner.Nextline () wird vorzeitig geschlossen und wirft eine NoSuchelementException aus.

Code: Select all

import java.util.Scanner;

public class Checker {
public static class outputVal{
private String polarity, type;

public outputVal(String x, String y){
this.polarity = x;
this.type = y;
}
public String getPolarity() {
return polarity;
}
public String getType() {
return type;
}
}
public static int[] getInput(){
Scanner scanner = new Scanner(System.in);
try{
String input = scanner.nextLine();
String [] inputTokens  = input.split("\\s+");
int [] intTokens  = new int[5];
for(int i=0; i0 ? "Positive" : "Negative";
}
}

public static String evenOdd(int x){
return x%2 ==0 ? "Even" : "Odd";
}

public static String checkValue(int[] x){
if(x.length x[(x.length)-1] ? "Greater" : "Lesser";
}
}

public static void main(String[] args) {
int[] input = new int[5];

while(input.length == 0){
System.out.print("Enter five integers: ");
input = getInput();
if(input.length == 0 )System.out.println("[ERROR] Inappropriate Data. Please try again.");
}

if(input.length

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post