-1

我正在为家庭作业编写一些代码,我一生都无法弄清楚为什么它不会运行。它一直运行,直到我添加了文件读取方法,我知道它在另一个程序中工作。我直接从我的其他一些工作中获取了代码。那么,有谁能比我更擅长 Java 的人告诉我我做错了什么来解决这个问题?由于这是家庭作业,请不要告诉我如何解决任何其他问题,但我不会阻止有关它们的提示。

import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;

public class vowels_r_us {

//for file reading
private static FileInputStream inFile;
private static InputStreamReader inReader;
private static BufferedReader reader;

//pasrsing input from file
private static StringTokenizer strTkn;

public static void main(String[] args) 
{

    initFile(); //prepare file for reading

    while (reader.ready())//iterate as long as there is more avaliable data
    {
        String word, suffix, line;
        line = getWordSuffix();
        word = line.substring(0, line.indexOf(' '));
        suffix = line.substring(line.indexOf(' '));


    }
}

/*CONJUGATION METHODS*/
static String pluralize(String s)
{
    String pluralForm;
    switch (classifyEnding(s))
    {
        case 'c':
            pluralForm = s.concat("GH");
            break;
        case 'v':
            pluralForm = s.substring(0, s.length() - 1).concat("G");//drop last letter add G
            break;
        case 'd':
            pluralForm = s + s.charAt(s.length() - 1) +"H";//double last letter, then add H
            break;
        default:
            pluralForm = "If you are getting this something is broken, have fun debugging.";
            break;
    }
    return pluralForm;
}

static String addSuffix(String word, String suffix)
{
    String suffixAdded;
    switch (classifyEnding(word))
    {
        case 'c':
            suffixAdded = word + suffix;
            break;
        case 'v':
            if(isVowel(suffix.charAt(0)))
            {
                suffixAdded = word + suffix.substring(1);//word + drop first letter of suffix then add suffix
            }
            else
            {
                suffixAdded = word + suffix.charAt(0) + suffix;//word + first letter of suffix + suffix
            }
            break;
        case 'd':
            if(isVowel(suffix.charAt(0)))
            {
                suffixAdded = word + suffix.charAt(0) + suffix;//word + first letter of suffix + suffix
            }
            else
            {
                suffixAdded = trimRepeatSequence(word) + suffix;
            }
            break;
        default:
            suffixAdded = "If you are getting this something is broken, have fun debugging.";
            break;
    }
    return suffixAdded;
}
/*END CONJUGATION METHODS*/

/*STRING MODIFICATION AND TESTING METHODS*/
//removes lefmost vowel or consonant from sequence
static String trimRepeatSequence(String s)
{
    String editedString = "";
    boolean vBasedEnding = isVowel(s.charAt(s.length() - 1));

    for (int i = s.length() - 1; i >= 0; i--)
    {
        if (isVowel(s.charAt(i)) != vBasedEnding)
        {
            editedString = s.substring(0, i+1) + s.substring(i+2, s.length());
            break;
        }
    }

    return editedString;
}

/* classify endings in to three grammatical categories, single vowel ending 'v', single consonant ending 'c', double type ending 'd'
 */
static char classifyEnding(String s)
{
    char grammaticalClass;
    if (isVowel(s.charAt(s.length()- 1)) == isVowel(s.charAt(s.length()- 2)))
    {
        grammaticalClass = 'd';
    }
    else
    {
        grammaticalClass = isVowel(s.charAt(s.length()- 1)) == true? 'v' : 'c';
    }
    return grammaticalClass;
}

static boolean isVowel(char c)
{
    boolean b;//rename
    switch (Character.toLowerCase(c))
    {
        case 'a': case 'c':
        case 's': case 'l':
            b = true;
            break;

        default:
            b = false;
            break;
    }
    return b;
}
/*END STRING MODIFICATION AND TESTING METHODS*/

/*FILE READER METHODS*/
//read file for input
public static void initFile() throws IOException
{
    inFile = new FileInputStream ("C:\\Users\\Tom\\Dropbox\\!!VHSAPCSData\\VHSP35data.txt");
    inReader = new InputStreamReader(inFile);
    reader = new BufferedReader(inReader);
}

public static String getWordSuffix() throws IOException
{
    String line;

    line = reader.readLine();

    return line;
}
}
4

1 回答 1

2

您需要将 IO 代码包装在 try/catch 中:

import java.io.*;
import java.util.Scanner;

public class ScanXan {
    public static void main(String[] args) throws IOException {

        Scanner s = null;

        try {
            s = new Scanner(new BufferedReader(new FileReader("xanadu.txt")));

            while (s.hasNext()) {
                System.out.println(s.next());
            }
        } finally {
            if (s != null) {
                s.close();
            }
        }
    }
}

取自:http ://docs.oracle.com/javase/tutorial/essential/io/scanning.html

于 2012-12-17T20:56:36.900 回答