1 Replies - 142 Views - Last Post: 07 December 2012 - 12:25 AM Rate Topic: -----

#1 Walker1101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-December 12

HELP, opening a file then read and display the data!

Posted 06 December 2012 - 11:37 PM

I am stuck on my code to open the string file and read the data that is stored in the file. What am I missing for my code. Please help me!

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

public class Stars
{
	public static void main(String[] args) throws IOException
	{
		String filename;			//File name
		String filename2;			//To open file name
		
		//Create a Scanner object for keyboard input.
		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();
    			
		//Consume the remaining newline character.
		keyboard.nextLine();
		
		//Get the filename.
		System.out.print("Enter the filename: ");
		filename = keyboard.nextLine();
		
		File file = new File(filename);
		Scanner inputFile = new Scanner(filename);
		
		//Open the file.
		PrintWriter outputFile = new PrintWriter(filename);
		
		for (int i = 1; i <= number; i++)
    	{
    		for (int j = 1; j <= number; j++)
    		{
    		    System.out.print("*");
    			outputFile.print("*");
    		}
				System.out.println();
    			outputFile.println();
    	}   
		
		System.out.print("Please enter your file name to open file: ");
		filename2 = keyboard.nextLine();
		
		File file2 = new File(filename2);
		Scanner inputFile2 = new Scanner(file2);
		
		while (inputFile.hasNext())
		{
			System.out.println("*");
			inputFile.out.println("*");
		}
		
		inputFile.close();
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: HELP, opening a file then read and display the data!

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9215
  • View blog
  • Posts: 34,612
  • Joined: 12-June 08

Re: HELP, opening a file then read and display the data!

Posted 07 December 2012 - 12:25 AM

Please do not open duplicate topics. Closing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1