This site requires JavaScript, please enable it in your browser!
Greenfoot back
Zweeg
Zweeg wrote ...

2018/12/15

How to read multiple tokens in scanner input?

Zweeg Zweeg

2018/12/15

#
I want to convert multiple tokens from the scanner input into a string. How can I do this? I've tried using scanner.next() but that returns just one token. I've also tried using scanner.nextLine() but that returns a java.lang.Number format exception. Here is the code I'm trying to use. It is supposed to add movie information to separate arrayLists. My scanner is called "a".
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public static void addMovie()
    {
            System.out.println("What is the movie title?");
            if(a.hasNext())
            {
                names.add(a.next());
            }
            System.out.println("What year was it released?");
            if(a.hasNext())
            {
                year.add(Integer.valueOf(a.next()));
            }
            System.out.println("What was it rated?");
            if(a.hasNext())
            {
                rating.add(a.next());
            }
            System.out.println("What is the genre?");
            if(a.hasNext())
            {
                genre.add(a.nextLine());
            }
            System.out.println("Describe the move:");
            if(a.hasNext())
            {
                genre.add(a.nextLine());
            }
    }
danpost danpost

2018/12/16

#
I am guessing you want to combine the genre with the description. Please confirm. Also, show the code lines used to create this Scanner object.
You need to login to post a reply.