+ 6
what is the problem in this program
import java.util.ArrayList; import java.util.Iterator; public class Program { public static void main(String[] args) { ArrayList<Integer> list=new ArrayList<Integer>(); for(int i=0 ;i<6 ; i++){ list.add(i); } int x=0; Iterator<Integer> it= list.Iterator(); while (it.hasNext) { x+=(it.next); } System.out.println(x); } }
17 odpowiedzi
+ 5
15
+ 3
what is the problem in this program
import java.util.ArrayList;
import java.util.Iterator;
public class Program
{
public static void main(String[] args) {
ArrayList<Integer> list=new ArrayList<Integer>();
for(int i=0 ;i<6 ; i++){
list.add(i);
}
int x=0;
Iterator<Integer> it= list.Iterator();
while (it.hasNext)
{
x+=(it.next);
}
System.out.println(x);
}
}
Answer :- 15
0
change it.hasNext to it.hasNext()
also change it.next to it.next()
0
change it.hasNext to it.hasNext()
also change it.next to it.next()
/*and the answer is 15
0
15
0
15
0
15
0
15...
0
try {
File f = new File("a.txt");
Scanner sc = new
(f);
while (sc.hasNext()) {
String a =
.next();
String b = sc.
();
System.out.println(a + " " + b);
}
sc.
();
}
catch (Exception e) {
System.out.println("Error");
}
sc
close
new
next
Scanner
File
Halpe Me
0
Iterator
integer
iterator
hasNext
next
0
Drag and drop from the options below to read and print the content of the file a.txt, and then close it.
try {
File f = new File("a.txt");
Scanner sc = new (f);
while (sc.hasNext()) {
String a = .next();
String b = sc.();
System.out.println(a + " " + b);
}
sc.();
}
catch (Exception e) {
System.out.println("Error");
}
- 1
hasNext will be the hasNext()
- 1
The answer is :
15
- 1
Drag and drop from the options below to iterate and print all of the items in the list of Integers.
< > it = list. ();
while (it. ()) {
System.out.println(it. ());
}
Integer Iterator next iterator hasNext
- 1
15
- 2
answer is 15
- 3
< > it = list. ();
while (it. ()) {
System.out.println(it. ());