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();
}
}
1 Replies - 55 Views - Last Post: 07 December 2012 - 12:25 AM
#1
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!
Replies To: HELP, opening a file then read and display the data!
#2
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.
Page 1 of 1
|
|

New Topic/Question
This topic is locked



MultiQuote







|