Java 参考方法并检查输入

Java 参考方法并检查输入,java,csv,methods,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.o

我创建了一个方法,用于检查输入是否在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; }}
main方法中,我指的是isValidNumber方法

 // some previous code

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


 //next code

因此,即使userInput是正确的并且存在于csv文件中,我也无法跳出循环。有人能帮我吗?

假设您的问题是您输入的任何内容都是有效的,那么问题就在于
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;      
 }

您的
isValidNumber
总是返回true,这就是您无法跳出循环的原因

试试下面的用法--


请详细说明你的问题facing@DarkKnight对不起,完成了。您能详细说明您从哪一行获得错误以及colmn的定义吗?我没有得到错误,它只是没有退出循环,所以我必须为第一个条件返回true,为第二个条件返回false?正确的?因为我已经试过了,但是在方法的末尾,我也需要返回一些东西。@javach1:我已经根据您的评论更新了我的答案。但是在最后一个括号中,它告诉我它缺少一个return语句。为什么?不客气!另外,我已经编辑了代码,所以现在您可能不会收到任何警告或错误。完美。顺便问一下,我如何检查一个方法是否正确,并继续使用下一个方法。在我的例子中,使用userinput---while(isValidNumeric&&isValidColumn),这样如果我输入文本,它就不能在第一个方法中转换文本。因此,我想先执行一个方法,然后执行另一个
,而(isValidNumber(userInput)和&isValidColumn(userInput1))
,在这里您的第一个方法将被调用,即
isValidNumber(userInput)
,如果它返回true,则只调用下一个方法。但这是一个and条件,它会检查这两个方法。如何检查第一个数字和第二列?
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;}

 }