Java 使用ArrayList和扫描仪从文本文件打印特定编号

Java 使用ArrayList和扫描仪从文本文件打印特定编号,java,arraylist,text-files,java.util.scanner,Java,Arraylist,Text Files,Java.util.scanner,我需要通过使用扫描仪键入一个数字,从文本文件中获取一个特定的数字。 程序需要能够将多个数字相加 例如,如果我键入1,那么我将得到数字80。在我输入2之后,我会得到数字85,然后把这两个数字相加。结果80+85=165 我的文本文件如下所示: 1 80 2 85 3 50 我可以打印文本文件中的所有数字并将其输入ArrayList,但我需要打印出一个特定的数字 不要使用数组列表,而是使用它并将其存储在java中的HashMap(键值对)中 HashMap map=newhashmap();

我需要通过使用扫描仪键入一个数字,从文本文件中获取一个特定的数字。 程序需要能够将多个数字相加

例如,如果我键入1,那么我将得到数字80。在我输入2之后,我会得到数字85,然后把这两个数字相加。结果80+85=165

我的文本文件如下所示:

1
80

2
85

3
50

我可以打印文本文件中的所有数字并将其输入ArrayList,但我需要打印出一个特定的数字

不要使用数组列表,而是使用它并将其存储在java中的HashMap(键值对)中

HashMap map=newhashmap();
地图.put(1,80);;
地图.put(2,85);;
//检索值的步骤
map.get(2);//返回85

因此,值的检索非常简单和复杂。

您可以读取所有txt文件数据,并将其以键值对的形式存储到地图中。键将是数字索引,值将是实际数字。然后从映射中获取键并添加它们各自的值。 代码如下所示:

public class NumberRead{
    public static String readFileAsString(String fileName)throws Exception 
    { 
        String data = ""; 
        data = new String(Files.readAllBytes(Paths.get(fileName))); 
        return data; 
    } 

    public static void main(String[] args) throws Exception {
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        String data = readFileAsString("-----Your Numbers.txt Path-----"); 
        String[] split = data.split("\\s+");
        for(int i=0;i<split.length;i++) {
            if(i%2==0) {
                map.put(Integer.parseInt(split[i]), Integer.parseInt(split[i+1]));
            }
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter First Number Index");
        int first = sc.nextInt(); 
        System.out.println("Enter Secound Number Index");
        int second = sc.nextInt();

        if(map.containsKey(first)&&map.containsKey(second)) {
            System.out.println("Addition is: "+((map.get(first))+map.get(second)));
        } else {
            System.out.println("Indexes are not present");
        }
        sc.close();
    }
}

到目前为止你都试过什么?你能使用
地图吗?存储数据会容易得多。另外,一定要分享到目前为止你写的代码。用它来建议解决方案会更容易。正如@NicholasK提到的,分享你迄今为止所写的代码,并就你分享的代码提出一个特定的问题。如果您这样做,将更容易帮助您:)。
public class NumberRead{
    public static String readFileAsString(String fileName)throws Exception 
    { 
        String data = ""; 
        data = new String(Files.readAllBytes(Paths.get(fileName))); 
        return data; 
    } 

    public static void main(String[] args) throws Exception {
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        String data = readFileAsString("-----Your Numbers.txt Path-----"); 
        String[] split = data.split("\\s+");
        for(int i=0;i<split.length;i++) {
            if(i%2==0) {
                map.put(Integer.parseInt(split[i]), Integer.parseInt(split[i+1]));
            }
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter First Number Index");
        int first = sc.nextInt(); 
        System.out.println("Enter Secound Number Index");
        int second = sc.nextInt();

        if(map.containsKey(first)&&map.containsKey(second)) {
            System.out.println("Addition is: "+((map.get(first))+map.get(second)));
        } else {
            System.out.println("Indexes are not present");
        }
        sc.close();
    }
}
1 80
2 85
3 50
4 95
5 75