1

I have created a method that checks if an Input is between the size of column in a csv File.

 public static boolean isValidNumber(String uInput) {

  Long convert = Long.parselong (uInput); 
  int convert2 = (int) convert;// Need to do this because of the JOptionPane
  if(colmn.length > convert) {
  System.out.println("The Column exists.");
   } 
    else { System.out.println("The Column doesn't exists.");}

 return true; }}

And in the main method i refer to the isValidNumber-Method

 // some previous code

 do { String userInput = JOptionPane.showInputDialog);
  } while(isValidNumber(userInput));} 


 //next code

So i can't get out of the Loop even if the userInput is correct and exists in the csv-file. Can someone help me out?

2 Answers2

1

Your isValidNumber always returns true and that's why you are not able to get out of the loop.

Try using below--

public static boolean isValidNumber(String uInput) {

  Long convert = Long.parselong (uInput); 
  int convert2 = (int) convert;// Need to do this because of the JOptionPane
  if(colmn.length > convert) {
  System.out.println("The Column exists.");
   return true;
   } 
    else { System.out.println("The Column doesn't exists."); return false;}

 }
Ashish Patil
  • 4,428
  • 1
  • 15
  • 36
  • But in the last bracket it tells me that it's missing a return Statement. Why? –  Aug 19 '16 at 06:39
  • You welcome ! Also, i have edited code, so probably you will not get any warning or error now. – Ashish Patil Aug 19 '16 at 06:46
  • Perfect. By the way how can i check if one method is true and continue with the next method. In my Case do userinput --- while(isValidNumeric && isValidColumn) So if i Input text it can't convert text in the first method. so i wanna execute first one method and then another –  Aug 19 '16 at 06:56
  • `while(isValidNumber(userInput)&&isValidColumn(userInput1))`, here your first method will get called i.e. `isValidNumber(userInput)` and if it returns true then only next method gets called. – Ashish Patil Aug 19 '16 at 07:05
  • but that's a AND condition and it checks both method. how do i check first number and second Column? –  Aug 19 '16 at 07:07
  • As per [this link](http://stackoverflow.com/questions/7199666/difference-between-and-in-java) "&& evaluates the left side of the operation, if it's true, it continues and evaluates the right side.", so here your first method i.e. `isValidNumber(userInput)` will always get called and evaluates for true or false. If it is false, it will not call `isValidColumn` method – Ashish Patil Aug 19 '16 at 07:10
0

Assuming that your issue is that anything you enter is valid, the problem lies within the isValidNumber method itself:

public static boolean isValidNumber(String uInput) {

  Long convert = Long.parselong (uInput); 
  int convert2 = (int) convert;// Need to do this because of the JOptionPane
  if(colmn.length > convert) {
    System.out.println("The Column exists.");
  } 
  else { 
    System.out.println("The Column doesn't exists.");
  }

  return true; 
 }

This will yield true regardless, what you need to do is to move your return statements. After printing, you will need to return true/false accordingly:

public static boolean isValidNumber(String uInput) {

  Long convert = Long.parselong (uInput); 
  int convert2 = (int) convert;// Need to do this because of the JOptionPane
  if(colmn.length > convert) {
    System.out.println("The Column exists.");
    return true;
  } 
  else { 
    System.out.println("The Column doesn't exists.");
    return false;
  }
 }

Alternatively:

public static boolean isValidNumber(String uInput) {

  Long convert = Long.parselong (uInput); 
  int convert2 = (int) convert;// Need to do this because of the JOptionPane
  if(colmn.length > convert) {
    System.out.println("The Column exists.");
    return true;
  }       

  System.out.println("The Column doesn't exists.");
  return false;      
 }
npinti
  • 51,780
  • 5
  • 72
  • 96
  • So i have to return true for the first condition and false for the second? right? Because i've tried this already but at the end of the method i Need to return something too. –  Aug 19 '16 at 06:35
  • @javach1: I have updated my answer as per your comment. – npinti Aug 19 '16 at 06:37