Javaの.txtファイルに含まれているチルダ区切りテーブルの行データおよび列データを操作しようとしています。私は正常にスキャン/データを読んだが、リレーショナル代数のアクションをミラーリングするためにそれを操作する方法がわからないし、新しい.txtファイルにチルダ区切りのテーブルとして元のように書き込みますproject()のような別のメソッド。スキャナまたはバッファを使用して.txtファイルを読み込んだ後にJavaでテーブルデータを操作する
//restrict the cars table to toyotas producing a table named toyotas
Algebra.Restrict("cars","MAKE='Toyota'","toyotas");
//project just three columns from the toyotas table producing a table named answer
Algebra.Project("Toyotas","Make,Model,Price","answer");
//display the contents of the answer table
Algebra.Display("answer");
出力は次のようになります:
MAKE MODEL PRICE
----------------
Toyota Camry 18000
Toyota Tacoma 19000
Toyota Highlander 35000
たとえば、私はドライバプログラムの本体は、次のようになりますので、唯一のトヨタ車への出力が制限されますrestrict()
メソッドを作成したいですalgebra.Restrのための次のコードを使用してcars.txt
MAKE~MODEL~TYPE~PRICE
Toyota~Camry~Sedan~18000
Toyota~Tacoma~Truck~19000
Ford~Mustang~Sport~21000
Chevrolet~Corvette~Sport~48000
Ford~F150~Truck~25000
Toyota~Highlander~SUV~35000
からの入力ICT( "フォード"、 ""、 "トラック");:私が取得
public void Restrict(String a, String b, String c)throws FileNotFoundException, IOException{
{
Scanner x = null;
try
{
x = new Scanner(new File("cars.txt"));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
ArrayList<String[]> arr = new ArrayList<String[]>();
while (x.hasNext())
{
String str[] = x.next().split("~");
arr.add(str);
}
for (String[] column : arr)
{
if (column[0].equals(a))
{System.out.println(column[0] + " " + column[1] + " " + column[2]);
// save table to disk
PrintWriter outputfile = new PrintWriter("RestrictTable.txt");
outputfile.print(column[0] + " " + column[1] + " " + column[2]);
outputfile.close();
}
}
for (String[] column : arr)
{
if (column[1].equals(b))
{System.out.println(column[0] + " " + column[1] + " " + column[2]);
// save table to disk
PrintWriter outputfile = new PrintWriter("RestrictTable.txt");
outputfile.print(column[0] + " " + column[1] + " " + column[2]);
outputfile.close();
}
}
for (String[] column : arr)
{
if (column[2].equals(c))
{System.out.println(column[0] + " " + column[1] + " " + column[2]);
// save table to disk
PrintWriter outputfile = new PrintWriter("RestrictTable.txt");
outputfile.print(column[0] + " " + column[1] + " " + column[2]);
outputfile.close();
}
}
System.out.println("NEW TABLE SAVE TO DISK");
}
}
public void Project(String a)throws FileNotFoundException, IOException{
{
Scanner x = null;
try
{
x = new Scanner(new File("cars.txt"));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
ArrayList<String[]> arr = new ArrayList<String[]>();
while (x.hasNext())
{
String str[] = x.next().split("~");
arr.add(str);
}
for (String[] car : arr)
{
if (car[0].equals(a))
{
System.out.println(car[0] + " " + car[1] + " " + car[2]);
// save table to disk
PrintWriter outputfile = new PrintWriter("ProjectTable.txt");
outputfile.print(car[0] + " " + car[1] + " " + car[2]);
outputfile.close();
}
}
System.out.println("NEW TABLE SAVED TO DISK/ProjectTable.txt");
}
}
出力:
Ford Mustang Sport
Ford F150 Truck
Toyota Tacoma Truck
Ford F150 Truck
NEW TABLE SAVE TO DISK
所望の出力
MAKE~MODEL~TYPE
Ford~F150~Truck
NEW TABLE SAVE TO DISK
かで少なくとも
Ford F150 Truck
NEW TABLE SAVE TO DISK
NICE!ありがとう、ちょうど私が変数にデータを取得する方法を把握しようとしていたものです。だからプロジェクトのためには、私は最初の3つのcoulmns –
@SteveRogersええ、私はあなたが表示したいものを選択して選択することができますを投影したいと思うように同じだろうと思います。 'car []' 0-3はすべて車の識別子ですが、すべてを変数に保ちますが、何を出力するかは決めることができます。これがあなたの質問に答えた場合は、回答としてマークしてください。私は本当にポイントを使用することができる私は新しい: – JordanGS
@SteveRogers 'System.out.println(car [0] +" "+ car [1] +" "+ car [2] +" "car [3 ]); 'はすべて3列で、必要のないものを削除します。例えば、 'Camry 18000'と' System.out.println(car [1] + "" + car [3]); 'のような出力は2列目と4列目だけです。 :) – JordanGS