9 Replies - 7078 Views - Last Post: 09 November 2009 - 01:11 PM Rate Topic: -----

#1 SindaKay   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-November 09

Read lines from different files and output the line number and the lin

Posted 09 November 2009 - 12:09 PM

Write a program that asks the user for teh name of a text file. The program should display the contents of the file witheach line preceded witha line number followed by a colon. The line numbering should start at 1. Insure, by modifiying your text file, and running the program for each modification, that the program works correctly for files with one, two and five lines of text. Blank lines in the file should be numbered as well. Also insure your program throw an exception should the file name entered not exist.

Also, I can not use try or catch statements. I must use if, if-else-if, for, while, or do loops. Must be a very very basic code.

This is what I have been able to get so far. It runs the program but the output is always The file One_line.txt is not found, regardless if I entered Two_lines.txt or Five_lines.txt

import java.util.Scanner;

public class Ward_Tassinda_Midterm_04 //MUST match the file name!
{
   public static void main (String[] args) throws IOException
   {
		 		
 		//Create Scanner object for keyboard input.
		Scanner keyboard = new Scanner (System.in);
		
		//Get File name.
		System.out.println("Enter the file name: ");
		String filename = keyboard.nextLine();
		
		//Make sure the file exists.
		File file = new File ("One_line.txt");
		if (!file.exists())
		{
			System.out.println("The file One_line.txt is not found.");
			System.exit(0);
		}
	
		File file2 = new File ("Two_lines.txt");
		if (!file2.exists())
		{
			System.out.println("The file Two_lines.txt is not found.");
			System.exit(0);
		}
		
		File file3 = new File ("Five_lines.txt");
		if (!file3.exists())
		{
			System.out.println("The file Five_lines.txt is not found.");
			System.exit(0);
		}

		//Open file for reading
		Scanner inputFile = new Scanner(file);
					
		//Read lines from file
		while (inputFile.hasNext())
		{
			String str = inputFile.nextLine();
			System.out.println("1: " + str);
		}
		
		
	}//end main method
}//end class



Is This A Good Question/Topic? 0
  • +

Replies To: Read lines from different files and output the line number and the lin

#2 NeoTifa   User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4211
  • View blog
  • Posts: 18,472
  • Joined: 24-September 08

Re: Read lines from different files and output the line number and the lin

Posted 09 November 2009 - 12:15 PM

Well, you could just have

int lineNum = 1;
while (inputFile.hasNext()) {
System.out.println(lineNum + ": " + inputFile.nextLine());
lineNum++;
}



to display it.
Was This Post Helpful? 0
  • +
  • -

#3 SindaKay   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-November 09

Re: Read lines from different files and output the line number and the lin

Posted 09 November 2009 - 12:23 PM

View PostNeoTifa, on 9 Nov, 2009 - 11:15 AM, said:

Well, you could just have

int lineNum = 1;
while (inputFile.hasNext()) {
System.out.println(lineNum + ": " + inputFile.nextLine());
lineNum++;
}



to display it.


Thank you, I tried that but I am still getting the same error message: The file One_line.txt is not found. I tested it by trying Two_lines.txt, but it still gave me the out put of The file One_line.txt is not found.
Was This Post Helpful? 0
  • +
  • -

#4 NeoTifa   User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4211
  • View blog
  • Posts: 18,472
  • Joined: 24-September 08

Re: Read lines from different files and output the line number and the lin

Posted 09 November 2009 - 12:32 PM

Quote

//Get File name.
System.out.println("Enter the file name: ");
String filename = keyboard.nextLine();

What is the point of this? You're collecting this data then going and getting files that you've determined already. It's pointless.
Was This Post Helpful? 0
  • +
  • -

#5 SindaKay   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-November 09

Re: Read lines from different files and output the line number and the lin

Posted 09 November 2009 - 12:39 PM

View PostNeoTifa, on 9 Nov, 2009 - 11:32 AM, said:

Quote

//Get File name.
System.out.println("Enter the file name: ");
String filename = keyboard.nextLine();

What is the point of this? You're collecting this data then going and getting files that you've determined already. It's pointless.


That is how we are taught to do it. You first ask the user for the file name and then the program retreaves the file and reads the file. The output should be what was read in the file that was input by the user.
Was This Post Helpful? 0
  • +
  • -

#6 NeoTifa   User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4211
  • View blog
  • Posts: 18,472
  • Joined: 24-September 08

Re: Read lines from different files and output the line number and the lin

Posted 09 November 2009 - 12:45 PM

But that's not what's happening. You are creating 3 different file instances of 3 different files predetermined by you. In no way, shape, or form are you attempting to open a file prompted by the user.

Scanner input = new Scanner(System.in);
System.out.println("Enter file name/path to be opened including extention: ");
String path = input.next();

//error handling here

File file = new File(path);
if (!file.exists())
        {
            System.out.println("The file " + path +" is not found.");
            System.exit(0);
        }

//etc.


This post has been edited by NeoTifa: 09 November 2009 - 12:49 PM

Was This Post Helpful? 0
  • +
  • -

#7 SindaKay   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-November 09

Re: Read lines from different files and output the line number and the lin

Posted 09 November 2009 - 01:00 PM

View PostNeoTifa, on 9 Nov, 2009 - 11:45 AM, said:

But that's not what's happening. You are creating 3 different file instances of 3 different files predetermined by you. In no way, shape, or form are you attempting to open a file prompted by the user.

Scanner input = new Scanner(System.in);
System.out.println("Enter file name/path to be opened including extention: ");
String path = input.next();

//error handling here

File file = new File(path);
if (!file.exists())
        {
            System.out.println("The file " + path +" is not found.");
            System.exit(0);
        }

//etc.



Ok, I tried that and I am still getting the error message The file "filename" is not found. And I tried it with all the files, but none are working.

This is the way the code looks now:

[code]
import java.io.*;
import java.util.Scanner;

public class Ward_Tassinda_Midterm_04 //MUST match the file name!
{
public static void main (String[] args) throws IOException
{
//Create Scanner object for keyboard input.
Scanner keyboard = new Scanner (System.in);

//Get File name.
Scanner input = new Scanner(System.in);
System.out.println("Enter the file name to include the extention: ");
String filename = input.next();

//Make sure the file exists.
File file = new File (filename);
if (!file.exists())
{
System.out.println("The file " + filename + " is not found.");
System.exit(0);
}

//Open file for reading
Scanner inputFile = new Scanner(file);

//Read lines from file
int lineNum =1;
while (inputFile.hasNext())
{
System.out.println(lineNum + ": " + inputFile.nextLine());
lineNum++;
}


}//end main method
}//end class[code]
Was This Post Helpful? 0
  • +
  • -

#8 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Read lines from different files and output the line number and the lin

Posted 09 November 2009 - 01:02 PM

Have you tried double chcking that the file is in the same location as the CLass file is? If they are in different folders you will need to provide a file path and not just file name.
Was This Post Helpful? 0
  • +
  • -

#9 SindaKay   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-November 09

Re: Read lines from different files and output the line number and the lin

Posted 09 November 2009 - 01:07 PM

View PostFuzzyness, on 9 Nov, 2009 - 12:02 PM, said:

Have you tried double chcking that the file is in the same location as the CLass file is? If they are in different folders you will need to provide a file path and not just file name.


OMG you are a genius!!! I totally didn't think of that! And now it works!!! Thanks you so much.
Was This Post Helpful? 0
  • +
  • -

#10 NeoTifa   User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4211
  • View blog
  • Posts: 18,472
  • Joined: 24-September 08

Re: Read lines from different files and output the line number and the lin

Posted 09 November 2009 - 01:11 PM

Hince the //error handling here -__- I better get thanked too.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1