0

My final output should be like:

How many rows are in the jagged array? 4
Enter a row, separated by spaces: 9 2 14 5 8
Enter a row, separated by spaces: 3
Enter a row, separated by spaces: 15 23
Enter a row, separated by spaces: 9 8 7 6 5 4 3

After the funky operation, the resulting array is:

9    4   42   20   40
6
45   92
36   40   42   42   40   36   30

But I keep getting errors:

Exception in thread "main" java.util.NoSuchElementException: No line found
    at ScannerHacked.nextLine(ScannerHacked.java:1525)
    at jagged.main(jagged.java:14)

Here is my code:

import java.util.Scanner;

public class jagged {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("How many rows are in the jagged array? ");
        int row = sc.nextInt();
        int[][] jaggedArray = new int[row][];

        for(int i = 0; i < row; i++)
        {   Scanner rows = new Scanner(System.in);
            System.out.print("Enter a row, separated by spaces: ");
            String arraystring = rows.nextLine();
            String []a = arraystring.split(" ");
            jaggedArray[i] = new int[a.length];
        for(int j = 0; j < jaggedArray[i].length; j++)
        {
            int y = Integer.parseInt(a[j]);
            jaggedArray[i][j]  =  y;

        }

        }
        System.out.println("After the funky operation, the resulting array is:");
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < jaggedArray[i].length; j++)
            {
                if(jaggedArray[i][j] > 9)
                System.out.print("   "+(jaggedArray[i][j]*(i+j+1)) + "");
                    else
                System.out.print("    "+(jaggedArray[i][j]*(i+j+1)) + "");
            }
            System.out.print("\n");
        }   
    }

}
5
  • 2
    paste class ScannerHacked Commented Feb 6, 2014 at 5:24
  • Please show us your code at line no 1525 in ScannerHacked.java Commented Feb 6, 2014 at 8:06
  • There is not any exception in this code, it is running successfully. Commented Feb 6, 2014 at 8:22
  • are you using sc.close() in your program? Commented Feb 6, 2014 at 8:30
  • I will use sc.close now, thank you for your input. Commented Feb 6, 2014 at 19:39

1 Answer 1

1

As i can see, you are using two Scanner with the same source (System.in) in main() method...

Scanner sc = new Scanner(System.in);
Scanner rows = new Scanner(System.in);

it might throw an Exception, So you should close first Scanner i.e. sc.close(); when going to use another Scanner with same source.

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

1 Comment

Thank you, that explains it.

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.