I need to read input test cases from console into a 2D array and a 1D array but unable to find the error in my code
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
public static final int SIZE = 5;
public static int card[][];
public static int ball[];
public static void read()throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int count=0;
for(int row = 0; row < SIZE && count < 25; row++){
String line= br.readLine();
System.out.println("line"+line);
//if((line= br.readLine()) != null) {
int column = 0;
StringTokenizer st = new StringTokenizer(line);
while(st.hasMoreTokens()){
card[row][column] = Integer.parseInt(st.nextToken());
column++;
count++;
}
column = 0;
// }
}
int i = 0, b=0;
String line ;
while((line =br.readLine() )!= null && i < 75) {
StringTokenizer st1 = new StringTokenizer(line);
while(st1.hasMoreTokens()){
ball[i++]= Integer.parseInt(st1.nextToken());
}
}
}
public static void printing() {
System.out.println("Card values\n");
for(int i=0;i<SIZE;i++) {
for(int j=0;j<SIZE;j++){
System.out.print(card[i][j]+"\t");
}
System.out.println();
}
System.out.println("Ball values\n");
for(int j=0;j<75;j++) {
System.out.print(ball[j]+"\t");
//System.out.println();
}
}
public static void main (String[] args) throws java.lang.Exception
{
card = new int[SIZE][SIZE];
ball = new int[75];
for(int t=0 ; t<2 ; t++) {
read();
printing();
}
}
}
Also the test case input is as follows
10 17 44 59 62
2 28 31 58 68
5 16 37 53 71
6 26 35 51 66
9 21 34 60 70
45 37 19 47 16 39 66 14 28 15
72 17 62 12 55 11 73 75 9 18
56 4 29 32 61 63 51 38 33 2
8 36 6 24 23 22 21 5 60 35
41 74 34 7 67 25 50 10 43 53
3 46 68 40 48 69 54 30 20 70
31 59 57 49 1 42 58 27 52 13
64 44 71 26 65
1 7 4 9 2
2 28 31 58 68
5 16 37 53 71
6 26 35 51 66
9 21 34 60 70
45 37 19 47 16 39 66 14 28 15
72 17 62 12 55 11 73 75 9 18
56 4 29 32 61 63 51 38 33 2
8 36 6 24 23 22 21 5 60 35
41 74 34 7 67 25 50 10 43 53
3 46 68 40 48 69 54 30 20 70
31 59 57 49 1 42 58 27 52 13
64 44 71 26 65
I am able to read the first test case but while reading the 2nd test case i am getting null error.
On your second pass:
for(int t=0 ; t<2 ; t++) {
read();
You create a second BufferedReader from System.in.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
It looks like you're expecting this to re-read the input. But all the input has already been read.
You might be able to reset this small input stream with a combination of InputStream.readLimit(), InputStream.mark() and InputStream.reset(). Or you could put the data in a file, and create a new FileInputStream for each read.
Related
I've created a loop and random number generator that generates 100 numbers within the range of 1-100. I need to format these numbers so that it is 10 per line. I tried using printf and had a hard time. In addition, I have to find the average of all these numbers. The issue is I am unsure of how to do this because all the numbers are under the int variable 'randoms'. I can't add a single variable together and divide by 100.
public static void main(String[] args) {
Random rand = new Random();
int n = 100;
for (int i=1; i<=n; i++) {
int randoms = rand.nextInt(101);
}
}
You may print each number without a new line, and with spaces before to pad at 4-length string, and each 10 values, print a new line. For the average, use math : sum/count
Random rand = new Random();
int n = 100;
int total = 0;
for (int i = 1; i <= n; i++) {
int randoms = rand.nextInt(101);
total += randoms;
System.out.format("%4d", randoms);
if (i % 10 == 0) {
System.out.println();
}
}
System.out.println("AVG " + total / (double) n);
49 55 89 26 88 58 80 98 62 8
34 65 9 3 28 71 30 11 50 50
18 90 61 62 18 93 83 83 57 14
9 54 49 6 24 28 60 8 86 83
60 6 17 67 49 89 66 13 65 50
70 24 3 90 89 4 47 49 48 7
16 38 79 59 51 9 22 81 8 84
52 30 64 97 42 100 30 26 66 44
22 46 16 100 73 100 56 63 8 48
50 88 55 93 6 82 65 46 44 7
AVG 49.29
I wrote this code for a programming assignment given in a course about graphs on Coursera. enter code here. It passes on the test cases given in the question description but fails on one when submitted I'm new to graphs. Can some one help me find the error in this code?
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Iterator;
public class Reachability {
private static boolean[] visited;
private static int reach(ArrayList<Integer>[] adj, int x, int y) {
if(x == y){
return 1;
}
visited[x] = true;
Iterator itr = adj[x].iterator();
while(itr.hasNext()){
x = (int)itr.next();
if(!visited[x]){
return reach(adj,x,y);
}
}
return 0;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
ArrayList<Integer>[] adj = (ArrayList<Integer>[])new ArrayList[n];
for (int i = 0; i < n; i++) {
adj[i] = new ArrayList<Integer>();
}
for (int i = 0; i < m; i++) {
int x, y;
x = scanner.nextInt();
y = scanner.nextInt();
adj[x - 1].add(y - 1);
adj[y - 1].add(x - 1);
}
int x = scanner.nextInt() - 1;
int y = scanner.nextInt() - 1;
visited = new boolean[n];
System.out.println(reach(adj, x, y));
}
}
Given below is the failed test case.
Failed case #6/16: (Wrong answer)
Input:
100 100
27 96
6 9
81 98
21 94
22 68
76 100
8 50
38 86
71 75
32 93
16 50
71 84
6 72
22 58
7 19
19 76
44 75
24 76
31 35
11 89
42 98
63 92
37 38
20 98
45 91
23 53
37 91
76 93
67 90
12 22
43 52
23 56
67 68
1 21
17 83
63 72
30 32
7 91
50 69
38 44
55 89
15 23
11 72
28 42
22 69
56 79
5 83
55 73
13 72
7 93
20 54
21 55
66 89
2 91
18 88
26 64
11 61
28 59
12 86
42 95
17 82
50 66
66 99
40 71
20 40
5 66
92 95
32 46
7 36
44 94
6 31
19 67
26 57
53 84
10 68
28 74
34 94
25 61
71 88
10 89
28 52
72 79
39 73
11 80
44 79
13 77
30 96
30 53
10 39
1 90
40 91
62 71
44 54
15 17
69 74
13 67
24 69
34 96
21 50
20 91
42 46
Your output:
0
Correct output:
1
The problem is that you do an early return from reach() if y cannot be reached via the first unvisited neighbor of x:
return reach(adj,x,y);
What you want to do instead is:
If y can be reached then return 1.
If y cannot be reached then continue with the next neighbor
That would read:
if (reach(adj, x, y) == 1)
return 1;
Unrelated comment: You may want to use Iterator<Integer> instead of the raw type Integer. This avoids the cast and compiler warnings.
I want to order numbers between the range of 1-100 with File Reader. It's very weird otput. My code:
import java.io.*;
import java.util.*;
public class Main {
public static String strLine;
public static int order = 1;
public static void main(String[] args) {
Queue<String> queue = new PriorityQueue<String>();
try{
FileReader fr = new FileReader("File.txt");
BufferedReader br = new BufferedReader(fr);
while ((strLine = br.readLine()) != null) {
queue.offer(strLine);
}
br.close();
} catch (IOException e){
System.out.println("File not found");
}
while (!queue.isEmpty()){
System.out.println(order + ".Number: " + queue.poll());
order++;
}
} }
File.txt:
81
56
42
3
49
100
61
76
74
79
6
90
34
93
22
71
72
63
67
18
14
5
91
39
53
86
40
45
60
55
48
62
8
4
80
11
87
47
15
46
97
66
43
58
41
54
33
96
82
44
98
27
85
10
32
95
35
24
7
30
83
9
31
99
25
29
57
36
69
50
28
59
12
37
78
73
70
89
65
51
77
88
20
17
21
94
64
19
26
23
92
52
2
16
68
75
38
1
84
13
output:
1.Number: 1
2.Number: 10
3.Number: 100
4.Number: 11
5.Number: 12
6.Number: 13
7.Number: 14
8.Number: 15
9.Number: 16
10.Number: 17
11.Number: 18
12.Number: 19
13.Number: 2
14.Number: 20
15.Number: 21
16.Number: 22
17.Number: 23
18.Number: 24
19.Number: 25
20.Number: 26
21.Number: 27
22.Number: 28
23.Number: 29
24.Number: 3
25.Number: 30
26.Number: 31
27.Number: 32
28.Number: 33
29.Number: 34
30.Number: 35
31.Number: 36
32.Number: 37
33.Number: 38
34.Number: 39
35.Number: 4
36.Number: 40
37.Number: 41
38.Number: 42
39.Number: 43
40.Number: 44
41.Number: 45
42.Number: 46
43.Number: 47
44.Number: 48
45.Number: 49
46.Number: 5
47.Number: 50
48.Number: 51
49.Number: 52
50.Number: 53
51.Number: 54
52.Number: 55
53.Number: 56
54.Number: 57
55.Number: 58
56.Number: 59
57.Number: 6
58.Number: 60
59.Number: 61
60.Number: 62
61.Number: 63
62.Number: 64
63.Number: 65
64.Number: 66
65.Number: 67
66.Number: 68
67.Number: 69
68.Number: 7
69.Number: 70
70.Number: 71
71.Number: 72
72.Number: 73
73.Number: 74
74.Number: 75
75.Number: 76
76.Number: 77
77.Number: 78
78.Number: 79
79.Number: 8
80.Number: 80
81.Number: 81
82.Number: 82
83.Number: 83
84.Number: 84
85.Number: 85
86.Number: 86
87.Number: 87
88.Number: 88
89.Number: 89
90.Number: 9
91.Number: 90
92.Number: 91
93.Number: 92
94.Number: 93
95.Number: 94
96.Number: 95
97.Number: 96
98.Number: 97
99.Number: 98
100.Number: 99
Process finished with exit code 0
Since it is a queue of Strings it is sorted alphabetically. If you want to keep it as Strings, you can use a custom comparator that converts the Strings to ints and compares those:
// This constructor requires an initial capacity before the comparator
Queue<String> queue = new PriorityQueue<String>(100, new Comparator<String>() {
public int compare(String s1, String s2) {
return Integer.parseInt(s1) - Integer.parseInt(s2);
}
});
Another option is to change the type of the queue to Integer and convert the input:
Queue<Integer> queue = new PriorityQueue<Integer>();
...
while ((strLine = br.readLine()) != null) {
queue.offer(Integer.parseInt(strLine));
}
so basically I am trying to write my own custom iterator to print every second number of an ArrayList. I populated the arraylist with ints from 0 up to 100.
But somehow it still not using my custom Iterator, oddly print out every single int in my arrayList.
Am I approaching this the wrong way?
Code:
public class IteratorTesting implements Iterable{
ArrayList<Integer> rand;
public IteratorTesting(){
rand = new ArrayList<>();
}
public ArrayList<Integer> getRand(){
return rand;
}
public int getSize(){
return rand.size();
}
#Override
public Iterator iterator() {
return new MyIterator();
}
public class MyIterator implements Iterator<Object>{
int currentIndex=0;
#Override
public boolean hasNext() {
if(currentIndex<rand.size()){
return true;
}else{
return false;
}
}
#Override
public Object next() {
return rand.get(currentIndex+=2);
}
}
public static void main(String[] args) {
IteratorTesting name = new IteratorTesting();
for (int i = 0; i < 100; i++) {
name.getRand().add(i);
}
Iterator it = name.getRand().iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
Output:
run:
0
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
On this line
Iterator it = name.getRand().iterator();
you've fetched the Iterator for the ArrayList, instead of your custom iterator. So you're just iterating the list in the regular way.
Hy!
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException?
package studing;
public class Array {
public static void main(String[] args) {
int i;
int[] ar1 = new int[50];
for(i = 0; i < ar1.length; i++)
{
ar1[i] = i * 2 + 1;
System.out.print(ar1[i] + " ");
};
System.out.println();
for(i = ar1.length; i > -1; i--)
{
ar1[i] = i * 2 - 1;
System.out.print(ar1[i]);
};
}
}
After compiling the console displays:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95
97 99
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
50 at studing.Array.main(Array.java:18)
I want the output console in the second row 99 97 95 93 ...
Thanks in advance!
You start at ar1.length in your second loop, which is out of bounds.
To get it to work, you need to start at ar1.length-1, which is the maximum index for your array.
This is because arrays are 0-based in Java, as noted in the comments by #Maroun Maroun.
The first time the second for loop runs, i = 50 (the initial value), which is beyond the end of the array. This value is used before the index update of i--, so ar1[50] is out of bounds (proper indexes are 0 to 49 for a 50 element array).