Java 理解课堂的困难
我很难理解类是如何工作的,也不知道如何将我的currentToken转换成单词,然后使用单词类对其进行置乱或不置乱,然后将单词返回到println中 主要类别:Java 理解课堂的困难,java,class,Java,Class,我很难理解类是如何工作的,也不知道如何将我的currentToken转换成单词,然后使用单词类对其进行置乱或不置乱,然后将单词返回到println中 主要类别: import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; public class Readability { public static void main(S
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
public class Readability
{
public static void main(String[] args)
throws FileNotFoundException {
Scanner doc = new Scanner( new File("README.txt"));//imports README.txt
System.out.println(tokens);
}
public static ArrayList<String> punctuation(Scanner doc){//creates new method called punctuation
ArrayList<String> tokens = new ArrayList();//creates new arraylist called tokens
while (doc.hasNext()){//loops through README.txt to find all punctuation that needs to be fixed
String currentToken = doc.nextLine();//assigns the character that is being read as currentToken
Word wordObject = new Word(currentToken);
wordObject.toString();
if (currentToken.length()>0){//loop that converts all currentTokens to lowerCase and adds currentToken to token
tokens.add(currentToken);
}
}
return tokens;
}
}
import java.util.Scanner;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.ArrayList;
公共类可读性
{
公共静态void main(字符串[]args)
抛出FileNotFoundException{
Scanner doc=new Scanner(新文件(“README.txt”);//导入README.txt
System.out.println(代币);
}
公共静态ArrayList标点符号(Scanner doc){//创建名为标点符号的新方法
ArrayList tokens=new ArrayList();//创建名为tokens的新ArrayList
while(doc.hasNext()){//通过README.txt循环查找所有需要修复的标点符号
字符串currentToken=doc.nextLine();//指定作为currentToken读取的字符
Word Word对象=新词(currentToken);
wordObject.toString();
if(currentToken.length()>0){//将所有currentToken转换为小写并将currentToken添加到token的循环
添加(currentToken);
}
}
归还代币;
}
}
词类:
import java.util.ArrayList;
import java.util.Collections;
public class Word{
String word;
public Word(String w){
word=w;
}
public String toString(){
if (word.length()<6) {
return word;
}
else {
String first = word.substring(0,2);
int length = word.length();
String middle = word.substring(2,length-2);
ArrayList<String> scrambled = new ArrayList<String>();
scrambled.add(middle);
Collections.shuffle(scrambled);
String last = word.substring(length-2);
return first+scrambled+last;
}
}
}
import java.util.ArrayList;
导入java.util.Collections;
公共类词{
字符串字;
公共字(字符串w){
字=w;
}
公共字符串toString(){
if(word.length()不确定这是否是您需要的,但可能会有所帮助
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.stream.Collectors;
public class Readability {
public static void main(String[] args) throws FileNotFoundException {
Scanner doc = new Scanner(new File("README.txt"));//imports README.txt
List<String> tokens = Readability.punctuation(doc);
tokens.stream().forEach(token -> System.out.println(token));
}
public static List<String> punctuation(Scanner doc) {//creates new method called punctuation
List<String> tokens = new ArrayList();//creates new arraylist called tokens
while (doc.hasNext()) {//loops through README.txt to find all punctuation that needs to be fixed
String currentToken = doc.nextLine();//assigns the character that is being read as currentToken
Word wordObject = new Word(currentToken);
if (currentToken.length() > 0) {//loop that converts all currentTokens to lowerCase and adds currentToken to token
tokens.add(wordObject.scramble());
}
}
return tokens;
}
}
class Word {
private String word;
public Word(String w) {
word = w;
}
public String scramble() {
if (word.length() < 6) {
return word;
} else {
String first = word.substring(0, 2);
int length = word.length();
String middle = word.substring(2, length - 2);
List<String> scrambled = new ArrayList<String>();
scrambled.add(middle);
Collections.shuffle(scrambled);
String scrambledWord = scrambled.stream().collect(Collectors.joining());
String last = word.substring(length - 2);
return first + scrambledWord + last;
}
}
}
import java.util.Collections;
导入java.util.List;
导入java.util.Scanner;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.ArrayList;
导入java.util.stream.collector;
公共类可读性{
公共静态void main(字符串[]args)引发FileNotFoundException{
Scanner doc=new Scanner(新文件(“README.txt”);//导入README.txt
列表标记=可读性。标点符号(doc);
tokens.stream().forEach(token->System.out.println(token));
}
公共静态列表标点符号(Scanner doc){//创建名为标点符号的新方法
List tokens=new ArrayList();//创建名为tokens的新ArrayList
while(doc.hasNext()){//通过README.txt循环查找所有需要修复的标点符号
字符串currentToken=doc.nextLine();//指定作为currentToken读取的字符
Word Word对象=新词(currentToken);
if(currentToken.length()>0){//将所有currentToken转换为小写并将currentToken添加到token的循环
add(wordObject.scramble());
}
}
归还代币;
}
}
类词{
私有字符串字;
公共字(字符串w){
字=w;
}
公共字符串加扰(){
if(word.length()<6){
返回词;
}否则{
第一个字符串=字。子字符串(0,2);
int length=word.length();
字符串中间=字。子字符串(2,长度-2);
列表加扰=新建ArrayList();
加上(中间);
收藏。洗牌(混乱);
String scrambledWord=scrambled.stream().collect(collector.joining());
最后一个字符串=字。子字符串(长度-2);
返回第一个+加扰的DWORD+最后一个;
}
}
}
您的程序有问题吗(哪一个)?@Henry My println给了我一个错误:找不到符号:变量标记。我也无法判断我当前的标记是否被word类读取并被加扰,以及这些标记是否会进入我的主类中的主方法。返回first+scrambled+last;这将导致问题,因为您连接的不是字符串ng@ValentinCarnu标点符号
方法是静态的。不需要重新设置可读性
@Valention我一点也没有提到。toString方法究竟为什么会返回列表?更改返回类型,您将遇到编译问题