java 为什么我的接口实现不工作?

fiei3ece  于 2023-04-28  发布在  Java
关注(0)|答案(2)|浏览(184)

我做了一个java项目,它接受了一串数字,然后在接口方法中通过一些动作来产生结果。我在没有实现接口的情况下测试了这个,它工作得很完美,产生了正确的结果。下面是NumberRangeSummarizerImpl的文件(还没有与接口文件交互,工作完美)

import java.util.*;
//import numberrangesummarizer;

public class NumberRangeSummarizerImpl {

    public Collection<Integer> collect(String input) 
    {
        
        // Split the input string into individual number strings and parse them into integers
        String[] numberStrings = input.split(",");
        List<Integer> numbers = new ArrayList<>();
        for (String numberString : numberStrings) {
            try {
                int number = Integer.parseInt(numberString.trim());
                numbers.add(number);
                //numbers are added to an arraylist 
            } catch (NumberFormatException e) {
                // Ignore any non-integer input values t7hfy6h6tg6 number int add the sorted list t
            }
        }
        return numbers;
    }

    
    public String summarizeCollection(Collection<Integer> input) {
        if (input == null || input.isEmpty()) {
            return "Input invalid.";
        }
        // Sort the input numbers ASC
        List<Integer> sortedNumbers = new ArrayList<>(input);
        Collections.sort(sortedNumbers);
        
        // compile output string
        StringBuilder sb = new StringBuilder();
        int startNumber = sortedNumbers.get(0);
        int lastNumber = startNumber;
        sb.append(startNumber);
        for (int i = 1; i < sortedNumbers.size(); i++) {
            int number = sortedNumbers.get(i);
            if (number == lastNumber + 1) {
                // The current number is sequential to the previous number
                lastNumber = number;
            } else {
                // The current number is not sequential to the previous number
                if (lastNumber > startNumber) {
                    // Output a range of numbers instead of just the lastNumber
                    sb.append("-").append(lastNumber);
                }
                sb.append(", ").append(number);
                startNumber = number;
                lastNumber = number;
            }
        }
        // Handle the last range of numbers, if there was one
        if (lastNumber > startNumber) {
            sb.append("-").append(lastNumber);
        }
        return sb.toString();
    }
}

www.example. com (main在哪里)

import java.util.*;
public class App {
    public static void main(String[] args) throws Exception {
        NumberRangeSummarizerImpl summarizer = new NumberRangeSummarizerImpl();
        //Collection<Integer> inputNumbers = summarizer.collect("1,3,6,7,8,12,13,14,15,21,22,23,24,31");
        Collection<Integer> inputNumbers = summarizer.collect("1,1,3,6,7,8,12,13,14,15,21,22,23,24,25,26,27,31");
        String outputString = summarizer.summarizeCollection(inputNumbers);
        System.out.println(outputString);
    }
}

现在我已经在同一个文件夹(src)中创建了一个接口java文件

package numberrangesummarizer;

import java.util.Collection;
public interface NumberRangeSummarizer {

    //collect the input
    Collection<Integer> collect(String input);

    //get the summarized string
    String summarizeCollection(Collection<Integer> input);

}

现在我需要通过在NumberRangeSummarizerImpl中实现来使用该接口

import java.util.*;
import numberrangesummarizer;

public class NumberRangeSummarizerImpl implements NumberRangeSummarizer {

    public Collection<Integer> collect(String input) 
    {
        
        // Split the input string into individual number strings and parse them into integers
        String[] numberStrings = input.split(",");
        List<Integer> numbers = new ArrayList<>();
        for (String numberString : numberStrings) {
            try {
                int number = Integer.parseInt(numberString.trim());
                numbers.add(number);
                //numbers are added to an arraylist 
            } catch (NumberFormatException e) {
                // Ignore any non-integer input values t7hfy6h6tg6 number int add the sorted list t
            }
        }
        return numbers;
    }

    
    public String summarizeCollection(Collection<Integer> input) {
        if (input == null || input.isEmpty()) {
            return "Input invalid.";
        }
        // Sort the input numbers ASC
        List<Integer> sortedNumbers = new ArrayList<>(input);
        Collections.sort(sortedNumbers);
        
        // compile output string
        StringBuilder sb = new StringBuilder();
        int startNumber = sortedNumbers.get(0);
        int lastNumber = startNumber;
        sb.append(startNumber);
        for (int i = 1; i < sortedNumbers.size(); i++) {
            int number = sortedNumbers.get(i);
            if (number == lastNumber + 1) {
                // The current number is sequential to the previous number
                lastNumber = number;
            } else {
                // The current number is not sequential to the previous number
                if (lastNumber > startNumber) {
                    // Output a range of numbers instead of just the lastNumber
                    sb.append("-").append(lastNumber);
                }
                sb.append(", ").append(number);
                startNumber = number;
                lastNumber = number;
            }
        }
        // Handle the last range of numbers, if there was one
        if (lastNumber > startNumber) {
            sb.append("-").append(lastNumber);
        }
        return sb.toString();
    }
}

它调用包,接口与Impl在同一个文件夹中,但它在无法解析NumberRangeSummarizer时抛出错误
我不明白为什么?为什么它不起作用?

0x6upsns

0x6upsns1#

我试图重建您的问题,其中所有三个文件都存在于src文件夹中,没有import numberrangesummarizer;它工作。
您是否尝试删除此声明?
一般来说,它是bad practice to files not in a named package

64jmpszr

64jmpszr2#

我不认为你的import是正确的。这不是正确的语法,应该是import package.Typeimport package.*
另外,假设NumberRangeSummarizerImpl与接口类型在同一个包中,那么根本不需要使用import语句。

相关问题