The Artima Developer Community
Sponsored Link

Java Answers Forum
Where does it go wrong??

2 replies on 1 page. Most recent reply: Jun 8, 2002 11:39 PM by Thomas Dautzenberg

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Thomas Dautzenberg

Posts: 7
Nickname: dautz99
Registered: Jun, 2002

Where does it go wrong?? Posted: Jun 7, 2002 1:30 PM
Reply to this message Reply
Advertisement
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();
}
}


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Where does it go wrong?? Posted: Jun 8, 2002 9:19 PM
Reply to this message Reply
Thomas, could you re-post the code using the [java] tags? I, for one, will be more inclined to look at it if it is a little more readable.

Thomas Dautzenberg

Posts: 7
Nickname: dautz99
Registered: Jun, 2002

Re: Where does it go wrong?? Posted: Jun 8, 2002 11:39 PM
Reply to this message Reply
I know the answer already, I should use
n = Integer.parseInt(st.trim());

instead of

n = Integer.parseInt(st);

Flat View: This topic has 2 replies on 1 page
Topic: DYNAMIC BINDING Previous Topic   Next Topic Topic: Java Security Question

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use