-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacterIterator.java
More file actions
63 lines (50 loc) · 1.3 KB
/
Copy pathCharacterIterator.java
File metadata and controls
63 lines (50 loc) · 1.3 KB
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
package Tools;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class CharacterIterator extends FileIterator<Character> {
private Scanner sc;
public CharacterIterator(String path){
try {
sc = new Scanner(new File(path));
sc.useDelimiter("");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public CharacterIterator(){
sc = new Scanner(System.in);
}
boolean end = false;
@Override
public boolean hasNext() {
return sc.hasNext()||!end;
}
@Override
public Character next() {
if(!sc.hasNext()&&!end){
end = true;
current = '\\';
return '\\';//EOF
}else if(sc.hasNext()){
Character character = sc.next().charAt(0);
if(character=='\r'){
line++;
column=0;
}else if(character!='\n'){
column+=1;
}
current = character;
return character;
}else{
throw new NoSuchElementException();
}
}
public void skipLine() {
sc.nextLine();
column=0;
line++;
}
}