难度理解课

时间:2018-11-27 08:59:54

标签: java class

我很难理解类是如何工作的,无法弄清楚如何将我的currentToken变成一个单词,然后使用单词class对其进行加扰或不加扰,然后将已加扰或未加扰的单词返回到println中。

主类:

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.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;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

不确定这是否是您所需要的,但也许有帮助

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;
        }
    }
}