Jagged array in java -
my final output should like:
how many rows in jagged array? 4 enter row, separated spaces: 9 2 14 5 8 enter row, separated spaces: 3 enter row, separated spaces: 15 23 enter row, separated spaces: 9 8 7 6 5 4 3 after funky operation, resulting array is: 9 4 42 20 40 6 45 92 36 40 42 42 40 36 30
but keep getting errors:
exception in thread "main" java.util.nosuchelementexception: no line found @ scannerhacked.nextline(scannerhacked.java:1525) @ jagged.main(jagged.java:14)
here 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 in jagged array? "); int row = sc.nextint(); int[][] jaggedarray = new int[row][]; for(int = 0; < row; i++) { scanner rows = new scanner(system.in); system.out.print("enter row, separated 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 funky operation, resulting array is:"); (int = 0; < row; i++) { (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"); } } }
as can see, using 2 scanner
same source (system.in)
in main()
method...
scanner sc = new scanner(system.in); scanner rows = new scanner(system.in);
it might throw exception, should close first scanner
i.e. sc.close();
when going use scanner
same source.
Comments
Post a Comment