-1

How to validate this method specially

    System.out.println("Enter number of vertices");
    n=Integer.parseInt(br.readLine());

this. please help me

public static void main(String[] args) throws NumberFormatException, IOException {

    BufferedReader br = new BufferedReader ( new InputStreamReader (System.in));

    System.out.println("Enter number of vertices");
    n=Integer.parseInt(br.readLine());

    graph2 g = new graph2(n);
    g.createGraph();

    System.out.println("\nEnter the source node:");
    int s = g.getNumber();
    System.out.println("\nEnter the target node:");
    int t = g.getNumber();
    long start = System.currentTimeMillis();
      algomain2 ff = new  algomain2(g,s,t);

    System.out.println("\nFinal Residual Network with no augmenting path");

    System.out.print("\nCapacity Flow matrix for your graph is :\n");
    for (int i=1;i<=n;i++)
    {
        for (int j=1;j<=n;j++)
        System.out.print("     "+ff.f[i][j]+"/"+g.cf[i][j]);
        System.out.print("\n");
    }

    int maxflow=0;
    long end = System.currentTimeMillis();
    System.out.println("\n Max flow is: ");
    for(int i=1; i<=n;i++)
        if(g.adj[s][i]==1)
            maxflow=maxflow + ff.f[s][i];

    System.out.println(maxflow);
    System.out.println("Time taken (ms):"+ (end-start));

}
qwertyuio
  • 159
  • 3
  • 9
  • Could you please tell in detail like what you actually want to get? – S.Mandal Mar 29 '20 at 06:37
  • @S.MANDAL I want to put only numbers without any characters – qwertyuio Mar 29 '20 at 06:40
  • @qwertyuio does this help? https://stackoverflow.com/questions/5439529/determine-if-a-string-is-an-integer-in-java – ajc2000 Mar 29 '20 at 06:55
  • Does this answer your question? [Determine if a String is an Integer in Java](https://stackoverflow.com/questions/5439529/determine-if-a-string-is-an-integer-in-java) – ajc2000 Mar 29 '20 at 06:57

1 Answers1

1

Since you are using int n=Integer.parseInt(br.readLine);, Integer.parseInt() always tries to convert your input to the integer one, and assign to integer variable. You don't have to worry about it, as Integer.parseInt() method internally does this.

And as you throw NumberFormatException in method signature, if Integer.parseInt() does not get integer value or something that it can't convert to integer, the NumberFormatException exception will be thrown.

So, if you give number input, n will store the input and you program runs well. If you give input except number (means something that contains character), code will give exception and terminate the execution.

To handle it, you can put your code inside try-catch block to make it properly handle. Like below

try{

    System.out.println("Enter number of vertices");
    n=Integer.parseInt(br.readLine());
}
catch(NumberFormatException e){

    System.out.println("Please enter number only");
}

Or you can make some method to check whether the input is integer or not. If it is integer, return Integer.parseInt(param), else return false. And inside this method, just put your code (basically the above code snippet will be there with proper return statement)

S.Mandal
  • 172
  • 1
  • 10