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 22 of 22

Thread: Recording data , so you know whats next

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Recording data , so you know whats next

    have to have a flag to check what data is next
    Can you explain what problem you are having detecting what type of data is next? Post a sample of the input file and explain the contents/format of what is on each line in the file.
    else if(lineOfText.startsWith("[V") && (lineOfText.startsWith("[T"))
    This will never be true because a line can not start with two different Strings. It could start with one OR the other.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Recording data , so you know whats next

    there is no problem yet because i just wanted to get this bit right first and it is else if(lineOfText.startsWith("[")

    just wondering what should i put

    --- Update ---

    there is no problem yet because i just wanted to get this bit right first and it is else if(lineOfText.startsWith("[")

    just wondering what should i put

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Recording data , so you know whats next

    Try writing what you think should work and see what happens. When you have problems you can't figure out, post the code and your questions.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Recording data , so you know whats next

    thats what i think should work , when looking at the psuedo code =

    while ( there are more lines in the file)
    {
    lineoftext = nextline from scanner
    if (line start with //)
    {
    ignore
    }
    else if (line is blank)
    {
    ignore
    }
    else if (line starts with [)
    {
    set type of data
    }
    else {
    deal correctly
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Recording data , so you know whats next

    What does the last step in the else do: deal correctly?
    Where does the code handle the lines for the type set by the next to last step? "set type of data"
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Recording data , so you know whats next

    the deal code will create an object , read in the data and also store it in the array
    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);
    }
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Recording data , so you know whats next

    The else statements are executed when none of the above if statements are true.
    Where does the code handle the lines for the type set by the next to last step? "set type of data"
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Recording data , so you know whats next

    thats what the code is , thats why im confused

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Recording data , so you know whats next

    The code should use the results of the "set type of data" step to know how to process the following lines.
    The else statement ignores those results.

    Rework the logic so it uses the results of the type that has been found.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Recording data , so you know whats next

    yeah ino but how do i record it n use the result .
    Attached Files Attached Files

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Recording data , so you know whats next

    There needs to be a section of the code that is executed after the code determines the type of the data that follows.
    How many lines of data are for the data type found?
    Is there a line that indicates the end of the data for a type?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Recording data , so you know whats next

    there should be 7 for the car , but it keeps coming up null and not storing anything and no it will follow a comment , blank lines then [Van data , then do the van data

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Recording data , so you know whats next

    it keeps coming up null
    If you are getting errors, you need to copy and post the full text of the error message.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Recording data , so you know whats next

    java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:907)
    at java.util.Scanner.next(Scanner.java:1416)
    at Vehicle.readData(Vehicle.java:112)
    at Car.readData(Car.java:35)
    at ReservationSystem.readVehicleData(ReservationSyste m.java:77)

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Recording data , so you know whats next

    NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:907)
    at java.util.Scanner.next(Scanner.java:1416)
    at Vehicle.readData(Vehicle.java:112)
    At line 112 the code calls the next() method when there is no data to be read. Use the Scanner class's hasNext method to check that there is data BEFORE trying to read data that is not there.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Recording data , so you know whats next

    there is data there though , all the vehicles have regNo's and its saying that on 112 that it cant find the regNo,.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Recording data , so you know whats next

    NoSuchElementException
    That error says that there is no data to read.

    To see where the program is reading in the file, add a println() statement that prints out everything that is read. If you look at the last thing printed and look at the file's contents you should see where the problem is.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Recording data , so you know whats next

    nothing is being read in

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Recording data , so you know whats next

    nothing is being read in
    Why is that? How does the code find and read from the file?
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Recording data , so you know whats next

    through the scanner in a different class in a method . and also in the sub class it reads in additional data

    public void readData(Scanner scanner)
    {
    group = scanner.next();
    vehID = scanner.next();
    regNo = scanner.next();
    make = scanner.next();
    model = scanner.next();
    String yes = scanner.next();
    if(yes.equalsIgnoreCase("yes"))
    {
    airCon = true;
    }
    else if(yes.equalsIgnoreCase("no"))
    {
    airCon = false;
    }
    engineSize = scanner.nextDouble();
    fuelType = scanner.next();
    gearbox = scanner.next();
    transmission = scanner.next();
    mileage = scanner.nextInt();
    dateFirstRegistered = scanner.next();


    }

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Recording data , so you know whats next

    What is the last item read before the NoSuchElementException?
    What is in the file after the last thing that was read? The print outs of everything right after it was read will show you.

    Which line is line 112 in readData()? That is the one that is trying to read data that is not there.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Recording Sound
    By mulligan252 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: July 1st, 2014, 12:45 AM
  2. Replies: 0
    Last Post: November 16th, 2012, 05:25 AM
  3. List methods add(int k, Data data), set(int k, Data data), remove(int k)
    By Enirox in forum Object Oriented Programming
    Replies: 3
    Last Post: September 20th, 2012, 06:43 AM
  4. Need help with a program recording Lego Robot (Lego Mindstorms) movement.
    By Kranti1992 in forum Java Theory & Questions
    Replies: 3
    Last Post: January 18th, 2012, 01:16 PM
  5. call recording throuph java
    By 7121987 in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: April 29th, 2010, 04:12 PM