Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Quick question about parsing in my code /// will gift steam game for help if its allowed

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Quick question about parsing in my code /// will gift steam game for help if its allowed

    I Have a main Vehicletest and I have a vehicle class that i've modified. Now i use FileInputStream to pull data from a "vehicle.txt" but i have no idea how to grab it.
    p.s I tried posting the code but i'm forbidden (probably because i'm a new user)

    --- Update ---

    /*/*
    * To change this license header, choose License Headers in Project Properties.
    * To change this template file, choose Tools | Templates
    * and open the template in the editor.*/
     
     
    /**
     *
     * @author Harebear
    */
        class Vehicle
    {
       private String ownerName;
       private String address;
       private String phone;
       private String email;
     
       public Vehicle(String ownerName, String address, String phone, String email)
       {
           this.ownerName = ownerName;
           this.address= address;
           this.phone = phone;
           this.email = email;
       }
     
     
     
     
     
     
       @Override
       public String toString()
       {
           return String.format("%-15s %-20s %10s %-15s ",ownerName, address, phone, email);
     
     
       }
     
        Object getPhone() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    }
     
    class Car extends Vehicle
    {
       private boolean convertible;
       private String color;
     
       public Car(String ownerName, String address, String phone, String email, boolean convertible, String color)
       {
          super(ownerName, address, phone, email);
          this.convertible = convertible;
          this.color = color;
       } 
     
       public boolean getConvertible()
       {
          return convertible;
       }
     
       public void setConvertible(boolean convertible)
       {
          this.convertible = convertible;
       }
     
       public String getColor()
       {
          return color;
       }
     
       public void setColor(String color)
       {
          this.color = color;
       }
     
       @Override
       public String toString()
       {
           return super.toString() + "\r\n" +
                  "Convertible          : " + convertible + "\r\n" +
                  "Color                : " + color;
       }
     
    }
     
     
    class AmericanCar extends Car
    {
       private boolean madeDetroit;
       private boolean unionShop;
     
       public AmericanCar(String ownerName, String address, String phone, String email, boolean convertible, String color, boolean madeDetroit, boolean unionShop)
       {
          super(ownerName, address, phone, email, convertible, color);
          this.madeDetroit = madeDetroit;
          this.unionShop = unionShop;
       }
     
       public boolean getMadeDetroit()
       {
          return madeDetroit;
       }
     
       public void setMadeDetroit(boolean madeDetroit)
       {
          this.madeDetroit = madeDetroit;
       }
     
       public boolean getUnionShop()
       {
          return unionShop;
       }
     
       public void setUnionShop(boolean unionShop)
       {
          this.unionShop = unionShop;
       }
     
       @Override
       public String toString()
       {
           return super.toString() + "\r\n" +
                  "Made in Detroit      : " + madeDetroit + "\r\n" +
                  "Union shop           : " + unionShop;
       }
     
    }
     
    class ForeignCar extends Car
    {
       private String manufacturerCountry;
       private float importDuty;
     
       public String getManufacturerCountry()
       {
          return manufacturerCountry;
       }
     
       public ForeignCar(String ownerName, String address, String phone, String email, boolean convertible, String color, String manufacturerCountry, float importDuty)
       {
          super(ownerName, address, phone, email, convertible, color);
          this.manufacturerCountry = manufacturerCountry;
          this.importDuty = importDuty;
       }  
     
       public void setManufacturerCountry(String manufacturerCountry)
       {
          this.manufacturerCountry = manufacturerCountry;
       }
     
       public float getImportDuty()
       {
          return importDuty;
       }
     
       public void setImportDuty(float importDuty)
       {
          this.importDuty = importDuty;
       }
     
       @Override
       public String toString()
       {
           return super.toString() + "\r\n" +
                  "Manufacturer country : " + manufacturerCountry + "\r\n" +
                  "Import duty          : " + importDuty;
       }
    }
     
    class Bicycle extends Vehicle
    {
       private int numberSpeeds;
     
       public Bicycle(String ownerName, String address, String phone, String email, int numberSpeeds)
       {
          super(ownerName, address, phone, email);
          this.numberSpeeds = numberSpeeds;
       } 
     
       public int getNumberSpeeds()
       {
          return numberSpeeds;
       }
     
       public void setNumberSpeeds(int numberSpeeds)
       {
          this.numberSpeeds = numberSpeeds;
       }
     
       @Override
       public String toString()
       {
           return super.toString() + "\r\n" +
                  "Number of speeds     : " + numberSpeeds;
       }
    }
     
    class Truck extends Vehicle
    {
       private float numberTons;
       private float truckCost;
       private String datePurchased;
     
       public Truck(String ownerName, String address, String phone, String email, float numberTons, float truckCost, String datePurchased)
       {
          super(ownerName, address, phone, email);
          this.numberTons = numberTons;
          this.truckCost = truckCost;
          this.datePurchased = datePurchased;
       } 
     
       public float getNumberTons()
       {
          return numberTons;
       }
     
       public void setNumberTons(float numberTons)
       {
          this.numberTons = numberTons;
       }
     
       public float getTruckCost()
       {
          return truckCost;
       }
     
       public void setTruckCost(float truckCost)
       {
          this.truckCost = truckCost;
       }
     
       public String getDatePurchased()
       {
          return datePurchased;
       }
     
       public void setDatePurchased(String datePurchased)
       {
          this.datePurchased = datePurchased;
       }
     
       @Override
       public String toString()
       {
           return super.toString() + "\r\n" +
                  "Number of tons       : " + numberTons + "\r\n" +
                  "Truck cost           : " + truckCost + "\r\n" +
                  "Date purchased       : " + datePurchased;
       }
     
    }


    --- Update ---

    // VehicleTest.java
     
    import java.io.*;
    import java.util.*;
    import static jdk.nashorn.internal.objects.NativeJava.typeName;
     
    public class VehicleTest
    {
       public static void main(String[] args) throws IOException 
       {
          ArrayList<Vehicle> vehicles = new ArrayList<Vehicle>();
          FileInputStream in = new FileInputStream("vehicles.txt");
          BufferedReader br = new BufferedReader(new InputStreamReader(in));
          String line;
          String ownerName, address, phone, email, color, manufacturerCountry, datePurchased;
          boolean convertible, madeDetroit, unionShop;     
          float importDuty, numberTons, truckCost;
          int numberSpeeds;
     
          line = br.readLine();
          try
          {
     
             while(line != null)
             { 
     
                ownerName = br.readLine();
                address = br.readLine();
                phone = br.readLine();
                email = br.readLine();
     
                if (line.equals("vehicle"))
                {
                    boolean add = vehicles.add(new Vehicle(ownerName, address, phone, email));
                }
                else if (line.equals("car"))
                {
                   convertible =  Boolean.parseBoolean(br.readLine());
                   color = br.readLine();
                   vehicles.add(new Car(ownerName, address, phone, email, convertible, color));
                }
                else if (line.equals("american car"))
                {
                   convertible =  Boolean.parseBoolean(br.readLine());
                   color = br.readLine();
                   madeDetroit = Boolean.parseBoolean(br.readLine());
                   unionShop = Boolean.parseBoolean(br.readLine());
                   vehicles.add(new AmericanCar(ownerName, address, phone, email, convertible, color, madeDetroit, unionShop));
                }
                else if (line.equals("foreign car"))
                {
                   convertible = Boolean.parseBoolean(br.readLine());
                   color = br.readLine();
                   manufacturerCountry  = br.readLine();
                   importDuty = Float.parseFloat(br.readLine());
                   vehicles.add(new ForeignCar(ownerName, address, phone, email, convertible, color, manufacturerCountry, importDuty));           
                }
                else if (line.equals("bicycle"))
                {
                   numberSpeeds = Integer.parseInt(br.readLine());
                   vehicles.add(new Bicycle(ownerName, address, phone, email, numberSpeeds));   
                }
                else if (line.equals("truck"))
                {
                   numberTons = Float.parseFloat(br.readLine());
                   truckCost = Float.parseFloat(br.readLine());
                   datePurchased = br.readLine();
                   vehicles.add(new Truck(ownerName, address, phone, email, numberTons, truckCost, datePurchased)); 
                }
     
               line =br.readLine();
             }
          }
          catch(Exception e)
          {
             System.out.println(e);
             return;
          }
          finally
          {
             br.close();
             in.close();
          } 
     
          // convert to an array
          Vehicle[] va = new Vehicle[vehicles.size()];
          va = vehicles.toArray(va);
     
          br = new BufferedReader(new InputStreamReader(System.in));
     
          while (true)
          {        
             System.out.println("\n\n               Menu");
             System.out.println("               ----\n\n");
             System.out.println("1. Print all records.\n");
             System.out.println("2. Sort records by email address.\n");
             System.out.println("3. Print number of records.\n");
             System.out.println("4. Print bicycle and truck records.\n");
             System.out.println("5. Print vehicles in area code 904.\n\n");
             int choice = 0;
     
             try
             {
                do
                {
                   System.out.print("Your choice 1-5 or stop to close: ");
                   String input = br.readLine();
                   if (input.toLowerCase().equals("stop")) return;
                   choice = Integer.parseInt(input);
                }
                while(choice < 1 || choice > 5);
     
             }        
             catch(Exception e)
             {
                System.out.println(e);
             }
     
             System.out.println(); 
     
             switch(choice)
             {
                case 1:
                   printAll(va);
                   break;
                case 2:
                   sort(va, true); 
                   break;
                case 3:
                   printNumberRecords(va);
                   break;
                case 4:
                   printBicyclesAndTrucks(va);
                   break;
                case 5:
                   printAreaCode904(va);
                   break;
             }
          }
     
       }   
       private static void printAll(Vehicle[] va)
       {
          for(int i = 0; i < va.length; i++)
          {
             System.out.println("Vehicle type         : " + va[i].getClass().getName());
             System.out.println(va[i].toString());
             System.out.println();
          }
       }
     
       private static void sort(Vehicle[] va, boolean print)
       {
          Vehicle temp;
     
          for(int i = 0;i < va.length - 1;i++)
          {
             for(int j = i + 1; j < va.length;j++)
             {
                if(va[j].getEmail().compareTo(va[i].getEmail()) < 0)
                {
                   temp = va[j];
                   va[j] = va[i];
                   va[i] = temp;
                }
             }
          }
     
          if (print) printAll(va);
       } 
     
     
       private static void printNumberRecords(Vehicle[] va)
       {
          System.out.println("The number of records is " + va.length);
          System.out.println();
       }
     
       private static void printBicyclesAndTrucks(Vehicle[] va)
       {
          sort(va, false); // make sure sorted first
     
          for(int i = 0; i < va.length; i++)
          {
             String typeName = va[i].getClass().getName();
     
             if (typeName.equals("Bicycle") || typeName.equals("Truck"))
             {  
                System.out.println("Vehicle type         : " + typeName);
                System.out.println(va[i].toString());
                System.out.println();
             } 
          }
       }
     
       private static void printAreaCode904(Vehicle[] va)
       {
          for(int i = 0; i < va.length; i++)
          {
             if (va[i].getPhone().equals("904") || typeName.equals("904"))
             {
                System.out.println("Vehicle type         : " + va[i].getClass().getName());
                System.out.println(va[i].toString());
                System.out.println();
             }
          }
     
       }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Quick question about parsing in my code /// will gift steam game for help if its allowed

    but i have no idea how to grab it.
    What does "grab it" mean?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Quick question about parsing in my code /// will gift steam game for help if its allowed

    like "grab" the info from the text file

    --- Update ---

    In the txt file i might have 20 different pieces of data. I run the program, it prompts me for what i'm looking for. I choose all the data from those who have american cars.

    --- Update ---

    I'm guessing it's seeing the data from the vehicles.txt file but it doesn't know how to read them

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Quick question about parsing in my code /// will gift steam game for help if its allowed

    Sorry, I don't know what it means to "grab" info.
    The posted code reads lines from the file into Strings. Is that related to what you want to do?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Quick question about parsing in my code /// will gift steam game for help if its allowed

    Quote Originally Posted by Norm View Post
    The posted code reads lines from the file into Strings. Is that related to what you want to do?
    yes. sorry i know grab isn't the correct terminology

    --- Update ---

    When it trys to read the data it returns zero output

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Quick question about parsing in my code /// will gift steam game for help if its allowed

    There are several calls to the readLine() method in the posted code. Add a println() statement after each of them that prints out what was read into the variable from the readLine() method so you can see what the code is reading.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,312
    My Mood
    Mellow
    Thanks
    194
    Thanked 669 Times in 656 Posts

    Default Re: Quick question about parsing in my code /// will gift steam game for help if its allowed

    When it trys to read the data it returns zero output
    Show the details of this statement with a sample run copied from your console and posted.

Similar Threads

  1. quick question
    By aychveeaysee in forum Java Theory & Questions
    Replies: 1
    Last Post: May 9th, 2014, 05:48 PM
  2. quick question, please HELP!
    By Nemus in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 29th, 2011, 01:23 PM
  3. Quick Question
    By sird00dguyman in forum Java Theory & Questions
    Replies: 2
    Last Post: August 4th, 2011, 06:14 PM
  4. Another Quick Question
    By Jacksontbh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 1st, 2011, 07:18 AM
  5. Question regarding XML Parsing
    By newbie in forum Java Theory & Questions
    Replies: 2
    Last Post: February 24th, 2011, 05:03 AM