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 6 of 6

Thread: Please Help. Urgent. For loops and printing to output statement.

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Please Help. Urgent. For loops and printing to output statement.

    Hello all,


    I am making a program that takes user entered amounts and makes a table of random numbers..

    However,

    I am trying to make the user entered amounts show up in the terminal window and output file...I cannot figure this out after hours of trying.

    Someone PLEASE help!

    I will really appreciate..

    here is my current code

    It is printing fine in the terminal window but not the output file..I need the same thing in the terminal and output ||


     rnumber = randomGenerator.nextInt(hnumber);
     
               System.out.println();
               int counter = rnumber;
               for (int rows = 1; rows <= rownumber; ++rows) 
                    {
                    for (int columns = 1; columns <= cnumber; ++columns) 
                    {
     
                        rnumber = randomGenerator.nextInt(hnumber);
                        rnumber++;
                        System.out.printf("%4d", rnumber);
     
                        PrintWriter myOut;
                        try { myOut = new PrintWriter (new FileOutputStream("out6.dat"));
     
     
     
     
                                    myOut.printf("%4d", rnumber);
     
     
     
                        }
                        System.out.println();
                    }
                    myOut.close();
                }
     
                        catch (IOException e) {
                            System.out.println("exception is "+e.getMessage());
                        }
    Last edited by pbrockway2; March 17th, 2013 at 12:09 AM. Reason: code tags added


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,307
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Please Help. Urgent. For loops and printing to output statement.

    What is written to the output file?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Please Help. Urgent. For loops and printing to output statement.

    Quote Originally Posted by Norm View Post
    What is written to the output file?

    UPDATE:

    This is what I've gotten so far.. I will post my whole code & terminal window &output file.. The problem is my program is not giving me the correct output file but the terminal window is correct..


    What is in the terminal window:
    "
    This program will generate random numbers.

    Enter the lowest value possible (0 or 1): 1

    Enter the highet value possible: 100

    How many numbers would you like?: 25

    How many rows of numbers would you like?: 5

    How many columns of numbers would you like?: 5

    90 50 59 55 65
    34 75 35 72 42
    98 67 8 39 42
    31 61 98 41 60
    81 69 74 75 1


    "

    All of the above is user entered information.

    Output file has just one number in it. The number 1. However it chnages everytime with a different number, but only one number.

    I need the output file to be the same consistenly with the terminal window. Someone please help. I've spent many many hours on this so far and I need to finish it.










    import java.util.Scanner;
    import java.util.Random;
    import java.io.*;

    public class project6
    {
    public static void main (String [] args)
    {

    Scanner keyboard = new Scanner(System.in);
    Random randomGenerator = new Random();
    int number, hnumber, tnumber, rnumber, rownumber, cnumber;

    System.out.println("This program will generate random numbers.");
    System.out.println();



    do {
    System.out.print("Enter the lowest value possible (0 or 1): ");
    number = keyboard.nextInt();
    String junk = keyboard.nextLine();
    if ((number == 0) || (number == 1)){
    break;

    }
    else{
    System.out.println("Sorry, you need to enter a zero or 1 for the lowest number. \nTry again.");


    }
    } while ((number ==0)||(number ==1));

    System.out.print("\nEnter the highet value possible: ");
    hnumber = keyboard.nextInt();
    String junk = keyboard.nextLine();

    System.out.print("\nHow many numbers would you like?: ");
    tnumber = keyboard.nextInt();
    junk = keyboard.nextLine();

    System.out.print("\nHow many rows of numbers would you like?: ");
    rownumber = keyboard.nextInt();
    junk = keyboard.nextLine();

    System.out.print("\nHow many columns of numbers would you like?: ");
    cnumber = keyboard.nextInt();
    junk = keyboard.nextLine();








    rnumber = randomGenerator.nextInt(hnumber);
    PrintWriter myOut;
    System.out.println();
    int counter = rnumber;

    for (int rows = 1; rows <= rownumber; ++rows)
    {
    for (int columns = 1; columns <= cnumber; ++columns)
    {

    rnumber = randomGenerator.nextInt(hnumber);
    rnumber++;
    System.out.printf("%4d", rnumber);




    try { myOut = new PrintWriter (new FileOutputStream("out6.dat"));

    myOut.printf("%4d", rnumber);

    myOut.close();
    }



    catch (IOException e) {
    System.out.println("exception is "+e.getMessage());
    }



    }
    System.out.println();
    }



    }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,307
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Please Help. Urgent. For loops and printing to output statement.

    Output file has just one number in it.
    That sounds like the code is writing the current number to the file replacing all the other values written to the file. Only the last number written will be in the file.

    If you want all the data to be written to the file, create the file BEFORE the loop, write to the file INSIDE the loop,
    and close the file AFTER the loop.


    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    timisaballer23 (April 9th, 2013)

  6. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Please Help. Urgent. For loops and printing to output statement.

    Thanks for the reply. I'm trying to do that however it doesnt really make sense how?

    This is where I'm at.

    <
     { try { myOut = new PrintWriter (new FileOutputStream("out6.dat"));
     
                              myOut.printf("%4d", rnumber);
     
     
                        }
     
               for (int rows = 1; rows <= rownumber; ++rows) 
                    {
                    for (int columns = 1; columns <= cnumber; ++columns) 
                    {
     
                        rnumber = randomGenerator.nextInt(hnumber);
                        rnumber++;
                        System.out.printf("%4d", rnumber);
     
                    }
                    System.out.println();
                }
     
                myOut.close();
                catch (IOException e) {
                            System.out.println("exception is "+e.getMessage());
                        }
                    }
    >
    Last edited by Norm; March 17th, 2013 at 09:19 AM. Reason: removed spaces from code tag

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,307
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Please Help. Urgent. For loops and printing to output statement.

      myOut.printf("%4d", rnumber);
    That code will write one number to a file.

    Where is the code to write the rest of the numbers to the file?
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    timisaballer23 (April 9th, 2013)

Similar Threads

  1. help with printing output in columns
    By jblankinship in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 14th, 2012, 02:03 PM
  2. please help with if-else statement, loops and beginner stuff
    By javabeg123 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 6th, 2011, 08:38 PM
  3. how to make program of Depreciation using if statement and loops
    By Pulsifier in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 17th, 2011, 12:23 AM
  4. Loops only on the first statement
    By Liuric in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 2nd, 2010, 08:36 AM
  5. need help Printing output from a JTextfield
    By juanbond311 in forum Java Theory & Questions
    Replies: 27
    Last Post: June 21st, 2010, 09:26 AM