Results 1 to 10 of 10
  1. #1
    ajw1993 is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default Recording data , so you know whats next

    I have a ptoblem i have 3 types of data in one file for example car data , van data , truck data. The problem is i have to have a flag to check what data is next and set a variable , so we know what object to create and store.

    Each data says with a "[" , followed by either a C , V ([Car data])
    Java Code:
      else if(lineOfText.startsWith("[V") && (lineOfText.startsWith("[T"))) // flag
            
            {
                lineOfText = null; //setting the variable to record the data
            }
            else
            {
                    Scanner scanner2 = new Scanner(lineOfText);
                    scanner2.useDelimiter("[ ]*,[ ]*");
                  
                    Vehicle newV = new Car();
                    newV.readData(scanner2); // Read in the line of data.
                    vehicleList.add(newV); // Store in the array.
                    
                    scanner2.close();
                    //System.out.println(lineOfText);
             }
         }
            
           scanner.close();
        }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: Recording data , so you know whats next

    You still didn't adequately explained the problem. What are you looking for? What have you tried?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    ajw1993 is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default Re: Recording data , so you know whats next

    ive tried the code above and also with it being startsWith([) and not sure if its right or not

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: Recording data , so you know whats next

    Okay. I am assuming lineOfText is a String since startsWith is a String class method. Anyway, your logic is wrong. The string can't start with both tokens (i.e. &&) so you need to change it to one or the other (e.g. ||).

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    ajw1993 is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default Re: Recording data , so you know whats next

    well each data set starts with [ . so it will be the startsWith.([)

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: Recording data , so you know whats next

    But that is not what you have. Look at the following:

    Java Code:
    else if(lineOfText.startsWith("[V") && (lineOfText.startsWith("[T")))
    What this says is:

    If lineOfText starts with "[V" and lineofText starts with "[T" then do something. It is impossible for any given line to begin with different strings at the same time. It can only be one or the other. So you need to replace the && with ||.


    Java Code:
    else if(lineOfText.startsWith("[V") || (lineOfText.startsWith("[T")))
    However, since your code is incomplete I can't tell if this will do what you want it too or not in the context of the entire program.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    ajw1993 is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default Re: Recording data , so you know whats next

    well its like that because there is different types of data that have to be stored car , van n truck . but they all start with [followed by the data thatss next.
    i have to read in the file n store the data that the data is required with.
    Attached Files Attached Files

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: Recording data , so you know whats next

    I understand that and I looked at your data. But I do not believe you are understanding what I am saying. Here is a suggestion. I want you to run this program and try typing in [T or [V to see if it prints the word, "Match." It never will. And that is what you are doing above. I can probably help you fix it but you first need to understand the error and why it is happening.

    Java Code:
    import java.util.Scanner;
    
    public class Example1 {
          public static void main (String [] args) {
                 Scanner scanner = new Scanner(System.in);
                 do {
                        System.out.print("Enter line: ");
                        String line = scanner.nextLine();
                        
                        if (line.startsWith("[T") && line.startsWith("[V")) {
                                  System.out.println("Match");
                        } else if (line.equalsIgnoreCase("quit")) {
                                break;
                        } else {
                                  System.out.println("No Match");
                        }
                    } while (true);
                 System.out.println("Done!");
              }
    }



    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    ajw1993 is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default Re: Recording data , so you know whats next

    i get why it doesnt work , but i wouldd like to make it simple step by step . firstly how to store the car data from there i would know what to do for the rest

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: Recording data , so you know whats next

    OK.


    1. First you need to read in the data from the file and see if the first character is a [ (square bracket).
    2. Then determine what kind of data by looking at the next character (C, T, or V).
    3. Then read in and store in appropriate place until another [ is found in the first position. If end of file, then process stored data.
    4. Otherwise go back to step 2.
    5. Don't forget to ignore commnets in your file (anything which starts with //)



    Note. When you read in the line of data, I recommend you store it in a class file named something like Vehicle. Use your comma delimited scanner to parse the data. Depending on what you have to do with the data you can store each column as a String or as indviidual types (String, double, int, etc) as instance fields in the class. Each Vehicle class instance can be stored in an array. You could even subclass the Vehicle class (if you have experience do that) and make a Van, Car, and Truck class. There are many ways to do it.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 1
    Last Post: 03-11-2013, 07:24 PM
  2. Recording a call in java
    By raeesaa in forum Networking
    Replies: 0
    Last Post: 01-07-2013, 08:40 AM
  3. Recording audio/video
    By SuperPokeunicorn in forum New To Java
    Replies: 1
    Last Post: 07-13-2012, 01:53 AM
  4. Recording Sound and Filtering
    By greg_greg in forum Advanced Java
    Replies: 0
    Last Post: 09-20-2011, 04:13 PM
  5. Applet throws exception while recording
    By Basit56 in forum Java Applets
    Replies: 1
    Last Post: 08-20-2009, 01:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •