Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Input output file help

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Input output file help

    Hey all, this is my first post and its nice to meet you all.

    I appreciate all your help.

    I have most of my program done already, but I'm having some problems with input and output files. There are 2 files of each. Lab4Input1.txt and 2 and Lab4Output1.txt and 2. The problem I'm having is selecting which file a user picks.

    The code is as follows for what I'm having issues with:

    Scanner keyIn = new Scanner(System.in); //assigns "keyIn" to keyboard
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    int fileNum;
    String inName;
    String outName;
    boolean inputBad = true;

    do
    {
    System.out.print("Please enter file number, 1 or 2 > ");
    fileNum = keyIn.nextInt( );

    if (fileNum == 1 || fileNum == 2);
    boolean inputBad = false;


    }
    while (inputBad = true);



    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    if (fileNum == 1)
    {
    Scanner inName = new Scanner(new FileReader("Lab4Input1.txt"))
    PrintWriter outName = new PrintWriter("Lab4Output1.txt");
    }
    else
    {
    inName = "Lab4Input2.txt";
    outName = "Lab4Output2.txt";
    }

    The first part I don't understand, its what my teacher said, but I dont know what it means. Why do I need to the inputBad = false; stuff.

    The second part is what I am really stuck on. A user is supposed to type in which file he wants to choose, 1 or 2. My problem is making the code for how it chooses which files to use.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Input output file help

    The first part is just making sure the user enters a 1 or 2. If the user enters something else, it will repeat the question.

    As for the second part, read this: Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member OutputStream's Avatar
    Join Date
    Apr 2011
    Posts
    32
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 3 Posts

    Default Re: Input output file help

    while (inputBad = true);

    Shouldn't that be:

    while (inputBad == true);

    Or even shorter:

    while (inputBad);

  4. #4
    Member
    Join Date
    Jun 2010
    Posts
    75
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Input output file help

    Also, one other thing is that your if statement contains a null statement because you follow it immediately with a semicolon. If you want the next line of code to execute when the if condition is fulfullied, remove the semicolon.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Input output file help

    Quote Originally Posted by mjpam View Post
    Also, one other thing is that your if statement contains a null statement because you follow it immediately with a semicolon. If you want the next line of code to execute when the if condition is fulfullied, remove the semicolon.
    Good catch. Better yet, always use {} blocks for if statements and loops.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. [SOLVED] Need help with input/output error
    By stefan2892 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 7th, 2011, 10:44 AM
  2. [SOLVED] T_T [hellllppppp] Input/Output
    By bontet in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: October 29th, 2010, 11:32 AM
  3. Input/Output file help
    By Plural in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 25th, 2010, 08:34 PM
  4. Input/Output file help
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 23rd, 2010, 06:26 PM
  5. [SOLVED] java me how to: input - math operation - output
    By Lifer in forum Java ME (Mobile Edition)
    Replies: 3
    Last Post: April 7th, 2010, 05:36 PM