Results 1 to 4 of 4
  1. #1
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default How do I create an outfile and print the result of each expression??

    The prompt is: Create outfile names "exam1.txt" and print the result of each expression.

    That's it, verbatim, and it doesn't really make sense to me because of how it's worded. Anyways, it's due by 11:59, and I already did the expressions:

    Java Code:
    public class ExpressionResults {
    public static void main(String args[]) { 
    
    int a = 6 / 2 + 7 / 3;
    System.out.println("a = " + a);
    
    int b = 2 + 5 + 3 + 1;
    System.out.println("b = " + b);
    
    String c = 2 + " 2 + 3 " + 4;
    System.out.println("c = " + c);
    
    String d = 4 + 5 + 3 + " + 4 + 5 " + " equals to " + 4 + 5 + 3 + 4 + 5 + ".";
    System.out.println("d = " + d);
    
    String e = "hello 34 " + 5 * 3;
    System.out.println("e = " + e);
    
    int f = 17 % 10 / 2;
    System.out.println("f = " + f);
    
    String g = "\\hello\nhow\tare \"you\"?\\\\";
    System.out.println("g = " + g);
    
    double h = (char)('b' + 2);
    System.out.println("h = " + h);
    
    double i = 'A' + 'a';
    System.out.println("i = " + i);
    }
    }
    And can someone view my last question on this thread too please because it's due tonight at 11:59, and it's simple for you I guess...

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,651
    Rep Power
    5

    Default Re: How do I create an outfile and print the result of each expression??

    OK, here's a hint.

    Java Code:
    import java.io.PrintStream;
    
    public class ExpressionResults {
       public static void main(String args[]) {
    
          PrintStream out = System.out;
          int a = 6 / 2 + 7 / 3;
          out.println("a = " + a);
    
          int b = 2 + 5 + 3 + 1;
          out.println("b = " + b);
    
          String c = 2 + " 2 + 3 " + 4;
          out.println("c = " + c);
    
          String d = 4 + 5 + 3 + " + 4 + 5 " + " equals to " + 4 + 5 + 3 + 4 + 5
                + ".";
          out.println("d = " + d);
    
          String e = "hello 34 " + 5 * 3;
          out.println("e = " + e);
    
          int f = 17 % 10 / 2;
          out.println("f = " + f);
    
          String g = "\\hello\nhow\tare \"you\"?\\\\";
          out.println("g = " + g);
    
          double h = (char) ('b' + 2);
          out.println("h = " + h);
    
          double i = 'A' + 'a';
          out.println("i = " + i);
       }
    }
    Notice I haven't really changed your code in any functional way. System.out is nothing more that a static instance of a PrintStream with the console as the output device. I simply assigned System.out to another PrintStream type named "out" and changed all the System.out references to out. So it still runs as it did before.

    Now all you have to do is look up the API for PrintStream and create a new instance which prints to a file. Then just assign that instance to "out" and you have it.

    Note: Don't forget to close the PrintStream before your program terminates.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default Re: How do I create an outfile and print the result of each expression??

    OMG, I DID IT! by myself (as in not copy, paste) this time, lol... and it wasn't even that hard, lol. Thanks.

    Java Code:
    import java.io.PrintStream;
    import java.io.FileOutputStream;
    import java.io.FileNotFoundException;
    
    public class ExpressionResults {
    public static void main(String[] args) throws FileNotFoundException { 
    
    PrintStream outfile = new PrintStream(new FileOutputStream("exam1.txt"));
    System.setOut(outfile);
    
    int a = 6 / 2 + 7 / 3;
    outfile.println("a = " + a);
    
    int b = 2 + 5 + 3 + 1;
    outfile.println("b = " + b);
    
    String c = 2 + " 2 + 3 " + 4;
    outfile.println("c = " + c);
    
    String d = 4 + 5 + 3 + " + 4 + 5 " + " equals to " + 4 + 5 + 3 + 4 + 5 + ".";
    outfile.println("d = " + d);
    
    String e = "hello 34 " + 5 * 3;
    outfile.println("e = " + e);
    
    int f = 17 % 10 / 2;
    outfile.println("f = " + f);
    
    String g = "\\hello\nhow\tare \"you\"?\\\\";
    outfile.println("g = " + g);
    
    double h = (char)('b' + 2);
    outfile.println("h = " + h);
    
    double i = 'A' + 'a';
    outfile.println("i = " + i);
    }
    }

  4. #4
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default Re: How do I create an outfile and print the result of each expression??

    CAN YOU OR SOMEONE ELSE HELP ME W/ THIS THOUGH BEFORE 11:59???:

    Scalable method, using constant, to print figures; use SIZE value

Similar Threads

  1. Replies: 2
    Last Post: 07-18-2011, 07:11 AM
  2. Replies: 3
    Last Post: 02-25-2011, 04:34 PM
  3. A create a print view with grids.
    By AcousticBruce in forum New To Java
    Replies: 4
    Last Post: 02-24-2011, 02:15 AM
  4. Create program to evaluate prefix expression
    By Debonairj in forum New To Java
    Replies: 1
    Last Post: 08-12-2010, 02:35 AM
  5. Replies: 1
    Last Post: 08-07-2007, 05:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •