例1用户需要验证XML文件。我需要检查用户是否验证了XML文件,如果验证= true,我需要显示XML文件的信息,我的问题是,如何检查用户是否验证了XML?
即使我在嵌入2时验证了XML,它也始终是假的。
boolean valid = false;
switch (number)
{
case 1 :
validation();
break;
case 2 :
if (validtrue(valid) == true)
System.out.println("true");// checking
else
System.out.println("please validate the XML file first ");
break;
static void validation()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter the name of te XML file wiht .xml");
String xmlFName = sc.nextLine();
try {
File file = new File(xmlFName); // validate the xml
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(file);
NodeList lst = doc.getElementsByTagName("book");
}catch(Exception e)
{
System.out.print(e.getMessage());
}
System.out.println("the "+ xmlFName +" file has been validated ");
boolean valid = true;
validtrue(valid);
}
static boolean validtrue(boolean valid)
{
if valid == true
return true;
else return false;
}发布于 2020-10-13 08:28:32
如果我正确理解,您有两个菜单选项:一个是用户验证某个内容的菜单,另一个是用户在进行验证时执行一些操作的菜单选项。并且用户应该能够在不执行选项1的情况下转到选项2。
public static void main(String[] args) {
Scanner myInput = new Scanner( System.in );
boolean validated = false;
int option;
while(true){
option = myInput.nextInt();
switch (option) {
case 1:
validated = validation();
break;
case 2:
if (validated){
System.out.println("true");// checking
}
else{
System.out.println("please validate the XML file first ");
}
break;
}
}
}
private static boolean validation(){
return true; //or false
}https://stackoverflow.com/questions/64329812
复制相似问题