0

Hi guys please help me out! Sorry if wasn't clear enough in the title. My english is incredibly limited.

So I have this java code and it has to work like this:

MENU:

1 - Register customer info

2 - Register customer airline mileage

3 - List specific customer their miles

4 - List the customer with the most and least mileage

5 - List everyones names and their miles

I can register their info just fine but I can't seem to register their miles for some reason.

I enter 1 to type in the info, and after I type everything it goes back to the menu and I type 2 and what it's supposed to do is: ask for a name of someone I had already registered before and then I'd register their airline miles. But when I type 2 to do that it doesn't even let me enter a name. It just goes to the 'else' condition "name not found"

This would be the problematic block of code:

        case 2: 
            System.out.println("customer name: ");
            name = scanner.nextLine();
            d=0;

            while ( d < c-1 && name!=data1[d])
            {d++;}

            if (name==data1[d])
            {
                System.out.println("enter airline miles of " +data1[d]);
                airMiles = scanner.nextDouble();

                airMile[d] = airMile[d]+airMiles;
            }

            else 

            {System.out.println("name not found");}
            break;

And this is the entire code in case any of you would like to take a look.

import java.util.Scanner;
public class Vetor49eng {
public static void main(String[] args) {
    Scanner scanner = new Scanner (System.in);

    int op, c, d, posLast, posFirst;
    String name, filler;
    double airMiles;
    String [] data1 = new String [50];
    String [] data2 = new String [50];
    String [] data3 = new String [50];
    double [] airMile = new double [50];

    for (c=0; c<5; c++)
    {airMile[c]=0.;}
    c=0;

    do
    {
        System.out.println("1. register customer info");
        System.out.println("2. register customer airline miles");
        System.out.println("3. show miles of a customer");
        System.out.println("4. show the names of the customer with the most miles and least miles");
        System.out.println("5. show all their names and their miles");
        System.out.println("6. exit");
        op = scanner.nextInt();

        switch (op)
        {
        case 1: 
            if(c<50)
            {
                System.out.println("customer " +(c+1) +": ");
                filler = scanner.nextLine(); //IT ALWAYS SKIP THIS FIRST LINE SO I CREATED THIS FILLER

                System.out.println("name: ");
                data1[c] = scanner.nextLine();

                System.out.println("address: ");
                data2[c] = scanner.nextLine();

                System.out.println("telephone number: ");
                data3[c] = scanner.nextLine();

                c++;
            }

            else

            {System.out.println("archive complete");}
            break;

        case 2: 
            System.out.println("customer name: ");
            name = scanner.nextLine();
            d=0;

            while ( d < c-1 && name!=data1[d])
            {d++;}

            if (name==data1[d])
            {
                System.out.println("enter airline miles of " +data1[d]);
                airMiles = scanner.nextDouble();

                airMile[d] = airMile[d]+airMiles;
            }

            else 

            {System.out.println("name not found");}
            break;

        case 3:
            System.out.println("customer name: ");
            name = scanner.nextLine();
            d=0;

            while (d<c-1 && name!=data1[d])
            {d++;}

            if (name==data1[d])

            {System.out.println("miles of " +data1[d] +": " +airMile[d]);}

            else

            {System.out.println("name not found");}
            break;

        case 4: 
            d=1;
            posLast=0;
            posFirst=0;

            while (d<=c)
            { if (airMile[d] > airMile[posFirst])
            { posFirst=d; }
            else
            { if (airMile[d] < airMile[posLast])
            { posLast = d; }
            }
            d++;
            }
            System.out.println("customer with the most mileage:");
            System.out.println("name: " +data1[posFirst]);
            System.out.println("address: " +data2[posFirst]);
            System.out.println("phone number: " +data3[posFirst]);
            System.out.println("miles: " +airMile[posFirst]);
            System.out.println();
            System.out.println("customer with the least mileage:");
            System.out.println("name: " +data1[posLast]);
            System.out.println("address: " +data2[posLast]);
            System.out.println("phone number: " +data3[posLast]);
            System.out.println("miles: " +airMile[posLast]);
            break;

        case 5: 
            System.out.println("list");
            for (d=0; d<c; d++)
            {System.out.println(d +" - " +data1[d] +": " +airMile[d]);}
            break;

        case 6:
            System.out.println("have a nice flight");
            break;
        default: 
            System.out.println("option not available");
        }
    }
    while (op!=6);
    System.out.println();
}
}

Sorry for my english and for taking up your time. I would really appreciate some help here. I'm (very) new to this.

B. Robs
  • 21
  • 2

1 Answers1

0

Well there are a few things with that code block. When comparing STRINGS as you are doing here you do not want to use ==. The two strings are not the same object, while they may have the same value they are completely different objects and will thus give you an error when you are attempting to verify the input.

For a string you want to use either .equals() if case is important or .equalsIgnoreCase() if you don't care about the casing.

Now for your issue of it skipping, you need to use = scanner.next() here. Using nextLine is discarding any entries and is automatically making your entry null.

         case 2:                    
                System.out.print("customer name: ");
                name = scanner.next();
                d = 0;

                while (d < c - 1 && !name.equalsIgnoreCase(data1[d])) {
                    d++;
                }

                if (name.equalsIgnoreCase(data1[d])) {
                    System.out.println("enter airline miles of " + data1[d]);
                    airMiles = scanner.nextDouble();

                    airMile[d] = airMile[d] + airMiles;
                } else {
                    System.out.println("name not found");
                }
                break;
basic
  • 3,162
  • 2
  • 16
  • 30