0

I'm making this program in Java and in this piece of code, it takes me as input the number "num" but skips the insertion of "nomeStanze". I'm trying to figure out why it doesn't work but I'm not succeeding. I am attaching both the portion of the code and the entire code. Thanks everyone for the help

Portions of the code:

 for (int i=0; i<5; i++)
            {
                System.out.println("Digita il numero della stanza: ");
                num=ins.nextInt();
                System.out.println("Digita il cliente che vuole prendere la stanza "+i);
                n=ins.nextLine();
                nomeStanze[num]=n;
                ver[num]=true;
            }

Entire code:

import java.util.Scanner;
public class Programma {
public static void main (String args[]) {
    Scanner ins = new Scanner(System.in);
    int r=0;
    CAlbergo albergo1 = new CAlbergo ();
    boolean[] ver = new boolean[100];
    int num=0;
    String n;

    for (int i=0; i<100; i++)
    {
        ver[i]=false;
    }

    String nomeStanze[] = new String [100];

    for (int i=0; i<100; i++)
    {
        nomeStanze[i]=null;
    }

    do {
        System.out.println("Digita 1 per inserire i 5 clienti\n2 per stampare l'elenco dei clienti presenti\n3 per stampare le camere libere\n4 per controllare dove risiede un cliente\n5 per inserire un cliente nella prima camera libera\ne 0 per uscire.");
        r=ins.nextInt();
        switch (r) {
            case 0:
            System.out.println("Sto uscendo...");
            break;

            case 1:
            for (int i=0; i<5; i++)
            {
                System.out.println("Digita il numero della stanza: ");
                num=ins.nextInt();
                System.out.println("Digita il cliente che vuole prendere la stanza "+i);
                n=ins.nextLine();
                nomeStanze[num]=n;
                ver[num]=true;
            }


            for (int i=0; i<100; i++)
            {
                System.out.println(nomeStanze[i]);
            }
            /*
            for(int i=0;i<5;i++)
            {
                System.out.println("Inserisci il numero della camera: ");
                do{
                    num=ins.nextInt();
                    if(num>100)
                    {
                        System.out.println("Le ricordo che le camere reinserisca usono 100, n altro numero.");
                    }
                }while(num>100 || ver[num]==true);  
                System.out.println("Inserisci il nome della persona che occupa questa stanza: ");
                nomeStanze[num]=ins.nextLine();
                ver[num]=true;
            }
            */

            break;
        }
    }
    while (r!=0);

}

}

  • `ins.nextInt()` does not handle the end-of-line token, and this is messing up your next call to `ins.nextLine()`. The duplicate explains the details of this problem and how to solve it (usually by explicitly handling the EOL token when and where needed by calling `ins.nextLine()`). – Hovercraft Full Of Eels Nov 27 '20 at 16:58

0 Answers0