首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将整数数组转换为集合,然后查找交集

将整数数组转换为集合,然后查找交集
EN

Stack Overflow用户
提问于 2020-10-07 10:18:00
回答 1查看 31关注 0票数 0

这是我正在做的一个项目。请参阅此Imgur链接以查看说明。

https://i.stack.imgur.com/d64zW.png

I have this to work too from but it doesn't seem to be helping me much

这是我正在做的一个寻找两个集合的交集的实验室。我已经尝试了我能想象到的一切。这是我的代码。我认为我的主要问题是:将下面的行数保存为整数,并将字符串数组转换为整数数组,然后将其添加到集合中。

代码语言:javascript
复制
import java.io.IOException;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
import java.util.*;

public class intersectionRunner {
    public static void main(String args[]) throws IOException
    {
Scanner in = new Scanner(new File("intersection.dat"));
int numberOfProjects = Integer.parseInt(in.nextLine()); //proven to be =3
int numLinesBelow=0;
for(int i=0; i<numberOfProjects; i++) {
//proven to run 3 times
    Set<Integer> intersection = new TreeSet<Integer>();
    if(in.nextLine().length()==1) {
        numLinesBelow=in.nextLine();
        System.out.println(numLinesBelow);
    }
    for(int j =0; j< 0; j++) {
        
    }
}
    
    
    //MAIN ENDS
}
    
    
    
    //CLASS ENDS
}

现在我的输出是:Exception in thread "main" java.lang.Error: Unresolved compilation problem: Type mismatch: cannot convert from String to int at intersectionRunner.main(intersectionRunner.java:18)

老实说,这是一场灾难。我被吓坏了,因为我已经尝试了我能想到的一切,但一切都以错误告终。明天就要交了

输入文件如下所示

代码语言:javascript
复制
3
1 2 3 4 5 5
3 4 5 6 7 5
4 5 1 2 3 5 6 7 8 9
5
10 11 15 61 42 14 112 34
12 13 11 61 42 10 34 12 14 112
10 10 13 11 112 54 61 13 13 34
11 112 15 42 13 34 17 10
71 10 11 112 75 63 15 42 34 56 67
4
1 2
3 4 5
6 7 8 9
10 11 12 13 14
EN

回答 1

Stack Overflow用户

发布于 2020-10-14 20:48:51

为了解决这个问题,请不要

代码语言:javascript
复制
numLinesBelow=in.nextLine();

使用

代码语言:javascript
复制
numLinesBelow=in.nextInt();

或者在检查后将字符串解析为int

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64236280

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档