0
对不起,如果有明显的答案,我没有看到它,但我是编程新手。我试图使用一个.txt文件中的标记作为方法中的变量,但最初是在另一个方法中声明的。从txt文件代码行的一个例子是:存储令牌的方法使用另一种方法(无阵列使用)
Lisa 0 0 0 0 464 38 1 6 31 113 298
我的代码从用户收集数据,并找到那名从文本文件,并使用它的数据。我从文本文件中需要的代码只是整数。
public static void name(String n, Scanner NAME){
while(NAME.hasNextLine()){
String name2 = NAME.next();
if(name2.equalsIgnoreCase(n)){
System.out.println("Popularity ranking of name \"" +name2+ "\"");
int num1=0;
for(int i = 0; i<LS; i++){
System.out.println(((i*10)+YEAR)+ ": " + num1);
num1=NAME.nextInt(); //where the tokens i need are
//serving as attention getter
}
}
}
System.out.println("Name not found");
}
public static void intro(){
System.out.println("This program graphs the popularity of a name");
System.out.println("in Social Security baby name statistics");
System.out.println("recorded since the year 1900.");
Scanner console=new Scanner(System.in);
System.out.println("Type a name: ");
String n = console.next();
name(n,NAME);
graphData(NAME);
}
}
在那里我试图使用标记的地方是以下几点:
public static void graphData(Scanner NAME){
while(NAME.hasNextLine()){
int num=NAME.nextInt();
for(int i = 0;i<LS;i++){
g.drawString("" + num,i*WIDTH,30+(num/2)); //part specifically
num.nextInt(); //where im trying to use the tokens
}
}
}
这不是完整的程序,只是我的感觉是必要的。请注意,有四个类常量(YEAR,NAME,LS和WIDTH)。他们不能改变,我不能在这个程序中使用数组。 有关我如何去做的任何想法? 我一直在寻找无处不在,但找不到任何东西。我也尝试了一些东西,例如重写的是令牌在names()
并将其应用于graphData()
。
你可以做一个类和变量存储在类。您将在文件中每行有一个该类的实例。 'graphData'方法也可以在这个类中,这样巡视文件中的每个东西都可以绘制它自己。 – Robert 2014-12-08 03:00:24