An example of a java.util.NoSuchElementException error

Source: Internet
Author: User

Reprinted from http://blog.csdn.net/ppact/article/details/1688445

Wrote an example of copying a file from A to B, which requires that duplicate rows be removed.

So think of set, which would have been very easy, and the result of writing data out of the wrong

Java.util.NoSuchElementException

On the network repeatedly check also no related content, so his slowly find, finally found. Below to write the example of this error and analysis, I hope to timely help and I also made a small mistake rookie!

Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStream;
Import Java.io.OutputStreamWriter;
Import Java.io.PrintWriter;
Import Java.util.HashSet;
Import Java.util.Iterator;
Import Java.util.Set;

public class Step01 {

public static void Main (string[] args) {
set<string> set = new hashset<string> ();
File in = new file ("Answer0712/aaa.txt");
File out = new file ("Answer0712/bbb.txt");
InputStream is = null;
OutputStream OS = null;
try {
is = new FileInputStream (in);
OS = new FileOutputStream (out);
BufferedReader br = new BufferedReader (new InputStreamReader (IS));
while (true) {
String str = br.readline ();
if (str = = null)
Break
Set.add (str);
}
Iterator i = Set.iterator ();
while (I.hasnext ()) {
SYSTEM.OUT.PRINTLN ("Null value judgment:" +i.next ());
// }
PrintWriter pw = new PrintWriter (New BufferedWriter (
New OutputStreamWriter (OS)), true);
Iterator i = Set.iterator ();
while (I.hasnext ()) {
System.out.println (I.next ()); This sentence is the culprit .
Pw.println (I.next ());
}
Pw.flush ();
} catch (Exception e) {
E.printstacktrace ();
} finally {
try {
Is.close ();
Os.close ();
} catch (IOException e) {
E.printstacktrace ();
}

}

}
}

The article marked the yellow one, originally to print out, in the console to see if the data written is correct.

The result is a java.util.NoSuchElementException error, that is, the yellow one, and one more iterator next ().

So it's wrong. It would be better to remove it, or instead

while (I.hasnext ()) {
string ss = (string) i.next ();
SYSTEM.OUT.PRINTLN (ss);
PW.PRINTLN (ss);
can solve the problem.

An example of a java.util.NoSuchElementException error

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.