-2

我很好奇是否有人可以帮助我理解我做错了什么。我只是在学习如何在 Java 中使用 OutputStreams 和 BufferedWriters,但我不确定我做得对。我知道如何使用一个 OutputStream 和 BufferedWriter,但我的问题是试图在一个类中使用它们中的两个。

现在我得到的主要错误在于我的 LowerAndUpper 类,并且在我的 try/catch 语句和 if/else 语句之间,我不确定我做错了什么。因此,我很感谢你们在这件事上提供的任何帮助,并帮助我了解如何同时使用其中两个项目。

这些是我遇到的错误,由于我还是 Java 的初学者,所以我并不完全理解这里发生了什么:

第 40 行错误:')' 如果 (creditsEarned>60 writerUpper.write){

第 40 行错误:不是语句 if (creditsEarned>60 writerUpper.write){

第 40 行错误:';' 预期如果(creditsEarned>60 writerUpper.write){

第 43 行错误:没有 'if' 的 'else' else (writerLower.write){

第 43 行错误:不是其他语句(writerLower.write){

第 43 行错误:';' 其他预期(writerLower.write){

这是我的代码:

import java.nio.file.*;
import java.io.*;
import static java.nio.file.StandardOpenOption.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LowerAndUpper {


public static void main(String[] args) {
    Path file1 =
            Paths.get("C:/temp/lowerclassman.txt");
    Path file2 =
            Paths.get("C:/temp/upperclassman.txt");
    String s = "";
    String delimiter = ",";
    int id;
    String name;
    double creditsEarned;
    final int QUIT = 999;
    try {
        OutputStream output = new BufferedOutputStream(Files.newOutputStream(file1));
        BufferedWriter writerUpper = new BufferedWriter(new OutputStreamWriter(output));
        OutputStream output2 = new BufferedOutputStream(Files.newOutputStream(file2));
        BufferedWriter writerLower = new BufferedWriter(new OutputStreamWriter(output2));
        while (true) {
            id = Validate.collectInt("Enter student ID number or " + QUIT
                    + " to quit >> ");
            if (id == QUIT) {
                break;
            }
            name = Validate.collectString(2, "Enter student name "
                    + id + " >> ");
            creditsEarned = Validate.collectWage("Enter credit hours >> ");
            s = id + delimiter + name + delimiter + creditsEarned;
            if (creditsEarned>60 writerUpper.write){
              System.out.println("Student is a Lowerclassman");

            else (writerLower.write){
 System.out.println("Student is an Upperclassman");
}
        }
           writerUpper.write(s, 0, s.length());
            r.newLine();

        } //end while


        writer.close();
        writer2.close();
    } catch (Exception e) {
        System.out.println("Message: " + e);
    }

}
}


// **************************************************************************

class Validate {

public static int collectInt(String messageIn) {
    Scanner input = new Scanner(System.in);
    int intOut = 0;
    boolean valid = true;

    System.out.println(messageIn);
    while (valid) {
        try {
            intOut = input.nextInt();
            valid = false;
        } catch (InputMismatchException ie) {
            input.nextLine();
            System.out.println("You must enter a whole number");
        } //end catch
    } //end while

    return intOut;

}//end collectInt method

//*************************************************************************
public static String collectString(int strLen, String messageIn) {
    Scanner input = new Scanner(System.in);
    String strOut = "";
    boolean valid = true;

    System.out.println(messageIn);
    while (valid) {
        try {
            strOut = input.nextLine();
            if (strOut.length() < strLen) {
                throw new Exception();
            }
            valid = false;
        } catch (Exception e) {
            System.out.printf("You must be at least %s characters\n",
                    strLen);
        } //end catch
    } //end while

    return strOut;
} //end collectString method

//*************************************************************************
public static String collectZipcode(String messageIn) {
    Scanner input = new Scanner(System.in);
    String strOut = "";
    boolean valid = true;

    System.out.println(messageIn);
    while (valid) {
        try {
            strOut = input.nextLine();
            Integer.parseInt(strOut);
            if (strOut.length() != 5) {
                throw new Exception();
            }
            valid = false;
        } catch (NumberFormatException ne) {
            System.out.println("Please enter a valid zip code");
        } catch (Exception e) {

            System.out.printf("A zip code should be 5 numbers long");
        } //end catch
    } //end while

    return strOut;
}//end collectZipcode method

//*************************************************************************
public static String collectEmail(String messageIn) {
    String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
    Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);

    Scanner input = new Scanner(System.in);
    String strOut = "";
    boolean valid = true;

    System.out.println(messageIn);
    while (valid) {
        try {
            strOut = input.nextLine();
            CharSequence emailChk = strOut;
            Matcher matcher = pattern.matcher(emailChk);
            if (!matcher.matches()) {
                throw new Exception();
            }
            valid = false;
        } catch (Exception e) {
            System.out.printf("Please try again with a valid email "
                    + "address\n");
        } //end catch
    } //end while

    return strOut;

}//end collectEmail method

//*************************************************************************
public static Double collectWage(String messageIn) {

    Scanner input = new Scanner(System.in);
    double dblOut = 0;
    boolean valid = true;

    System.out.println(messageIn);
    while (valid) {
        try {
            dblOut = input.nextDouble();
            valid = false;
        } catch (InputMismatchException ie) {
            input.nextLine();
            System.out.println("You must enter a whole number ");
        } //end catch
    } //end while

    return dblOut;

}//end collectInt method

//*************************************************************************
public static String collectPhone(String messageIn) {
    String expression = "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$";
    Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);

    Scanner input = new Scanner(System.in);
    String strOut = "";
    boolean valid = true;

    System.out.println(messageIn);
    while (valid) {
        try {
            strOut = input.nextLine();
            CharSequence phoneChk = strOut;
            Matcher matcher = pattern.matcher(phoneChk);
            if (!matcher.matches()) {
                throw new Exception();
            }
            valid = false;
        } catch (Exception e) {
            System.out.printf("Please try again with a valid phone "
                    + "number\n");
        } //end catch
    } //end while

    return strOut;

}//end collectPhone method

//*************************************************************************
public static String collectSsn(String messageIn) {
    String expression = "^\\d{3}[- ]?\\d{2}[- ]?\\d{4}$";
    Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);

    Scanner input = new Scanner(System.in);
    String strOut = "";
    boolean valid = true;

    System.out.println(messageIn);
    while (valid) {
        try {
            strOut = input.nextLine();
            CharSequence ssnChk = strOut;
            Matcher matcher = pattern.matcher(ssnChk);
            if (!matcher.matches()) {
                throw new Exception();
            }
            valid = false;
        } catch (Exception e) {
            System.out.printf("Please try again with a valid social security "
                    + "number\n");
        } //end catch
    } //end while

    return strOut;

}//end collectSsn 
} //end Validate Class

提前感谢您的所有帮助。

4

1 回答 1

0

首先,您已经初始化:

BufferedWriter writerUpper = new BufferedWriter(new OutputStreamWriter(output));

但随后使用了另外两个变量名称:

writer.close();

和:

r.newLine();

可以在本文档中找到 BufferedWriter 的正确用法:http: //docs.oracle.com/javase/7/docs/api/java/io/BufferedWriter.html

至于 if 和 else 语句,请确保您的代码格式正确。if 语句的一般用法是有一个条件:

if(condition) 
{
    //do something
}
else
{
    //do something else
}

即针对您的情况:

if (creditsEarned>60 writerUpper.write){
          System.out.println("Student is a Lowerclassman");

        else (writerLower.write){
System.out.println("Student is an Upperclassman");
}
}

应该:

if (creditsEarned> 60){
    writerUpper.write(s, 0, s.length());
    writerUpper.newLine();
}
else
{
    writerLower.write(s, 0, s.length());
    writerLower.newLine();
}

然后确保使用与初始化相同的变量名称:

writerLower.close();
writerUppder.close();
于 2013-11-04T01:06:01.193 回答