在Java中使用两个OutputStreams和BufferedWriter
我很好奇是否有人能帮我理解我做错了什么。我只是在学习如何在Java中使用OutputStreams和BufferedWriter,我不确定我是否做对了。我知道如何使用一个OutputStream和BufferedWriter,但我的问题是试图在一个类中使用其中的两个 现在,我遇到的主要错误是在我的上下层阶级中,在我的try/catch语句和if/else语句之间,我不确定我做错了什么。所以我非常感谢你们在这件事上给我的任何帮助,以及帮助我理解如何同时使用其中两件物品 这些是我遇到的错误,因为我还是Java的初学者,所以我不太清楚这里发生了什么: 第40行错误:“)”应为 如果(creditsEarned>60 writerUpper.write){ 第40行错误:不是语句 如果(creditsEarned>60 writerUpper.write){ 第40行错误:应为“;” 如果(creditsEarned>60 writerUpper.write){ 第43行错误:“else”不带“if” else(writerLower.write){ 第43行错误:不是语句 else(writerLower.write){ 第43行错误:应为“;” else(writerLower.write){ 这是我的密码:在Java中使用两个OutputStreams和BufferedWriter,java,compiler-errors,fileoutputstream,bufferedwriter,Java,Compiler Errors,Fileoutputstream,Bufferedwriter,我很好奇是否有人能帮我理解我做错了什么。我只是在学习如何在Java中使用OutputStreams和BufferedWriter,我不确定我是否做对了。我知道如何使用一个OutputStream和BufferedWriter,但我的问题是试图在一个类中使用其中的两个 现在,我遇到的主要错误是在我的上下层阶级中,在我的try/catch语句和if/else语句之间,我不确定我做错了什么。所以我非常感谢你们在这件事上给我的任何帮助,以及帮助我理解如何同时使用其中两件物品 这些是我遇到的错误,因为我还
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
导入java.nio.file.*;
导入java.io.*;
导入静态java.nio.file.StandardOpenOption.*;
导入java.nio.file.attribute.BasicFileAttributes;
导入java.nio.file.attribute.FileTime;
导入java.util.InputMismatchException;
导入java.util.Scanner;
导入java.util.regex.Matcher;
导入java.util.regex.Pattern;
公共阶级下层和上层{
公共静态void main(字符串[]args){
路径文件1=
get(“C:/temp/lowercassman.txt”);
路径文件2=
get(“C:/temp/upperclassman.txt”);
字符串s=“”;
字符串分隔符=“,”;
int-id;
字符串名;
双重信用;
最终int QUIT=999;
试一试{
OutputStream输出=新的BufferedOutputStream(Files.newOutputStream(file1));
BufferedWriter writerUpper=新的BufferedWriter(新的OutputStreamWriter(输出));
OutputStream output2=新的BufferedOutputStream(Files.newOutputStream(file2));
BufferedWriter writerLower=新的BufferedWriter(新的OutputStreamWriter(output2));
while(true){
id=Validate.collectInt(“输入学生id号或”+退出
+“退出>>”;
如果(id==退出){
打破
}
name=Validate.collectString(2,“输入学生姓名”
+id+“>>”;
creditsEarned=Validate.collectWage(“输入学分>>”;
s=id+delimiter+name+delimiter+creditsEarned;
如果(creditsEarned>60 writerUpper.write){
System.out.println(“学生是低年级学生”);
else(writerLower.write){
System.out.println(“学生是高年级学生”);
}
}
writeUpper.write(s,0,s.length());
r、 换行符();
}//结束时
writer.close();
writer2.close();
}捕获(例外e){
System.out.println(“消息:+e”);
}
}
}
// **************************************************************************
类验证{
公共静态整型集合整型(字符串messageIn){
扫描仪输入=新扫描仪(System.in);
int intOut=0;
布尔有效=真;
System.out.println(messageIn);
while(有效){
试一试{
intOut=input.nextInt();
有效=错误;
}捕获(输入不匹配异常ie){
input.nextLine();
System.out.println(“必须输入整数”);
}//端盖
}//结束时
返回输入;
}//结束收集方法
//*************************************************************************
公共静态字符串集合字符串(int strLen,String messageIn){
扫描仪输入=新扫描仪(System.in);
字符串strOut=“”;
布尔有效=真;
System.out.println(messageIn);
while(有效){
试一试{
strOut=input.nextLine();
if(strOut.length()BufferedWriter writerUpper = new BufferedWriter(new OutputStreamWriter(output));
writer.close();
r.newLine();
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();