Java Help - Basic 101 stuff...

Discussion in 'Software' started by bratwhoknows, Dec 8, 2007.

  1. bratwhoknows

    bratwhoknows Private E-2

    This is my first trip down the path coding with Java.

    I'm getting three errors in my code - and I'm cross-eyed from having spent four and a half hours looking for a stray semi-colon in my last program. First error I'm getting is "float cannot be dereferenced"

    fMiles = new float[fNum][1];
    for (i=0; i<Miles.length; i++)​

    The next two are incompatible types. FMiles = fMiles;
    FGallons = fGallons;

    I've attached the code in a txt document. Any advice (because I'm a idiot over this) would be *GREATLY* appreciated.

    Thanks.
     

    Attached Files:

    • mpg.txt
      File size:
      1.8 KB
      Views:
      3
  2. timduk

    timduk Private E-2

    It looks like you need to study array initialization...

    float fMiles[][] = new float[10][1];

    creates a 2-dimensional regular array of 10 entries, 1 entry each.

    float fMiles[][] = new float[10][];

    gives a two dimensional irregular array with the potential of a variable number of entries for each entry, so then you can say...

    fMiles[0] = new float[5];
    fMiles[1] = new float[7]..... etc.

    To assign a variable from within an array, you would need a specific index..

    float FMiles = fMiles[3][0]; ... assigns the first element of the fourth array to variable FMiles

    Without studying your code too deeply, it looks like you need something like this:

    float fMiles[][]; // uninitialized array, unknown size

    fMiles[] = new float[Num]; // number of fill ups

    fMiles[fNum][] = new float[1]; // space for one entry per fill up
    fMiles[fNum][0] = someValue; // assign a value to the array entry

    FMiles = fMiles[someIndex][0]; // as there is only one entry per index

    I'm not quite sure what you are trying to achieve. Are you sure you need 2 dimensional arrays?
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds