请参阅方法并检查输入

时间:2016-08-19 06:28:21

标签: java csv methods

我创建了一个方法,用于检查输入是否介于csv文件中列的大小之间。

 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; }}

方法中,我指的是 isValidNumber-Method

 // some previous code

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


 //next code

即使userInput正确并且存在于csv文件中,我也无法离开循环。有人可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

你的isValidNumber总是返回true,这就是你无法摆脱循环的原因。

尝试使用以下 -

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;}

 }

答案 1 :(得分:0)

假设您的问题是您输入的内容有效,则问题出在isValidNumber方法本身:

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; 
 }

这将产生true无论如何,您需要做的是移动您的返回语句。打印完成后,您需要相应地返回true / false

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;
  }
 }

可替换地:

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;      
 }