Question

I was curious if anyone could help me understand what I'm doing wrong. I'm just learning how to use OutputStreams and BufferedWriters in Java and I'm not sure I'm doing it right. I know how to use one OutputStream and BufferedWriter but my problem is trying to use two of them in one class.

Right now the main errors I'm getting lies in my LowerAndUpper class and is between my try/catch statements and in my if/else statements and I'm not sure what I'm doing wrong. So I'd appreciate any help you guys could give on the matter and in helping me understand how to use two of these items at once.

These are the errors I'm getting and since I'm still a beginner with Java in general I don't exactly understand what's going on here:

line 40 error: ')' expected if (creditsEarned>60 writerUpper.write){

line 40 error: not a statement if (creditsEarned>60 writerUpper.write){

line 40 error: ';' expected if (creditsEarned>60 writerUpper.write){

line 43 error: 'else' without 'if' else (writerLower.write){

line 43 error: not a statement else (writerLower.write){

line 43 error: ';' expected else (writerLower.write){

Here is my code:

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

Thanks in advance for all your help.

Was it helpful?

Solution

Firstly, you have initialised:

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

But then used two other varible names:

writer.close();

And:

r.newLine();

Correct usage of BufferedWriter can be found in this documentation:http://docs.oracle.com/javase/7/docs/api/java/io/BufferedWriter.html

As for the if and else statements, make sure you format your code correctly. General usage of an if statement is to have a condition:

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

Ie for your circumstance:

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

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

Should be:

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

And then make sure you use the same variable names as you initialised:

writerLower.close();
writerUppder.close();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top