Results 1 to 8 of 8
  1. #1
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Unexpected Output… Please help

    I expect the given code to write the following in the text file named OutFile.txt in the src folder (and a copy of this file is present the main directory of the project, where other folders like src and res are present) of this project:
    Java Code:
    Value at : 0 = 0
    Value at : 1 = 1
    Value at : 2 = 2
    But it does not write anything in the file. and gives this output in console:

    Java Code:
    0
    0
    
    0
    0
    0


    CODE:-

    Java Code:
    package exception_handling_1;
    
    //Note: This class won't compile by design!
    import java.io.FileWriter;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.List;
    
    
    public class ListOfNumbers {
    
    
     private List<Integer> list;
     private static final int SIZE = 3;
    
    
     public ListOfNumbers () {
         list = new ArrayList<Integer>(SIZE);
         for (int i = 0; i < SIZE; i++) {
             list.add(new Integer(i));
         }
     }
     
     
     static PrintWriter out;
     
     public void writeList() {
         //PrintWriter out=null;
         try{
         out = new PrintWriter(new FileWriter("OutFile.txt"));
         for (int i = 0; i < SIZE; i++) {
             out.println("Value at: " + i + " = " + list.get(i));
         }
         }catch (Exception e){
             System.out.println("IO Exception!");
         }finally {
             out.close();
             }
     }
     
     
    
    }


    Please tell me why am I getting this output. Thank you.
    Last edited by Nazneen Ali; 11-20-2013 at 04:35 PM. Reason: formatting
    Don't forget to smile :-)

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

    Default Re: Unexpected Output… Please help

    Aren't you missing some code? Where is your main entry point? Where do you print anything or call writeList()? And what about that first comment?

    If your class won't compile by design, how can you expect it to do anything?

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

  3. #3
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Unexpected Output… Please help

    How are you calling that class? Why do you get any console output? The only System.out I see is line 35.

    EDIT: Dammit, Jim!
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,907
    Rep Power
    5

    Default Re: Unexpected Output… Please help

    Quote Originally Posted by PhHein View Post

    EDIT: Dammit, Jim!
    Don't blame Jim, type faster!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,953
    Rep Power
    19

    Default Re: Unexpected Output… Please help

    Quote Originally Posted by gimbal2 View Post
    Don't blame Jim, type faster!
    <McCoy>He's a developer, not a typist!</McCoy>
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Unexpected Output… Please help

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

  7. #7
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Re: Unexpected Output… Please help

    I am sorry.

    I forgot main(), was reading a tutorial and I just copied their code and modified and started running it, and on the top of it, eclipse didn't complain about not having the main() and rather printed something. I think I did oversee the comment as well.
    Don't forget to smile :-)

  8. #8
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    100
    Rep Power
    0

    Default Re: Unexpected Output… Please help

    take a look at the IO class object documentation

Similar Threads

  1. Unexpected behavior
    By suhaas.mohandos@gmail.com in forum New To Java
    Replies: 9
    Last Post: 08-19-2013, 09:20 AM
  2. unexpected NullPointerException
    By dawnMist in forum New To Java
    Replies: 8
    Last Post: 11-07-2012, 05:40 PM
  3. Unexpected Output
    By ankiit in forum New To Java
    Replies: 20
    Last Post: 01-06-2012, 05:48 PM
  4. A basic method with an unexpected output
    By JONCOM in forum New To Java
    Replies: 6
    Last Post: 02-01-2011, 07:25 AM
  5. unexpected type
    By one in forum New To Java
    Replies: 13
    Last Post: 01-20-2009, 09:32 AM

Tags for this Thread

Posting Permissions

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