Results 1 to 10 of 10
  1. #1
    r0sedeanne's Avatar
    r0sedeanne is offline Member
    Join Date
    Sep 2011
    Location
    Honolulu, Hawaii
    Posts
    9
    Rep Power
    0

    Default Need help writing star pattern to file

    Instructions:
    1. Program will ask the user for a number input (integer).
    2. Program will then produce the stars ("*") as a square. However while it is printing a line of stars("*"), it will also write each set (line) of stars on a text file vice displaying on screen.
    3. The program will then open the same text file for reading, read a line at a time and display on screen.
    4. The program will keep reading lines until no more data (end of file).


    This is what I've got so far. The only error it's giving me is "<identifier> expected" on line 42, but I'm sure there are plenty of other things going wrong in here :/

    Java Code:
    import java.util.Scanner;
    import java.io.*;
    
    public class Stars
    {
        public static void main(String[] args) throws IOException
        {
        	String filename;
        	String manystars;
    
        	//create keyboard
        	Scanner keyboard = new Scanner(System.in);
    
        	//get number of stars
        	System.out.println("Welcome! This program will take your\n" +
        		               "number input, and generate a square \n" +
        		               "of stars.\n");
        	System.out.print("Please enter a number: ");
        	int number = keyboard.nextInt();
    
        	//consume newline character
        	keyboard.nextLine();
    
        	//get the file name
        	System.out.print("Enter the filename: ");
        	filename = keyboard.nextLine();
    
        	//open new file
        	PrintWriter outFile = new PrintWriter(filename);
    
    		//get data and write to file
        	for (int i=1; i<=number; i++)
        	{
        		for (int j=1; j<=number; j++)
        		outputFile.println("*");
        		outputFile.println(manystars);
    
        	}//end for
    
        	//close file
        	outputFile.close();
        	System.out.println("Data written to the file".);    //"identifier expected" error points to here
    
        	//open file
        	File file = new File(filename);
        	Scanner inputFile = new Scanner(file);
    
        	//read lines
        	while (inputFile.hasNext())
        	{
        		String manystars = inputFile.nextLine();
        		System.out.println(manystars);
        	}
    
        	//close file
        	inputFile.close();
    
        }//end main
    
    }//end of Stars
    Last edited by r0sedeanne; 09-11-2011 at 11:29 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Need help writing star pattern to file

    Are the "s in the correct place? Is there something that should be inside them?

  3. #3
    r0sedeanne's Avatar
    r0sedeanne is offline Member
    Join Date
    Sep 2011
    Location
    Honolulu, Hawaii
    Posts
    9
    Rep Power
    0

    Default Re: Need help writing star pattern to file

    ....Oh man. Thanks *facepalm*

    Now that that's out of the way.. I've got "cannot find symbol variable outputFile" and "manystars is already defined in main(java.lang.String[])" errors yelling at me.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Need help writing star pattern to file

    Please copy and paste here the FULL text of the error messages.

  5. #5
    r0sedeanne's Avatar
    r0sedeanne is offline Member
    Join Date
    Sep 2011
    Location
    Honolulu, Hawaii
    Posts
    9
    Rep Power
    0

    Default Re: Need help writing star pattern to file

    That is the full text.

    "cannot find symbol variable outputFile" on lines 35, 36, 41
    "manystars is already defined in main(java.lang.String[])" on line 51

    But I found out the fix to the first three. I had the variable defined as "outfile" but then called it "outputFile" from then on.

    Someone else pointed out to me that I have the "manystars" string declared twice, and that I should just delete one, but if I delete it from line 51 it tells me "variable manystars might not have been initialized" on line 36 and line 52, and if I delete line 9 instead it says "cannot find symbol variable manystars" on line 36.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Need help writing star pattern to file

    "manystars" string declared twice
    Don't delete the whole line, just change it so it is not a definition. Leave the assignment part that gives the variable a value.
    variable manystars might not have been initialized"
    Give it a null value or an empty String value where you define it.

  7. #7
    r0sedeanne's Avatar
    r0sedeanne is offline Member
    Join Date
    Sep 2011
    Location
    Honolulu, Hawaii
    Posts
    9
    Rep Power
    0

    Default Re: Need help writing star pattern to file

    Alright awesome, no more errors! But when I run the program it gives me a line of stars instead of a box.
    *
    *

    *
    *

    vs
    **
    **


    So, current code.

    Java Code:
    import java.util.Scanner;
    import java.io.*;
    
    public class Stars2
    {
        public static void main(String[] args) throws IOException
        {
        	String filename;
    
    
        	//create keyboard
        	Scanner keyboard = new Scanner(System.in);
    
        	//get number of stars
        	System.out.println("Welcome! This program will take your\n" +
        		               "number input, and generate a square \n" +
        		               "of stars.\n");
        	System.out.print("Please enter a number: ");
        	int number = keyboard.nextInt();
    
        	//consume newline character
        	keyboard.nextLine();
    
        	//get the file name
        	System.out.print("Enter the filename: ");
        	filename = keyboard.nextLine();
    
        	//open new file
        	PrintWriter outputFile = new PrintWriter(filename);
    
    		//get data and write to file
        	for (int i=1; i<=number; i++)
        	{
    		   	for (int j=1; j<=number; j++)
    	    	outputFile.println("*");
    	    	outputFile.println();
    
        	}//end for
    
        	//close file
        	outputFile.close();
        	System.out.println("Data written to the file.");
    
        	//open file
        	File file = new File(filename);
        	Scanner inputFile = new Scanner(file);
    
        	//read lines
        	while (inputFile.hasNext())
        	{
        		String manystars = inputFile.nextLine();
        		System.out.println(manystars);
        	}
    
        	//close file
        	inputFile.close();
    
        }//end main
    
    }//end of Stars

    I don't know if there's something wrong with the loop itself or if something I put in there afterwards is messing it up, but back in the beginning of the semester we had a similar assignment, make a box, but without the text file. Mine looked like this, and worked fine. I thought I could just change the "System.out.print("*");" to "outputFile.println("*");" and it would know to go into the text file instead.

    Java Code:
    import java.util.Scanner;
    
    public class Stars2 {
    
        public static void main(String[] args)
        {
    
        	Scanner keyboard = new Scanner(System.in);
    
        	System.out.println("Welcome! This program will take your\n" +
        		               "number input, and generate a square \n" +
        		               "of stars.\n");
        	System.out.print("Please enter a number: ");
        	int number = keyboard.nextInt();
    
        	for (int i=1; i<=number; i++)
        	{
    			for (int j=1; j<=number; j++)
    			System.out.print("*");
    			System.out.println();
    
        	}
    
        }
    
    }

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Need help writing star pattern to file

    The difference between the two outputs you posted is that the first one has lineend characters that move the next print out to the next line.
    If you want two *s on the same line, do NOT print out a lineend between them.
    The println method adds a lineend to the end of what it prints.

  9. #9
    r0sedeanne's Avatar
    r0sedeanne is offline Member
    Join Date
    Sep 2011
    Location
    Honolulu, Hawaii
    Posts
    9
    Rep Power
    0

    Default Re: Need help writing star pattern to file

    OH! I got it! Haha.

    Success! :D

    Thank you so much for your help!!

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

Similar Threads

  1. Star pattern help?
    By GeeKunMow in forum New To Java
    Replies: 13
    Last Post: 07-21-2011, 05:59 AM
  2. How to mirror Star Pattern
    By andypatel00 in forum New To Java
    Replies: 10
    Last Post: 04-04-2011, 03:50 AM
  3. Star triangle pattern
    By crazy4fun in forum New To Java
    Replies: 7
    Last Post: 03-06-2011, 01:03 PM
  4. intricate star pattern
    By aamster in forum New To Java
    Replies: 7
    Last Post: 10-11-2009, 06:50 AM
  5. Recursive Star Pattern HELP PLEASE!!
    By ITnewbie in forum New To Java
    Replies: 1
    Last Post: 09-25-2009, 09:55 PM

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
  •