-3

i'm trying to get multiple input from Scanner in Java, the input need to be in a for loop. Single input is working, as i put it in a for, the first cicle work as expected but the second stops at the first input. Before the loop i get an integer, so i've put a scan.nextLine() to get the '\n' character, in the loop i work only with strings.

I tought the problem could be the stream close, but i need it to be open, closing it i get a scanner Exception.

public void scegliEsame(Medico m)
{
    Scanner scan=new Scanner(System.in);
    String nome, data;

    System.out.print("Inserire numero di esami da prenotare: ");
    int ex=scan.nextInt();
    System.out.println();
    scan.nextLine();
    for(int i=0;i<ex;i++)
    {
        System.out.print("Inserire nome esame da voler effettuare: ");
        nome=scan.nextLine();

        if(m.getCosto(nome)==-1)
        {
            System.out.println("Esame non trovato");
            return;
        }
        else
            costo+=m.getCosto(nome);


        System.out.print("Inserire data di prenotazione: ");
        data=scan.nextLine();
        Esame e=new Esame(nome, m, data,costo);
        esami.add(e);
    }
}

Code method from class two

public void caricaEsami(Medico m)
{
    String esame;
    float costo;
    System.out.print("Inserire numero di esami totali: ");
    Scanner scan=new Scanner(System.in);
    int n=scan.nextInt();
    scan.nextLine();
    for(int i=0;i<n;i++)
    {
        scan=new Scanner(System.in);
        System.out.print("Inserire esame "+(i+1)+": ");
        esame=scan.nextLine();
        System.out.print("Inserire costo esame: ");
        costo=scan.nextFloat();
        Esame e=new Esame(esame, m, costo);
        m.setEsame(e);
        scan.nextLine();
    }
    medici.add(m);
}

I'm sorry for the italian language in my code, hope you could help me.

Alex
  • 19
  • 4
  • What do you mean by "*stops*"? – PM 77-1 Jun 18 '19 at 14:21
  • 1
    I tested your loop with `print` statements and it is working fine. Your problem is likely somewhere in the code we can't see, try removing the calls to other classes and just print `data` and `nome` and you'll see those values are storing corretly. – Nexevis Jun 18 '19 at 14:22
  • 1
    The code worked fine for me to, maybe this condition is the issue `if(m.getCosto(nome)==-1)` – Joakim Danielson Jun 18 '19 at 14:24
  • 1
    I've tested your code and the scanner is working fine. If I got your problem right, the "bug" is caused by the return statement inside your loop, which breaks the cycle. – Leonardo Meinerz Ramos Jun 18 '19 at 14:26
  • I think the conclusion here is that this is something you need to figure out yourself, either by using a debugger or by adding print statements of variables in your code. – Joakim Danielson Jun 18 '19 at 14:29
  • Thank you all for the answers, i've edited the question adding the code from class two, it's the other class where i use the Scanner I think the problem is in there, maybe because i don't close the stream, but if i do, the execution give me a Scanner exception – Alex Jun 18 '19 at 14:32

3 Answers3

0

Since your question is a problem with Scanner, Can you check if this works alright for you ? Most likely you have some other problem.

import java.util.*;
public class Main
{
    public static void main(String[] args) {
        System.out.println("Hello World");
        scegliEsame();
    }

    public static void scegliEsame(){
        Scanner scan=new Scanner(System.in);
        String nome, data;

        System.out.print("Enter number of exams to be booked: ");
        int ex=scan.nextInt();
        System.out.println();
        scan.nextLine();
        for(int i=0;i<ex;i++)
        {
            System.out.print("Enter exam name you want to make: ");
            nome=scan.nextLine();
            System.out.println("You Entered: " + nome);

        }
    }
}
Raj
  • 381
  • 2
  • 8
0

This code looks fine and it is working for me.


public class Test {

    List<String> list1 = new ArrayList<>();
    List<String> list2 = new ArrayList<>();

    public static void main(String[] args) {
        new Test().scegliEsame();   
    }

    public void scegliEsame()
    {
        Scanner scan=new Scanner(System.in);
        String nome, data;

        System.out.print("Inserire numero di esami da prenotare: ");
        int ex=scan.nextInt();
        System.out.println();
        scan.nextLine();

        for(int i=0;i<ex;i++)
        {
            System.out.print("Inserire nome esame da voler effettuare: ");
            nome=scan.nextLine();

            if(false)
            {
                System.out.println("Esame non trovato");
                return;
            }
            else
                list1.add(nome);


            System.out.print("Inserire data di prenotazione: ");
            data=scan.nextLine();
            list2.add(data);
        }
        System.out.println(list1+"\n\n"+list2);
    }
}
Pradyskumar
  • 144
  • 1
  • 9
0

If you want to use nextLine() more than one time you probably get this problem because it will take the \n from your first input as its input. To avoid that, use next().

oguzhancerit
  • 1,251
  • 1
  • 11
  • 22