Can someone help me figure out why this java code is not working? Thanks "test_input.txt" file: 34 + 89 - x * y23 cxo6y
Posted: Sat Feb 19, 2022 3:21 pm
Can someone help me figure out why this java code is not
working? Thanks
"test_input.txt" file:
34 + 89 - x * y23
cxo6y / 67z23 * 56& 34 * x
2 + x
These are the errors I get
BBHSBHB88a 284882898丑记8叫B8%994284折扣们 1 package sentenceutils; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.util.ArrayList; 6 import java.util.List; 7 import java.util.Scanner; 8 9 public class scanner1{ 10 11 public static void main(String[] args) { 12 List<Sentenceutils> sList = new ArrayList<Sentenceutils>(); 13 { 14 15 File file = new File("test_input.txt"); 16 17 Scanner scanner = Scanner FileInputStream(file)); 18 wh( scanner.hasNextLine()) { 19 String sent = scanner.nextLine(); 20 (sent.trim().length() > 0){ 21 Sentenceutils sutils = new Sentenceutils(sent); sList.add(sutils); 23 } } 25 System.out.println("File that was read:" + args[0]); 26 System.out.println("File contains " + slist.size() + " sentences. "); 27 System.out.println("Sentences reports: "); for (int line = 0; line < slist.size(); line++) { 30 System.out.println("Sentences " + line +";"); 31 System.out.println(slist.get(line).getSentence()); 32 33 Sentenceutils lineText = slist.get(line); 34 String[] tokens = lineText.getTokens(); 35 (int id = 0; id < tokens.length; id++) { 36 System.out.println(id+":"+tokens[id]); 37 } 38 String[] shingles = lineText.getShingles(); 39 (int id = 0; id < shingles.length; id++) { 40 (shingles[id] != null) { 41 System.out.print(shingles[id]+" "); } 43 } System.out.println(" "); 45 } 46 } 47 catch(Exception ex){ 46 } 47 catch(Exception ex){ 48 ex.printStackTrace(); 49 } 50 } 51 52 53 //package sentenceutils; 54 55 //import java.util.ArrayList; 56 //import java.util.List; 57 58 public class Sentenceutils { 59 private String sentence; 60 private String[] tokens; 61 private String[] shingles; 62 63 public Sentenceutils(String s) [ 64 this.sentence = s; 65 generate Tokens(); 66 generateshingles(); 67 1 68 69 private void generateshingles() { 70 List<String> shinglesList = new ArrayList<String>(); 71 for (int index=0; index < sentence.length()-1; index++) { 72 shinglesList.add(sentence.charAt(index)- +""+sentence.charAt(index+1)); 73 74 shingles = new String[sentence.length()]; 75 shinglesList.toArray(shingles); 76 } 77 78 private void generateTokens() { 79 tokens = sentence.split(" "); 80 } 81 82 public String getSentence() { 83 return sentence; 84 } 85 86 public String[] getTokens() { 87 return tokens; 88 } 89 90 } 91 } 92 93
scanner1.java:21: error: non-static variable this cannot be referenced from a static context Sentenceutils sutils = new Sentenceutils(sent); scanner1.java:38: error: cannot find symbol String[] shingles = lineText.getShingles(); A symbol: method getShingles() location: variable linetext of type scanner1. Sentenceutils 2 errors
working? Thanks
"test_input.txt" file:
34 + 89 - x * y23
cxo6y / 67z23 * 56& 34 * x
2 + x
These are the errors I get
BBHSBHB88a 284882898丑记8叫B8%994284折扣们 1 package sentenceutils; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.util.ArrayList; 6 import java.util.List; 7 import java.util.Scanner; 8 9 public class scanner1{ 10 11 public static void main(String[] args) { 12 List<Sentenceutils> sList = new ArrayList<Sentenceutils>(); 13 { 14 15 File file = new File("test_input.txt"); 16 17 Scanner scanner = Scanner FileInputStream(file)); 18 wh( scanner.hasNextLine()) { 19 String sent = scanner.nextLine(); 20 (sent.trim().length() > 0){ 21 Sentenceutils sutils = new Sentenceutils(sent); sList.add(sutils); 23 } } 25 System.out.println("File that was read:" + args[0]); 26 System.out.println("File contains " + slist.size() + " sentences. "); 27 System.out.println("Sentences reports: "); for (int line = 0; line < slist.size(); line++) { 30 System.out.println("Sentences " + line +";"); 31 System.out.println(slist.get(line).getSentence()); 32 33 Sentenceutils lineText = slist.get(line); 34 String[] tokens = lineText.getTokens(); 35 (int id = 0; id < tokens.length; id++) { 36 System.out.println(id+":"+tokens[id]); 37 } 38 String[] shingles = lineText.getShingles(); 39 (int id = 0; id < shingles.length; id++) { 40 (shingles[id] != null) { 41 System.out.print(shingles[id]+" "); } 43 } System.out.println(" "); 45 } 46 } 47 catch(Exception ex){ 46 } 47 catch(Exception ex){ 48 ex.printStackTrace(); 49 } 50 } 51 52 53 //package sentenceutils; 54 55 //import java.util.ArrayList; 56 //import java.util.List; 57 58 public class Sentenceutils { 59 private String sentence; 60 private String[] tokens; 61 private String[] shingles; 62 63 public Sentenceutils(String s) [ 64 this.sentence = s; 65 generate Tokens(); 66 generateshingles(); 67 1 68 69 private void generateshingles() { 70 List<String> shinglesList = new ArrayList<String>(); 71 for (int index=0; index < sentence.length()-1; index++) { 72 shinglesList.add(sentence.charAt(index)- +""+sentence.charAt(index+1)); 73 74 shingles = new String[sentence.length()]; 75 shinglesList.toArray(shingles); 76 } 77 78 private void generateTokens() { 79 tokens = sentence.split(" "); 80 } 81 82 public String getSentence() { 83 return sentence; 84 } 85 86 public String[] getTokens() { 87 return tokens; 88 } 89 90 } 91 } 92 93
scanner1.java:21: error: non-static variable this cannot be referenced from a static context Sentenceutils sutils = new Sentenceutils(sent); scanner1.java:38: error: cannot find symbol String[] shingles = lineText.getShingles(); A symbol: method getShingles() location: variable linetext of type scanner1. Sentenceutils 2 errors