我需要从文件中读取多个数组并打印它们。我的第一个类处理一个菜单驱动的程序,用户输入一个数字来告诉程序连接到文件,打印文件中的名称,整数,字符或双打。我坚持的第一件事是连接到文件。这是我从文件中读取的不完整的类:
import java.util.*;
public class Prog5Methods{
public Prog5Methods(){
}
public void ReadFromFile(Scanner input, String [] names, int [] numbers, char [] letters, double [] num2){
System.out.println("\nReading from a file...\n");
System.out.println("\nDONE\n");
int r = 0;
while(input.hasNext()){
names[r] = input.next();
numbers[r] = input.nextInt();
letters[r] = input.next().charAt(0);
num2[r] = input.nextDouble();
r++;
}
} // end of readFromFile
}
这是我正在阅读的文件包含:
Lee Keith Austin Kacie Jason Sherri Jordan Corey Reginald Brian Taray
Christopher Randy Henry Jeremy Robert Joshua Robert Eileen
Cassandra Albert Russell Ethan Cameron Tyler Alex Kentrell rederic
10 20 100 80 25 35 15 10 45 55 200 300 110 120 111 7 27 97 17 37
21 91 81 71 16 23 33 45
A b c w e r t q I u y b G J K S A p o m b v x K F s q w
11.5 29.9 100 200 115.1 33.3 44.4 99.9 100.75 12.2 13.1 20.3 55.5 77.7
12.1 7.1 8.2 9.9 100.1 22.2 66.6 9.9 1.25 3.75 19.9 3.321 45.54 88.8
名称在数组名[]中,整数在数组[]中等。我需要从这些数组中打印每个变量。
答案 0 :(得分:0)
要从文本文件中读取数据, FileReader 是您的最佳选择;
BufferedReader b = new BufferedReader(new FileReader("filename.txt"));
String s = "";
while((s = b.readLine()) != null) {
System.out.println(s);
}
将从文件中读取每一行并一次将其打印到标准输出一行。
import java.io.*;
public class Prog5 {
public static String names[];
public static int integers[];
public static char letters[];
public static float decimals[];
public void readFileContents() {
File f = new File("yourfilename.txt");
byte b = new byte[f.length()];
FileInputStream in = new FileInputStream(f);
f.read(b);
String wholeFile = new String(b);
String dataArray[] = wholeFile.split(" ");
for(int i = 0; i < dataArray.length; i++) {
String element = dataArray[i];
//in here you need to figure out what type element is
//or you could just count a certain number or each type if you know in advance
//then you need to parse it with eg Integer.parseInt(element); for the integers
//and put it into the static arrays
}
}
}
答案 1 :(得分:0)
使用List<T>
代替数组。
使用扫描程序从基础流中读取值
public static void ReadFromFile(Scanner input,
ArrayList<String> names,
ArrayList<Integer> numbers,
ArrayList<Character> letters,
ArrayList<Double> num2)
{
while(input.hasNext())
{
String val=input.next();
Object no=parseInt(val);
if(no!=null) //Is integer?
{
numbers.add((Integer)no);
}
else
{
no=parseDouble(val);
if(no!=null) // Is double?
{
num2.add((Double)no);
}
else
{
no=parseChar(val);
if(no!=null) //Is Char?
{
letters.add((Character)no);
}
else
{
names.add(val); // String
}
}
}
}
}
解析字符串的方法。
public static Integer parseInt(String str)
{
Integer retVal=-1;
try
{
retVal=Integer.parseInt(str);
}catch(Exception ex) { return null;}
return retVal;
}
public static Double parseDouble(String str)
{
double retVal=-1;
try
{
retVal=Double.parseDouble(str);
}catch(Exception ex) { return null;}
return retVal;
}
public static Character parseChar(String str)
{
Character retVal=null;
if(str.length()==1)
retVal=str.charAt(0);
return retVal;
}
测试您的代码
public static void main(String[] args) throws Exception
{
.......
ArrayList<String> names=new ArrayList<String>();
ArrayList<Integer> numbers=new ArrayList<Integer>();
ArrayList<Double> num2=new ArrayList<Double>();
ArrayList<Character> letters=new ArrayList<Character>();
ReadFromFile(input,names,numbers,letters,num2);
System.out.println(names);
System.out.println(numbers);
System.out.println(letters);
System.out.println(num2);
}
答案 2 :(得分:0)
你需要一个围绕文件名的“新文件”来创建一个扫描仪,
p5m.readFromFile (new Scanner (new File("user.data")),
要使用它,您需要java.io:
import java.io.*;
如果我坚持你的要求,使用数组和扫描仪,我可以这样做:
import java.util.*;
import java.io.*;
public class Prog5Methods
{
public void readFromFile (Scanner input, String [] names, int [] numbers, char [] letters, double [] num2)
{
System.out.println("\nReading from a file...\n");
String [][] elems = new String [5][];
for (int i = 0; i < 4; ++i)
{
elems[i] = input.nextLine ().split ("[ \t]+");
}
for (int typ = 0; typ < 4; ++typ)
{
int i = 0;
for (String s: elems[typ])
{
switch (typ)
{
case 0: names [i++] = s; break;
case 1: numbers[i++] = Integer.parseInt (s); break;
case 2: letters[i++] = s.charAt (0); break;
case 3: num2[i++] = Double.parseDouble (s); break;
}
System.out.println (i + " " + typ + " " + s);
}
}
System.out.println("\nDONE\n");
}
public static void main (String args[]) throws FileNotFoundException
{
Prog5Methods p5m = new Prog5Methods ();
p5m.readFromFile (new Scanner (new File("user.data")),
new String [28],
new int [28],
new char [28],
new double [28]);
}
}
问题1:我需要知道,每行有28个元素,我在这里即时打印它们。它们被困在匿名数组中并且从未使用过,但这仅适用于简短的演示。我可以声明数组,然后再打印:
String [] names = new String [28];
int [] numbers = new int [28];
char [] letters = new char [28];
double [] num2 = new double [28];
Prog5Methods p5m = new Prog5Methods ();
p5m.readFromFile (new Scanner (new File("user.data")),
names, numbers, letters, num2);
我仍然需要28个元素。我可以延迟数组的初始化,直到我从读取文件知道,有多少元素。
public String [][] readFromFile (Scanner input)
{
System.out.println("\nReading from a file...\n");
String [][] elems = new String [5][];
for (int i = 0; i < 4; ++i)
{
elems[i] = input.nextLine ().split ("[ \t]+");
}
return elems;
}
public static void main (String args[]) throws FileNotFoundException
{
Prog5Methods p5m = new Prog5Methods ();
String [][] elems = p5m.readFromFile (new Scanner (new File("user.data")));
int size = elems[0].length;
String [] names = new String [size];
int [] numbers = new int [size];
char [] letters = new char [size];
double [] num2 = new double [size];
for (int typ = 0; typ < 4; ++typ)
{
int i = 0;
for (String s: elems[typ])
{
switch (typ)
{
case 0: names [i++] = s; break;
case 1: numbers[i++] = Integer.parseInt (s); break;
case 2: letters[i++] = s.charAt (0); break;
case 3: num2[i++] = Double.parseDouble (s); break;
}
}
}
}
既然所有行都包含相同数量的元素,它们可能是相关的吗?因此,数据集描述了一些信息,代码和xy-quote等用户。在面向对象的土地上,这看起来像一个对象 - 让我们称之为用户:(如果你知道C,它就像一个结构)。
我们写了一个甜蜜的小班:
// if we don't make it public, we can integrate it
// into the same file. Normally, we would make it public.
class User {
String name;
int rep;
char code;
double quote;
public String toString ()
{
return name + "\t" + rep + "\t" + code + "\t" + quote;
}
// a constructor
public User (String name, int rep, char code, double quote)
{
this.name = name;
this.rep = rep;
this.code = code;
this.quote = quote;
}
}
并在主方法结束时使用它:
User [] users = new User[size];
for (int i = 0; i < size; ++i)
{
users[i] = new User (names[i], numbers[i], letters[i], num2[i]);
}
// simplified for-loop and calling toString of User implicitly:
for (User u: users)
System.out.println (u);
我不建议使用Arrays。一个ArrayList会更容易处理,但初学者通常会被他们刚学到的东西所束缚,而且你用数组标记了你的问题......