Steve Rogers Steve Rogers - 1 month ago 6
Java Question

manipulating table data in Java after reading .txt file using Scanner

I am trying to manipulate row and column data in a table contained in a .txt file in Java. I have successfully scanned/read the data but am not sure how to manipulate it mirror the actions of relational algebra. For example i would like to create a restrict() method that will restrict the output to only Toyotas So the main body of a driver program might look like this:

//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");

output would be:

MAKE MODEL PRICE




Toyota Camry 18000

Toyota Tacoma 19000

Toyota Highlander 35000

input from 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


using the following code

public class readfile {

private Scanner x;

public void openFile(){
try{
x = new Scanner (new File("cars.txt"));
x.useDelimiter("~");
}
catch (Exception e){
System.out.println("where is the file");
}
}

public void readFile(){
while (x.hasNext()){
String a = x.next();
String b = x.next();
String c = x.next();
String d = x.next();

System.out.printf("%s %s %s %s\n", a,b,c,d);
}
}

public void closeFile(){
x.close();
}


}


output i get

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


or another option

public void readData() throws FileNotFoundException, IOException{

final File file = new File("cars.txt");
try (final Scanner scan = new Scanner(file)) {
scan.useDelimiter("~");
while(scan.hasNext()) {
System.out.println(scan.next());

}
}
}

Answer

Step 1) Read the data into a variable and create a data object. The simplest is an array and list, better if you create car class and create a key value pairing but for simplicity here is a potential solution.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class cars
{

    public static void main(String[] args)
    {
        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("Toyota"))
            {
                System.out.println(car[0] + " " + car[1] + " " + car[2] + " " + car[3]);
            }
        }
    }
}

Output:

Toyota Camry Sedan 18000
Toyota Tacoma Truck 19000
Toyota Highlander SUV 35000

I did this in 5minutes but you can easily use it as a template to make methods restrict. Template would be similar to.

private boolean restrictBy (String make){
  if make.equals("Totota") {
    return true;
  } else {
    return false;
  }
}