-2

When I gave n=3, I can only Input 2 names, actully I should be able to give three names.

import java.util.*;

public class whoshouldpay {
    
    public static void main(String[] args)
    {
        Scanner getem=new Scanner(System.in);
        System.out.println("\nHow many members:\t");
        int n=getem.nextInt();
        String Names[]=new String[n];
        System.out.println("\nEnter the names\t");
        for (int i=0;i<n;i++)
        {
            Names[i]=getem.nextLine();
        }

        Random random=new Random();
        int rand_num=random.nextInt(n);

        System.out.println("\n"+Names[rand_num]+" Should pay the bill");

    }
}
Federico klez Culloca
  • 22,898
  • 15
  • 55
  • 90
  • "for loop terminating before the condition is false" did you actually print `i < n` after the loop to see if that was the case? Then you could have printed `Arrays.toString(Names)` to see what had been stored by the loop. (This skipping-after-nextInt is a common gotcha with `Scanner`; but you can do things like I suggest to help you diagnose the problem). – Andy Turner May 10 '21 at 11:28
  • The Names array contains the two names I have inputed. – Thiru Manikandan May 10 '21 at 12:17
  • It will, but it will also contain `""` as the first element. – Andy Turner May 10 '21 at 12:18

0 Answers0