-1

I am working on an assignment and came up with this code. I have all of the correct formulas and such. I ran the debugger and saw that my program skips my first if statement. I don't know why it continues to do this. I've been working on this program for hours and can't find a solution.

public static void main (String args[])
{
    Scanner inFile = null; //creating scanner
    try
    {
        inFile = new Scanner(new File("prog435t.dat")); //reading file
    }
    catch (FileNotFoundException e) //if no file
    {
        System.out.println("File not found!");
        System.exit(0);
    }

    String [] sex = new String [12] ;
    String[] bone = new String [12];
    int [] age = new int [12];
    double [] length = new double [12];

    for(int count = 0; count<12; count++)
    {
        sex[count] = inFile.next();           
        bone [count] =  inFile.next();
        age [count] = inFile.nextInt();
        length [count] = inFile.nextDouble();
    }

    double [] height = new double [12];

    for(int count = 0; count<12; count++)
    {
        if(sex[count] == "M")
        {
            switch(bone[count])
            {
                case "F":
                            height[count] = 69.089 + 2.238 * length[count];
                            height[count] = (int)(height[count] *1000 +.5)/1000.0; //rounding
                            break;
                case "T":
                            height[count] = 81.688 + 2.392 * length[count];
                            height[count] = (int)(height[count] *1000 +.5)/1000.0; //rounding
                            break;
                case "H": 
                            height[count] = 73.570 + 2.970 * length[count];
                            height[count] = (int)(height[count] *1000 +.5)/1000.0; //rounding
                            break;
                case "R":
                            height[count] = 80.405 + 3.650 * length[count];
                            height[count] = (int)(height[count] *1000 +.5)/1000.0; //rounding
                            break;
                default:
                            height[count] = 0;
                            break;

            }
        }

        else 
        {
            switch(bone[count])
            {
                case "F":
                            height[count] = 61.412 + 2.317 * length[count];
                            height[count] = (int)(height[count] *1000 +.5)/1000.0; //rounding
                            break;
                case "T":
                            height[count] = 72.572 + 2.533  * length[count];
                            height[count] = (int)(height[count] *1000 +.5)/1000.0; //rounding
                            break;
                case "H": 
                            height[count] = 64.977 + 3.144 * length[count];
                            height[count] = (int)(height[count] *1000 +.5)/1000.0; //rounding
                            break;
                case "R":
                            height[count] = 73.502 + 3.876 * length[count];
                            height[count] = (int)(height[count] *1000 +.5)/1000.0; //rounding
                            break;
                default:
                            height[count] = 0;
                            break;                                
            }
        }

        if(age[count] >30)
        {
            double newAge = age[count]-30;

            height[count] = height[count] - (0.06*newAge);
        }


    }

    for(int count = 0; count < 12; count++)
    {

    }

    System.out.println("Sex\tBone\tAge\tLength\t    Height\n");
    for(int count = 0; count<12; count++)
    {
        System.out.print(sex[count] + "\t" + bone[count] + "\t" + age[count] + "\t" + length[count] + "\t    " + height[count] +"\n");
    }
}

My data looks like this:

F H 45 36.266
M F 18 46.967
M H 29 33.309
Q F 40 30.078
M T 95 36.983
M R 30 23.1768
F F 31 42.55
M Q 40 30
F F 65 39.96
F T 50 30.962
F R 80 19.375
M H 36 24.017

This is the desired output:

sex bone    age length  height
F   H   45  36.266  178.097
M   F   18  46.967  174.201
M   H   29  33.309  172.498
M   T   95  36.983  166.251
M   R   30  23.177  165.000
F   F   31  42.550  159.940
F   F   65  39.960  151.899
F   T   50  30.962  149.799
F   R   80  19.375  145.600
M   H   36    24.017    144.540
Q   F   40  30.000  Wrong Sex Code
M   Q   40  30.000  Wrong Bone Code
Buurman
  • 1,914
  • 17
  • 26
madS
  • 7
  • 3

1 Answers1

0

Use equals instead of == to compare String objects (you compare the values). also:

"M".equals(sex[count])

or the other way, if you also check that care sex[count] is not Null

Loic Mouchard
  • 1,121
  • 7
  • 22
  • you are correct that OP should use equals, but OP is *not* comparing values but references. That is in fact exactly the cause of his problem. – Buurman Mar 04 '16 at 13:44