-1
    int store1, store2, store3, store4, store5;
        char ans = 'e';
        String gen;

        Scanner scan = new Scanner(System.in);
        Random randNum = new Random();
        System.out.println("Do you want to enter the sales figure (e) or do you want it randomly generated (r)? ");
        gen=scan.nextLine();
        if (ans == 'e' || ans == 'E')
        {
            System.out.println("Enter todays sales for: ");
            System.out.println("Store 1: ");
            store1=scan.nextInt();
            System.out.println("Store 2: ");
            store2=scan.nextInt();
            System.out.println("Store 3: ");
            store3=scan.nextInt();
            System.out.println("Store 4: ");
            store4=scan.nextInt();
            System.out.println("Store 5: ");
            store5=scan.nextInt();

            scan.close();

        }
        else 
        {
        System.out.println("Store 1: ");
        store1=randNum.nextInt(10);
        System.out.println("Store 2: ");
        store2=randNum.nextInt(10);
        System.out.println("Store 3: ");
        store3=randNum.nextInt(10);
        System.out.println("Store 4: ");
        store4=randNum.nextInt(10);
        System.out.println("Store 5: ");
        store5=randNum.nextInt(10);


        }

    }

} 

Need to figure out how to get this code working properly. Right now it only recognizes the 'e' and 'E' enter but not if i enter anything else. When it does do the else the random number gen won't work.

Nitin
  • 370
  • 2
  • 12
squash69
  • 5
  • 6

1 Answers1

1

Look at this part of your code

gen=scan.nextLine();
if (ans == 'e' || ans == 'E')

You are reading in gen, but comparing ans, this can't work.

change your statement to

if (gen.toUpperCase().charAt(0) == 'E')

Also, I smell that you are about to compare String, and I smell that you maybe don't know how as you stated I'm a beginner in progress.

So note that if you want to compare String, you need to use the .equals method as if you use == you will compare memory address (as for any other Object)

Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76