0

Here in my code, I have two String type ArrayList arr and arr2. arr ArrayList is for storing the pair of lines and here int t is for the number of pairs that user enters in arr. The Second ArrayList arr2 is for storing a single line and integer t2 is the number of a single line that a users stores in arr2. After that I have simply print arr and arr2

import java.util.Scanner;

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

        ArrayList<String> arr = new ArrayList<>();
        ArrayList<String> arr2 = new ArrayList<>();
        int t;
        t = sc.nextInt();

        sc.nextLine();

        for(int i=0;i<t;i++){
            String first,second;
            first = sc.nextLine();
            second = sc.nextLine();

            arr.add(first);
            arr.add(second);
        }

         sc.nextInt();
        int t2;
        t2 = sc.nextInt();

        sc.nextLine();

        for(int i=0;i<t2;i++){
            String input;
            input = sc.nextLine();
            arr2.add(input);
        }

        for(String val: arr){
            System.out.println(val);
        }
          System.out.println();
        for(String val: arr2){
            System.out.println(val);
        }

    }
}

But after entering all the input I'm getting Exception in thread "main" java.util.InputMismatchException

Sample Input-

3
ko te kader molla
tui rajakar tui rajakar
tumi ke ami ke
garo chakma bangali
jalo re jalo
agun jalo

2

jalo re jalo
ko te kader molla



Sample Output-

ko te kader molla
tui rajakar tui rajakar
tumi ke ami ke
garo chakma bangali
jalo re jalo
agun jalo

jalo re jalo
ko te kader molla


1
  • Consider using meaningful variable names. It is much easier for someone (or yourself) to follow pairsOfLines than arr2. Commented Jan 7, 2020 at 18:28

1 Answer 1

1

You're reading 2 times sc.nextInt(); after the first for-loop. In these lines:

sc.nextInt();
int t2;
t2 = sc.nextInt();

I believe your error comes from there as the next value is an int (3 in your sample input) but then you try to read another int but you're providing a String.

If this is not the issue, please post a complete Stack Trace.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.