In this application, i want the user to enter a number, but somehow the conversion of the value entered doesn't work. When I start the application and enter any value I get java.lang.NumberFormatException.How should I change the code??
import java.io.*; import java.lang.*; import java.awt.*; public class Assignment5 { public static void main( String args[] ) throws IOException { final int maxvert = 20; // Maximun number of vertices allowed int n; // Number of vertex in this instance char Ans = 'n'; // Use for loop int w[][]; // Inputted graph represented by matrix int s[][] = new int [maxvert][maxvert]; // Minimum spanning tree graph int nearest[] = new int [maxvert]; // Index of vertex nearest to vi int distance[] = new int [maxvert]; // Weight on edge between vi and the vertex int near = 0; // The index for which distance is the smallest int tmp_nearest, min; File f_in; FileInputStream input; int len; int f_len, max, ch, edge; String str; String oneChar; String infile = "in.dat"; String st = new String(""); do { w = new int [maxvert][maxvert]; // Inputted graph represented by matrix System.out.println("Prims Algorithm!"); System.out.println("This Algorithm finds the Minimum Spanning Tree of a graph."); System.out.println("How many vetices do you want (Max = "+ maxvert +" ) ?"); while((ch = System.in.read()) != '\n') { oneChar = String.valueOf( (char)ch ); st = st.concat(oneChar); } n = Integer.parseInt(st); System.out.println("The vertices = "+n); System.out.println("Enter graph in matrix form, "); System.out.println("(0 means a loop, -1 means on edge)"); for (int x = 0; x < n; x++) for (int y = 0; y < n; y++) { System.out.print("(V "+x+", V "+y+") : "); st = new String(""); while((ch = System.in.read()) != '\n') { oneChar = String.valueOf( (char)ch ); st = st.concat(oneChar); } w[x][y] = Integer.parseInt(st); if (w[x][y] == -1) // We use 1000 instead of infinity or -1 w[x][y] = 1000; } System.out.println(); System.out.println(); //int minus_one = -1; for (int x = 0; x < n; x++) //Formatted output, so it looks neat and lined-up { System.out.print(" "); for (int y = 0; y < n; y++) { if ((w[x][y] >= 100) && (w[x][y] <= 999)) System.out.print(+w[x][y]); if ((w[x][y] >= 10) && (w[x][y] <= 99)) System.out.print(" "+w[x][y]); if ((w[x][y] >= 0) && (w[x][y] <= 9)) System.out.print(" "+w[x][y]); if (w[x][y] == 1000) System.out.print(" -1"); } System.out.println(); } System.out.println("Is this correct ?"); //st = new String(""); Ans = (char)System.in.read(); }while((Ans != 'y') && (Ans != 'Y')); for (int i = 0; i < n; i++) // initialize Spanning tree array for (int j = 0; j < n; j++) { if (i == j) s[j] = 0; else s[j] = 1000; } for (int i = 0; i < n; i++) { nearest = 0; // For all vertices, initialize vi to be the // to be the nearest vertex in the set. distance = w[0]; // initialize the distance from the set // to be the weight on the edge to v1. } for (int z = 1; z <= n - 1; z++) { min = 1000; for (int i = 1; i < n; i++) // Check each vertex to see which on is the nearest { if ((distance >= 0) && (distance < min)) { min = distance; near = i; } } tmp_nearest = nearest[near]; s[near][tmp_nearest] = w[near][tmp_nearest]; //Add it to the set distance[near] = -1; for (int i = 1; i < n; i++) { if (w[near] < distance) { distance = w[near]; // Update its distance nearest = near; } } } System.out.println(); System.out.println("The minimum spanning tree is :"); for (int x = 0; x < n; x++) { System.out.print(" "); for (int y = 0; y < n; y++) { if ((s[x][y] >= 100) && (s[x][y] <= 999)) System.out.print(+s[x][y]); if ((s[x][y] >= 10) && (s[x][y] <= 99)) System.out.print(" "+s[x][y]); if ((s[x][y] >= 0) && (s[x][y] <= 9)) System.out.print(" "+s[x][y]); if (s[x][y] == 1000) System.out.print(" -1"); } System.out.println(); } System.out.println(); } }