Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
629 views
in Technique[技术] by (71.8m points)

arrays - Exception in thread "main" java.util.InputMismatchException.. that's what happens when i try to run the program

package com.company;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    // write your code here
    Scanner input = new Scanner(System.in);
    System.out.println("Enter the number of nodes");
    int nonodes = input.nextInt();
    int[] numNodes = new int[nonodes];
    for (int i = 0; i < numNodes.length; i++) {
        System.out.println("Enter the two connected nodes and the distant between them");
        String node1 = input.next();
        String node2 = input.next();
        double dist = input.nextDouble();
    }


}
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Input mismatch exception usually occurs when you provide an input to your scanner instance with a wrong value.

For example, if you ask for an interger using the scanner.nextInt() and provide a string value instead of an integer value. You would get this type of exception. So please do check input.next() and input.nextInt() lines.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...