The code below is the entire program so far (just to avoid any missing pieces.
The issue I'm having is that I'm trying use the .substring(int,int) method to pull two characters from the given string and write the two characters pulled into a separate array. The problem is that every character pulled from the string using .substring() is a blank space. There are no letters pulled at all. I tested the function using a simple print method and it went to show that printing sentences.get(i).substring(j,j++) only prints blank spaces. It's populating my arrays with those empty spaces.
Any clue as to what could be causing this? My compiler isn't giving me any errors or warnings whatsoever.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class JacSim {
public static void main(String [] args) throws FileNotFoundException {
Scanner userScanner = new Scanner(System.in);
System.out.println("Please enter the filename for your input file (with extension).");
String fileName = userScanner.nextLine();
File file = new File(fileName);
Scanner scanner = new Scanner( new FileInputStream(file));
List<String> sentences = new ArrayList();
while (scanner.hasNextLine()) {
String sentence = scanner.nextLine();
sentences.add(sentence);
}
System.out.println("Input Sentences:\n");
for(int i=0;i<sentences.size();i++) {
System.out.println(i + " : " + sentences.get(i));
}
List<List<String>> shingles = new ArrayList();
sentences.stream().forEach((String _item) -> {
shingles.add(new ArrayList());
});
System.out.println("\nSorted Shingle Arrays:\n");
shinglesMethod(shingles, sentences);
}
private static List shinglesMethod(List<List<String>> shingles, List<String> sentences) {
for (int i=0;i<sentences.size();i++) {
for(int j=0;j++<sentences.get(i).length();j++) {
shingles.get(i).add(sentences.get(i).substring(j,j++));
}
showList( i, shingles.get(i) );
}
return shingles;
}
private static void showList( int n, List List ) {
System.out.print(n + " : ");
List.stream().forEach((o) -> {
System.out.print( o + " " );
});
System.out.println();
}
The chunk of code to pay attention to is
for (int i=0;i<sentences.size();i++) {
for(int j=0;j++<sentences.get(i).length();j++) {
shingles.get(i).add(sentences.get(i).substring(j,j++));
}
showList( i, shingles.get(i) );
}
Forgot to clarify that the scanner is reading in the words properly and that each string is read as expected. The only issue I'm finding is with the .substring() method.
Aucun commentaire:
Enregistrer un commentaire