简体   繁体   English

如何检查文件中是否包含用户输入的特定数字? (Java)的

[英]How can I check if a file contains a certain number that the user has input? (java)

Hi I'm trying to write a program that will check a designated file for a number that a user has input, however if the number is not present in the file I want the program to loop back to the beginning, how could I do this? 嗨,我正在尝试编写一个程序,该程序将检查指定文件中用户输入的数字,但是如果文件中不存在该数字,我希望程序循环回到开头,我该怎么做?

Here is the mess I've made so far: 这是我到目前为止所做的一团糟:

import java.util.Scanner;

public class Classtest2 {
    public static void main(String[] args) {

    //    //1. class
    Scanner sc = new Scanner(Classtest2.class.getResourceAsStream("trombones.txt"));
    Scanner myScanner = new Scanner(System.in);

    System.out.println("What number would you like to check for?");
    String number = myScanner.nextLine() ;
    String keyWord = number, word;
    int lineNum = 0;

    while(sc.hasNextLine()) {
        word = sc.next();

        if(word.equals(myScanner.nextLine().trim())) {
            System.out.println("The number "+number+ " is there");

            break;
        } else {
            // not found
        }
    } // main
} // class

all you need to do is take input from user and then check is it exist in file .you shouldn't use myScanner.nextLine().trim() inside while loop because then it waits to get user input every loop time .ones you get a number from user ,you should check it in the file .what you did is get user input and then again wait for user to input value again and again 您需要做的就是从用户那里获取输入,然后检查文件中是否存在。您不应该在while循环内使用myScanner.nextLine().trim() ,因为这样一来,它等待每个循环时间获取用户输入。从用户那里获取一个数字,您应该在文件中检查它。您所做的是获取用户输入,然后再次等待用户一次又一次地输入值

fixed code 固定码

   import java.util.Scanner;

    public class Classtest2 {
        public static void main(String[] args) {

        //    //1. class
        Scanner sc = new Scanner(Classtest2.class.getResourceAsStream("trombones.txt"));
        Scanner myScanner = new Scanner(System.in);

        System.out.println("What number would you like to check for?");
        String number = myScanner.nextLine() ;
        int lineNum = 0;

        while(sc.hasNextLine()) {
            word = sc.next();

            if(word.equals(number.trim())) { //here was the problem
                System.out.println("The number "+number+ " is there");

                return;
            } else {

            }
        } 
        System.out.println("not found");  // not found
    } 

the best approach 最好的方法

import java.util.Scanner;

public class Classtest2 {

    public static void main(String[] args) {

        Scanner myScanner = new Scanner(System.in);
        System.out.println("What number would you like to check for?");
        String number = myScanner.nextLine();
        if(isFound(number)){
            System.out.println("The number "+number+ " is there");
        }else{
            System.out.println("The number "+number+ " doesn't exist");
        }
    }

    public static boolean isFound(String number) {

        Scanner sc = new Scanner(Classtest2.class.getResourceAsStream("trombones.txt"));
        String word="";
        while (sc.hasNextLine()) {
            word = sc.next();

            if (word.equals(number.trim())) { 
                return true;
            }
        }
        return false;
    }
}

This works for me, hope is useful for you: 这对我有用,希望对您有用:

package test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

public class Classtest2 {

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

        File f = new File("trombones.txt");
        f.createNewFile();
        Scanner myScanner = new Scanner(System.in);
        boolean numExiste = menu(new Scanner(f), myScanner);
        while (!(numExiste)){
            numExiste = menu(new Scanner(f), myScanner);
        }
        myScanner.close();

    }

    private static boolean menu(Scanner fileScanner, Scanner myScanner) throws FileNotFoundException{
        String word = "";
        System.out.println("What number would you like to check for?");
        String number = myScanner.nextLine().trim();

        while(fileScanner.hasNextLine()){
            word = fileScanner.nextLine();
            if(word.trim().equals(number)){
                System.out.println("The number "+number+ " is there");
                return true;
            }else{
                //Not the number
            }
        }
        System.out.println("The number "+ number+ " is not in the file\n\n");
        return false;

    }
} // main } // class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查 JSON 数组的 object 是否包含 Android Java 中具有特定名称的数字? - How can I check if object of JSON array contains number with certain name in Android Java? 如何检查我的Firebase数据库是否具有特定用户的数据? (Android / Java) - How can I check if my Firebase database has data for a certain user? (Android/Java) 我可以检查 List 是否包含一个对象,该对象具有在 java 中具有特定值的字段吗? - Can I check whether a List contains an object that has fields with a certain value in java? 如何检查字符串是否包含(数字) - How can I check if a string contains (number) 我如何检查用户输入是否不是java中的字符串 - How can i check if the user input is not string in java 如何检查用户输入的字符串是否包含文件扩展名 - How to check if the string the user has entered contains a file extension 如何检查一个数字是否包含某个数字? - How to check if a number contains a certain digit? 在Java中,如何检查输入是否为数字? - In Java, how do I check if input is a number? 如何根据用户输入在java上制作一定数量的数组 - How to make a certain number of arrays on java based on a user input Java:读取文本文件时,如何读取包含特定字符串的特定行? - Java: When reading a text file, how can i read that specific line which contains a certain string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM