Page 2 of 2 FirstFirst 12
Results 21 to 34 of 34
Like Tree3Likes

Thread: Help me please

  1. #21
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help me please

    Quote Originally Posted by DK Bello View Post
    I do not understand what you mean,
    if I make the return method of the constructor to be
    public String toString() {
    return "Car Reg No.: " + regNumber + ", Price: " + price +". "+ spec ;
    }
    This is confusing since constructors don't return anything. All they do is construct an object.

    then the car array cannot take the value because it have to be in this format car[i] = new Car (..., ..., ...)
    which means I have to put in 3 sets of strings in the array, and I find it impossible to divide to strings form the fine into set of threes for a single array
    You have to find a way to split your String.

    By the way, was your text file given to you from your instructor, or did you create it yourself? It looks suspiciously as if you're trying to put code in the text file, something you shouldn't do.

  2. #22
    DK Bello is offline Member
    Join Date
    Dec 2011
    Location
    UAE
    Posts
    29
    Rep Power
    0

    Default Re: Help me please

    the text file was giving to me by the instructor, the requirement is 'not to change any thing from the text file, I'm suppose to read it as it is.
    I have tried avery possible way of slitting the String but I couldn't, that's why I need help.

  3. #23
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  4. #24
    DK Bello is offline Member
    Join Date
    Dec 2011
    Location
    UAE
    Posts
    29
    Rep Power
    0

    Default Re: Help me please

    uploading to the forum is giving problem. But this is a link to the file.
    https://www.dropbox.com/s/wuj65rt1rsf2inu/carList.txt

  5. #25
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help me please

    OK, what you want to do is put your big program to the side for now, and create a small program to work on just parsing the file. Once you've figured out how to parse it, you can move the code to the larger program and create Car objects with it inside of your for loop. I will start you out with this small program, something like:

    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class GetCarlist {
       // Your FILE_NAME may be different
       private static final String FILE_NAME = "/Users/bello_dbello/Dropbox/" +
           "JavaEE/WhizzCar/src/whizzCar/carList.txt";
    
       // private static final String FILE_NAME = "src/yr12/m05/b/carList.txt"; // this is what I had to use
    
    
       public static void main(String[] args) {
          String fileName = FILE_NAME;
          File file = new File(fileName);
          Scanner fileScanner = null;
          try {
             fileScanner = new Scanner(file);
          } catch (FileNotFoundException e) {
             e.printStackTrace();
          }
    
          fileScanner.useDelimiter(";");
          while (fileScanner.hasNext()) {
             String line = fileScanner.next(); // get the line
    
             // display line, but surround with *** so can see where line starts 
             // and where the line ends. this is for demo only and you will comment
             // the line below out when you try to parse
             System.out.println("*** " + line + " ***"); 
             
             // here is where you will want to do your parsing work on the line, including
             // getting rid of new-lines
             // parsing by comma, but not inside of quotes
             // play with different ways to parse, and always println out your results to see
             // what you're doing
          }
       }
    }

  6. #26
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Help me please

    Quote Originally Posted by Ray Pooley View Post
    ---Quote (Originally by Ray Pooley)---
    From what I can see there are no newline/carriage returns in your text file so when you scan.next() you are pulling the whole file in.
    ---End Quote---
    Opened the file in Windows Notepad, did you?

    db
    ***************

    @db. Got this in my inbox but can't find the comment on the forum for some reason. (Maybe adding this comment might make it appear)

    If you are wondering whether I scanned the text for ASCII 13 and/or 10 then the answer is no I didn't.

    What I can say is that the file does not appear to be line orientated when viewed in an RTF editor.

    In any case, on examination there is certainly a usable line delimitter present in the structure of the data. The semi colon.

    So it would be stupid to rely on line orientation when there is a much more reliable delimitter available to deploy instead.

    Would you not agree with that?
    I would agree.

    I opened the file in Notepad, saw no newlines -- which is common when using Notepad to open a file that has \n or \r but not both as newline characters.

    After ascertaining that the file really had no \n or \r -- no newlines of any kind -- I deleted my response as it could be a distraction to DK Bello.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #27
    DK Bello is offline Member
    Join Date
    Dec 2011
    Location
    UAE
    Posts
    29
    Rep Power
    0

    Default Re: Help me please

    Thanks a lot.
    You mean that using the txt format and reading Strings line by line is almost impossible. So how can I use the XML now?

  8. #28
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help me please

    No, don't do XML here, and I'm not sure why he's recommending. This is a text file, and you have to remove the new line characters (via String.replace(...)) and then analyze the lines. Keep at it.

  9. #29
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help me please

    The line breaks are present, and when I removed them in my sample program, the logical structure of each data "row" became more apparent. Regardless, he'll have nothing to lose by trying to remove them.

    To the original poster, again, consider using String#replace(...) to remove the line breaks. You can get the OS-dependent line breaks by calling

    Java Code:
    String lineBreak = System.getProperty("line.separator");
    Then you can use the lineBreak variable wherever you need to detect line breaks.

    As for reading your file, use your Scanner with ";" as the delimiter as you were doing initially. Don't use a BufferedReader. Using the Scanner this way will let you obtain a logical "row" with ease.

  10. #30
    DK Bello is offline Member
    Join Date
    Dec 2011
    Location
    UAE
    Posts
    29
    Rep Power
    0

    Default Re: Help me please

    Thank you all.
    But sorry to say, I'm kind of lost. I tried to put the cars String into the car[] array, but I still end up with the array returning null and throwing errors after then.

  11. #31
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Help me please

    Before the poor guy builds a rudimentary compiler in Java, can we check to make sure that the instructor didn't simply mean that you should use that code in your program to generate the cars?

    That seems far, far more likely. To ask you to do this kind of work at your level of understanding would be ridiculous.
    Fubarable likes this.

  12. #32
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help me please

    Quote Originally Posted by Diargg View Post
    Before the poor guy builds a rudimentary compiler in Java, can we check to make sure that the instructor didn't simply mean that you should use that code in your program to generate the cars?

    That seems far, far more likely. To ask you to do this kind of work at your level of understanding would be ridiculous.
    Yes, on hindsight of course, you're absolutely right. I feel pretty foolish right now.

  13. #33
    DK Bello is offline Member
    Join Date
    Dec 2011
    Location
    UAE
    Posts
    29
    Rep Power
    0

    Default Re: Help me please

    Thank you all for your concern, But things here are getting more complicated and i'm almost out of time and losing hope. I think you should that a look of the question, maybe you can understand it better.
    This is just my first year and 50% of my course work, How hard can things get...

    https://www.dropbox.com/s/xlqqzao9al...%20Cars%29.pdf

  14. #34
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Help me please

    Poor phrasing on your instructors part, perhaps, but I would say that you can just copy paste the code he gave you into your program and use that to construct whatever list or array of Cars that you want.

    Cut the crap with Scanner, you won't need it at all, except perhaps to read in things from the GUI. At the discretion of the mods, I'd let this thread die, and come back when you run into any other problems.
    Fubarable likes this.

Page 2 of 2 FirstFirst 12

Posting Permissions

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