java - 我的 babyname 函数没有此类元素异常
问题描述
这是我的代码:
import java.awt.*;
import java.util.*;
import java.io.*;
public class BabyNames
{
public static int decades = 11;
public static void main (String [] args) throws FileNotFoundException
{
DrawingPanel panel = new DrawingPanel(550,560);
Graphics g = panel.getGraphics();
panel.setBackground(Color.WHITE);
String line = search();
graph(g,line);
}
public static String search() throws FileNotFoundException
{
Scanner util = new Scanner(new File("names.txt"));
String line = "";
String requestedName = name();
String rankings = "";
String rank = "";
while (util.hasNextLine())
{
line = util.nextLine();
Scanner tracer = new Scanner(line);
String name = tracer.next();
if (name.equalsIgnoreCase(requestedName))
{
for (int x =1;x<=decades;x++)
{
rank = tracer.next();
rankings+=rank+" ";
}
}
if (!util.hasNextLine())
{
System.out.print("no data");
break;
}
}
return rankings;
}
public static String name()
{
String requestName = "";
Scanner console = new Scanner(System.in);
System.out.println("Type a name: ");
requestName = console.next();
return requestName;
}
public static void graph(Graphics g,String rankings)
{
g.setColor(Color.YELLOW);
g.fillRect(0,0,550,30);
g.fillRect(0,530,550,30);
g.setColor(Color.LIGHT_GRAY);
for (int x = 0; x <= 11;x++)
{
g.drawLine(0+50*x,30,0+50*x,530);
}
for (int x = 0;x<=10;x++)
{
g.drawLine(0,30+50*x,550,30+50*x);
}
Scanner console = new Scanner(rankings);
g.setColor(Color.RED);
int position = 0;
int firstRank = console.nextInt();
int position2 = firstRank/2+30;
for (int x = 0; x<=decades-1;x++)
{
int rank = console.nextInt();
if (rank==0)
{
position=530;
}
else
{
position = rank/2+30;
}
g.drawLine(0+x*50-50,position2,0+x*50,position);
position2 = position;
}
}
}
该代码要求输入名称,然后在文件中搜索该名称,然后在另一个窗口中绘制排名图。
该文件的格式如下:
当我运行我的代码时,它没有给我一个这样的元素异常。
完整的错误信息:
java.util.NoSuchElementException at
java.util.Scanner.throwFor(Scanner.java:862) at
java.util.Scanner.next(Scanner.java:1485) at
java.util.Scanner.nextInt(Scanner.java:2117) at
java.util.Scanner.nextInt(Scanner.java:2076) at
BabyNames.graph(BabyNames.java:75) at
BabyNames.main(BabyNames.java:13)
解决方案
看看你的代码:
- 作为用户输入名称
- 查看给定文件并找到以给定名称开头的行并返回其排名
- 在 UI 上显示此排名
在第二你做:
- 查找具有给定名称的字符串
- 分别读取所有整数并用它构建一个字符串(为什么不从文件中读取整个字符串)
- 将此字符串检索回调用方。
在第三你做:
- 接受给定的字符串
- 将此字符串拆分为单独的整数(同样,为什么不接受
List<Integer>
而不是String[] {1, 2, 3}
) - 在 UI 上显示整数
使用后不要忘记关闭 Scanner
,它可能会出现问题。
public static void main(String[] args) throws IOException {
List<Integer> rankings = getRankings(Paths.get("d:/names.txt"));
}
public static List<Integer> getRankings(Path path) throws IOException {
return getNameRankings(getName(), path);
}
private static String getName() {
try (Scanner scan = new Scanner(System.in)) {
System.out.print("Type a name: ");
return scan.next();
}
}
private static List<Integer> getNameRankings(String name, Path path) throws IOException {
String[] data = Files.lines(path)
.map(String::trim)
.map(line -> line.split("\\s+"))
.filter(parts -> parts[0].equalsIgnoreCase(name))
.findFirst()
.orElse(null);
if (data == null)
return Collections.emptyList();
return IntStream.range(1, data.length)
.map(i -> Integer.parseInt(data[i]))
.boxed()
.collect(Collectors.toList());
}
推荐阅读
- ios - 目标 C - 不知道用于选择的类方法
- python - django viewset 扩展 mixins.UpdateModelMixin 但邮递员通过补丁方法更新模型返回'细节:不允许方法补丁'
- python - 我在这个带有 pool.map 的文件中做错了什么,导致什么都没有出现,我必须重新启动 shell?
- html - 第一行和第二行之间有间隙的自定义单选按钮
- vue.js - 注销时清除 nuxt 存储和所有子模块
- c# - 卡在 HttpPost 方法上,试图输入 JSON 文本,但对象应该是“空的”
- aws-lambda - 如何根据请求主体变量/项目执行不同的 aws lambda(aws api 网关)
- bing - 需要防止网站链接显示在 Bing 搜索结果中
- bash - 识别备份目录中的最大目录名称和最大时间戳
- python - 带有 Flask 的 Windows 服务仅在 DEBUG 模式下运行。普通模式似乎崩溃