Page 2 of 2 FirstFirst 12
Results 21 to 29 of 29
Like Tree1Likes

Thread: Array Troubles

  1. #21
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,513
    Rep Power
    25

    Default Re: Array Troubles

    Can you post the program's output that shows the problem?
    If you don't understand my response, don't ignore it, ask a question.

  2. #22
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: Array Troubles

    The output for the program is as follows

    Path is C:\Users\Brad\Downloads\FB1.csv
    File can be read and executed
    Your file is being analysed. This may take several minutes.
    Total Rows: 20
    Your file is being converted. This may take several minutes.
    20 7 58.23"

    The last line of text "20 7 58.23" is the last value in the array. I do not know why it is not printing out all elements in the array; I feel like I have done everything correct in my FOR statements??

  3. #23
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,513
    Rep Power
    25

    Default Re: Array Troubles

    A couple of problems.
    There are 3 different Strings used to define the path to a file. There should only be one if its the same file being referenced.
    The x and y values are defined outside of the for loops in the printArray method. Why is that?
    If you don't understand my response, don't ignore it, ask a question.

  4. #24
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: Array Troubles

    It works. Thank you to everyone for your help. I finally got everything working. Thank you thank you thank you.

  5. #25
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Array Troubles

    Hi,

    Glad that you solved your problems. Please allow me to mention a few points (some was mentioned before)

    findRows seems quite scary:
    - You have a variable filePath but then you use this varuable to store a line of the file? Do not reuse variables that way or if you simply have to do it that way (I do not see any reason here) then name the variable so that people see it.
    - You create a StringTokenizer and then? Nothing.
    So at least that method seems unfinished (Maybe it is just some unfinished work and no longer required?)

    convertFile
    - Same as above with convertFilePath
    - you should check the array boundaries - so if a line is to long then you should not run into an exception.
    - you have a variable newRowNumber - that holds the number of rows imported to far. (So I would rename it to numberOfRows or maxRowNumber or something like that). You should use this inside the printArray method so that you show the elements that you stored.
    - Why do you start the array with 1? The first element is 0. You should take care about this fact and store the first element in 0 and not in 1.
    - All these magic numbers. A strict rule should always be to not place numbers inside the code. Instead use constants that you set. (Exceptions might be numbers that are obvious and never change e.g. a 0 because an array starts with 0!) So create a MAX_ROWS constant or so with 480 and then check that boundary, too. So when a file is to long, you do not get an exception.
    - Check the scope of variables. A variable that is only required localy should be a local variable. As soon as you start multi threaded application you might run into trouble if you do not take care. (Best example is x and y in your code!)
    - Check the visibility of variables. You made all public. It is a good thing to not have the data itself public. Best example is your array you have so far. Maybe the next step is to drop the array and create a dynamic structure instead. If someone used your class, he has to update his code because you changed your code. If you simply created an interface where others could get an element, then only you had to adapt this function and everyone else does not need to change anything. (This is called encapsulation. You class hide the implementation details.)

    Ohh ... in PrintArray you do not need to set y=1 ... the for loop is taking full care of the value of y so there is no reason why you need to modify it there.

    That are some points that I want to mention. Hope that helps a little bit to improve your coding.

    With kind regards,

    Konrad
    Norm likes this.

  6. #26
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Array Troubles

    Let's look only at your print array method

    Java Code:
    public void printArray()
      {
        for (x = 1; x < 20; x++);
        {
          for(y = 1; y < 7; y++);
          {
            System.out.println (x +" "+ y +" "+ myFBfile[x][y]);
          }
          y = 1;
        }
      }
    I think you should start writing your brackets differently. I'll show you how, then I'll explain why

    Here is what you are doing:
    Java Code:
    if(this is true)
    {
       do this;
    }
    Here is what you should do:
    Java Code:
    if(this is true) {
       do this;
    }
    It is much easier to read. And it takes up less space.

    However, this is just a preference sort of thing. The real reason I'm telling you this is so that you remember that
    YOU DO NOT PUT ";" AFTER FORLOOPS OR IF STATEMENTS OR ANYTHING THAT curly braces (thanks jim) COME AFTER

    In your code, you for some reason put ";" after your forloops.

    forloops will ALWAYS look like this:
    Java Code:
    for(int i = 0; i < 100; i++) {
       do this;
    }
    Notice, no semicolon after the forloop itself.
    Last edited by AlexGraal; 02-16-2014 at 08:03 PM.

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

    Default Re: Array Troubles

    However, this is just a preference sort of thing. The real reason I'm telling you this is so that you remember that
    YOU DO NOT PUT ";" AFTER FORLOOPS OR IF STATEMENTS OR ANYTHING THAT BRACKETS COME AFTER
    Actually, their called curly braces and your statement is not quite correct.

    Java Code:
    MyClass foo = new MyClass() {
      // other stuff
    };
    And of course, barring a company's guidelines, it's up to the individual on how they format their code. One person's preference is another person's nightmare.

    Regards,
    Jim
    Last edited by jim829; 02-16-2014 at 07:21 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #28
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Array Troubles

    Yeah thanks I mistyped there.

    No, the example you gave is of putting a semicolon after a curly brace.

    I said that you don't ever have a semicolon followed by an OPEN curly brace.

    Furthermore, I think I said that this is just a preference.

    Here, I'll quote myself for you:
    "However, this is just a preference sort of thing. The real reason I'm telling you this is so that you remember that
    YOU DO NOT PUT ";" AFTER FORLOOPS"

    Yup, mentioned that it is a preference.

    It makes it easier to remember not to add a semicolon, if you add an open curly bracket on the same line. I thought that it was clear that that was the reason for my suggestion. I guess not.
    Last edited by AlexGraal; 02-16-2014 at 11:36 PM.

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

    Default Re: Array Troubles

    I said that you don't ever have a semicolon followed by a curly brace.
    Actually, to do that is more common that the anonymous class example (as your examples above demonstrate).

    Java Code:
    for(int i = 0; i < 100; i++) {
       do this;
    }
    In any event, it's not related to the thread topic so I am considering this thread closed.

    Regards,
    Jim
    Last edited by jim829; 02-16-2014 at 11:25 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Array Troubles
    By shodai in forum New To Java
    Replies: 2
    Last Post: 02-11-2014, 05:43 PM
  2. HashMap Troubles
    By Cod in forum New To Java
    Replies: 5
    Last Post: 02-09-2014, 10:52 AM
  3. troubles with CardLayout
    By StaticFlow in forum AWT / Swing
    Replies: 3
    Last Post: 05-25-2012, 02:27 AM
  4. Integer troubles again...
    By Tb0h in forum New To Java
    Replies: 1
    Last Post: 09-20-2009, 02:32 AM
  5. [SOLVED] Array troubles, yes I searched...
    By Reiyn in forum New To Java
    Replies: 11
    Last Post: 04-16-2009, 11:28 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
  •