如果有人能帮我理解我做错了什么,我很好奇。我只是学习如何在Java中使用OutputStreams和BufferedWriters,我不确定我是否正确。我知道如何使用一个OutputStream和BufferedWriter,但我的问题是尝试在一个类中使用其中两个。
现在我遇到的主要错误在于我的LowerAndUpper类,并且在我的try / catch语句和我的if / else语句之间,我不确定我做错了什么。所以我很感激你们能帮助我解决这个问题并帮助我理解如何同时使用这两个项目。
这些是我得到的错误,因为我仍然是Java的初学者,所以我并不完全理解这里发生了什么:
第40行错误:')'预计 if(creditsEarned> 60 writerUpper.write){
第40行错误:不是声明 if(creditsEarned> 60 writerUpper.write){
第40行错误:';'预期 if(creditsEarned> 60 writerUpper.write){
第43行错误:'else'没有'if' else(writerLower.write){
第43行错误:不是声明 else(writerLower.write){
第43行错误:';'预期 else(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
提前感谢您的帮助。
答案 0 :(得分: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();