Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从txt exercise进行二进制搜索_Java_Arrays_Search_Binary - Fatal编程技术网

Java 从txt exercise进行二进制搜索

Java 从txt exercise进行二进制搜索,java,arrays,search,binary,Java,Arrays,Search,Binary,编写一个程序,从文本文件中读取整数,然后对数字进行排序。然后使用笔记中的二进制搜索算法(第6周),要求用户输入一个数字,如果该数字不在列表中,则显示该数字的位置或未找到的位置 到目前为止,我的代码是: import java.util.Arrays; public class myBinarySearch { public static void main(String[] args) { int i; int target, found;

编写一个程序,从文本文件中读取整数,然后对数字进行排序。然后使用笔记中的二进制搜索算法(第6周),要求用户输入一个数字,如果该数字不在列表中,则显示该数字的位置或未找到的位置

到目前为止,我的代码是:

import java.util.Arrays;

public class myBinarySearch
{
    public static void main(String[] args)
    {
        int i;
        int target, found;
        int[] numArray = {36, 27, 29, 15, 16, 39, 11, 31};

        Arrays.sort(numArray);
        for(i=0; i <numArray.length; i++)
        System.out.print(numArray[i] + " ");

        System.out.print("\nEnter the number you are searching for: ");
        target = ReadKb.getInt();
        found = theBinarySearch(numArray, target);
        if(found == -1)
        System.out.println("Number not found");
        else
        System.out.println("Number: " +target +" found at position: " +found);
    }

    //Method searching using binary search algorithm
    private static int theBinarySearch(int []numArray, int target)
    {
        int mid,bottom,top;
        mid=0;
        bottom=0;
        top=numArray.length-1;
        while (top>= bottom)
        {
            mid=(top +bottom)/2;
            if(target==numArray[mid])
                break;
            else
                if(target<numArray[mid])
                    top=mid-1;
                else
                    bottom=mid+1;
        }

        if(target==numArray[mid])
            return mid;
        else
            return -1;
    }

}
导入java.util.array;
公共类myBinarySearch
{
公共静态void main(字符串[]args)
{
int i;
int目标,已找到;
int[]努马拉={36,27,29,15,16,39,11,31};
Arrays.sort(numArray);
对于(i=0;i=bottom)
{
中间=(顶部+底部)/2;
如果(目标==numArray[mid])
打破
其他的

如果(target您已经为
binarysrach
编写了代码,那么惟一缺少的就是从文件中读取它

如何从文件中读取数字

试试这个

    ....
    List<Integer> list = new ArrayList<Integer>();

    Scanner scanner = new Scanner(new File("resources/abc.txt"));
    while (scanner.hasNextInt()) {
        list.add(scanner.nextInt());
    }

    Integer[] numArray = list.toArray(new Integer[] {});

    Arrays.sort(numArray);
    ....
。。。。
列表=新的ArrayList();
Scanner Scanner=new Scanner(新文件(“resources/abc.txt”);
while(scanner.hasNextInt()){
添加(scanner.nextInt());
}
整数[]numArray=list.toArray(新整数[]{});
Arrays.sort(numArray);
....

以下是完整的代码:

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class myBinarySearch {
    public static void main(String[] args) throws Exception {
        int i;
        int target, found;
        // int[] numArray = { 36, 27, 29, 15, 16, 39, 11, 31 };

        List<Integer> list = new ArrayList<Integer>();

        Scanner scanner = new Scanner(new File("resources/abc.txt"));
        while (scanner.hasNextInt()) {
            list.add(scanner.nextInt());
        }

        Integer[] numArray = list.toArray(new Integer[] {});

        Arrays.sort(numArray);
        for (i = 0; i < numArray.length; i++)
            System.out.print(numArray[i] + " ");

        System.out.print("\nEnter the number you are searching for: ");
        target = new Scanner(System.in).nextInt();
        found = theBinarySearch(numArray, target);
        if (found == -1)
            System.out.println("Number not found");
        else
            System.out.println("Number: " + target + " found at position: " + found);
    }

    // Method searching using binary search algorithm
    private static int theBinarySearch(Integer[] numArray, int target) {
        int mid, bottom, top;
        mid = 0;
        bottom = 0;
        top = numArray.length - 1;
        while (top >= bottom) {
            mid = (top + bottom) / 2;
            if (target == numArray[mid])
                break;
            else if (target < numArray[mid])
                top = mid - 1;
            else
                bottom = mid + 1;
        }

        if (target == numArray[mid])
            return mid;
        else
            return -1;
    }

}
导入java.io.File;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.List;
导入java.util.Scanner;
公共类myBinarySearch{
公共静态void main(字符串[]args)引发异常{
int i;
int目标,已找到;
//int[]努马拉={36,27,29,15,16,39,11,31};
列表=新的ArrayList();
Scanner Scanner=new Scanner(新文件(“resources/abc.txt”);
while(scanner.hasNextInt()){
添加(scanner.nextInt());
}
整数[]numArray=list.toArray(新整数[]{});
Arrays.sort(numArray);
对于(i=0;i=底部){
中间=(顶部+底部)/2;
如果(目标==numArray[mid])
打破
否则如果(目标
您尚未定义ReadKB。在我的系统上,它显示:

mohit@mohit:~/somewhere$ javac myBinarySearch.java 
myBinarySearch.java:16: error: cannot find symbol
        target = ReadKb.getInt();
                 ^
  symbol:   variable ReadKb
  location: class myBinarySearch
选择某物作为

Scanner in = new Scanner();
and target = in.nextInt();

OP希望通过这种方式来实现新扫描仪(System.in).nextInt()的
。阅读用户的输入。您再次明确地帮助了我度过了难关!谢谢您继续出色的工作,谢谢您的帮助。很奇怪去年的cert 4是视觉基础现在今年的高级文凭直接进入Java谢谢您的帮助非常感谢您的好意