0

I'm encountering problems with this snippet of code:

public static void main(String[] args) 
    {
        Scanner in = new Scanner(System.in);
        String risposta;

        System.out.println("Quanti conti vuoi creare?\n");
        int numero_conti = in.nextInt();

        CCB[] conti = new CCB[numero_conti + 1];

        for (int i = 1; i <= numero_conti; i++)     
        {   

            conti[i] = new CCB();

            System.out.println("CONTO " + i + "\n");

            System.out.println("Banca: ");
            conti[i].setBanca(in.nextLine());

            System.out.println("Sede: ");
            conti[i].setSede(in.nextLine());

            System.out.println("Nome: ");
            conti[i].setNome(in.nextLine());

            System.out.println("Cognome: ");
            conti[i].setCognome(in.nextLine());

            System.out.println("Codice Fiscale: ");
            conti[i].setCodice_fiscale(in.nextLine());

            System.out.println("Numero di conto corrente: ");
            conti[i].setNumero_conto(in.nextInt());

            System.out.println("Saldo: ");
            conti[i].setSaldo(in.nextDouble());
        }

The output is the following:

Quanti conti vuoi creare?

1
CONTO 1

Banca: 
Sede:
>input here< 

The problem is as follows:

In the code "snippet" the for loop does not work well. It should show me in output every single input after every System.out. But the result is that instead of inserting 7 inputs, I can insert only 6 and the first two System.out are attached together.

How can I solve this problem?

I tried in every way. If I insert only next (); I can not insert multiple strings followed by space all at once.

The output I would like should be the following:

Quanti conti vuoi creare?

1
CONTO 1

Banca:>Input here< 
Sede: >Input here<

Please help me to solve this! Thank you!

Luca
  • 1
  • `print()` instead of `println()` to start with. And skip `\n` when using `println()` – Joakim Danielson Mar 06 '19 at 17:19
  • Ok now it show Quanti conti vuoi creare? 1 CONTO 1 Banca: Sede: – Luca Mar 06 '19 at 17:21
  • Banca:Sede attached togheter – Luca Mar 06 '19 at 17:22
  • `System.out.print("\nSome text:");` or an empty `println()` before – Joakim Danielson Mar 06 '19 at 17:22
  • Tried even this. Nothing happen. Just like first, Banca:Sede: – Luca Mar 06 '19 at 17:24
  • 1
    This has nothing to do with `println`. Please read the duplicate. – RealSkeptic Mar 06 '19 at 17:25
  • No, @JoakimDanielson. It's about the fact that the newline following the number is being read in the first `nextLine()` and therefore there is an immediate empty string, and then only 6 inputs requested. – RealSkeptic Mar 06 '19 at 17:27
  • So i need to do what? Insert nextLine(); to the beginning? – Luca Mar 06 '19 at 17:28
  • @RealSkeptic I removed my comment because I realised I needed to test this and I had missed the `nextInt`at the beginning so you are right but it is also an issue with using println when asking the question since OP wants to type the answer on the same line as the question/label. But that can't be fixed before the issue you pointed out is fixed first. – Joakim Danielson Mar 06 '19 at 17:38
  • I solved in this way `System.out.print("CONTO " + i + "\n"); System.out.print("Banca: "); conti[i].setBanca(in.nextLine()); in.nextLine(); System.out.print("Sede: "); conti[i].setSede(in.nextLine());` – Luca Mar 06 '19 at 17:44
  • But now it show me nothing into the array when i check into with conti[i].getBanca(); It's empty The output: `Conto: TRY TRY Codice fiscale: TRY Numero conto: 0 La sua banca: La sua sede: TRY Il suo saldo: 0.0 Cosa vuoi fare con questo conto? ---------------------------------------------------- 1.VERSAMENTO | 2.PRELIEVO | 3.CAMBIO CONTO | 4.SALDO | 5.ESCI` – Luca Mar 06 '19 at 17:45
  • Ok Solved! I understood the problem. To the beginning of the for loop i wrote in.nextLine();. Now everything goes well. Thanks for the help anyway, and sorry for the duplicate, i need to learn more how to use this wonderful website with this wonderful community. – Luca Mar 06 '19 at 17:53

0 Answers0