Reading info from .txt files in JAVA

Storing info from each line as a String

Page 1 of 1

10 Replies - 1848 Views - Last Post: 01 December 2009 - 04:09 PM Rate Topic: -----

#1 torpatt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-October 09

Reading info from .txt files in JAVA

Posted 29 November 2009 - 07:30 PM

I am currently working on a program for my Computer Science class where we need to create a Non Deterministic Finite Acceptor and have in process info that was stored on a .txt file that we also create.

In the .txt file we are to use # symbols to show a comment lines. The program needs to know to ignore those lines and search for the important ones. The NFA will be processed based on that information.

I believe I can handle programming the NFA but I need help storing the info from the txt file a String variable. I'm thinking I need ome sort of ArrayList but we haven't covered their use so I'm a little shaking on setting it up.

This is what I've come up with so far. I have extra comments because I was trying a few different things:

import javax.swing.JOptionPane;
import java.util.Scanner;
import java.io.*;
public class  COMP385NFA{//sets the file name to COMP 385NFA

	public static void main(String args[]){//starts the main method for the program
	/*  try{ // Open the input stream by accessing the file 
			FileInputStream fstream = new FileInputStream("COMP385program.txt"); // Get the object of DataInputStream
		 	DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
	
	 //Read File Line By Line
  
	while ((strLine = br.readLine()) != null)   {
		  System.out.println(strLine);
	}
	
	in.close();//Close the input stream
	}catch (Exception e){//Catch exception if any
	  System.err.println("Error: " + e.getMessage());
	}
	
	for(int i=0; i<test.length(); i++){
		if ((test.charAt(test.length()-1)).equalsIgnoreCase (words.charAt(words.length-1))&&(test.charAt(test.length-2)).equalsIgnoreCase(words.charAt(words.length-2))){
		System.out.println("Accepted");//display message to user
		}
		
		else{
		System.out.println("Not Accepted");//display message to user
		}
	}
	*/

	File file= new File("COMP385program.txt");//pulls file Numbers.txt
	Scanner inputFile= new Scanner(file);//tells computer to import info from file

	java.util.ArrayList user = new java.util.ArrayList();
	


	
	while(inputFile.hasNext()){//reads input from file
 		for(int i=0; i<user.size();i++){//sets for loop to catch the numbers
			user.add(inputFile.next());//set number as the next double
		}
	}
	
	inputFile.close();//closes the file

	}
}



Any help or direction would be greatly appreciated.

torpatt

This post has been edited by torpatt: 29 November 2009 - 07:34 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Reading info from .txt files in JAVA

#2 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12134
  • View blog
  • Posts: 45,114
  • Joined: 27-December 08

Re: Reading info from .txt files in JAVA

Posted 29 November 2009 - 07:33 PM

You might want to check out the String class and the Scanner class:
http://java.sun.com/...ang/String.html
http://java.sun.com/...il/Scanner.html

If you need more help, you will have to show us that you have made an effort.

[rules][/rules]

This post has been edited by macosxnerd101: 29 November 2009 - 07:33 PM

Was This Post Helpful? 0
  • +
  • -

#3 torpatt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-October 09

Re: Reading info from .txt files in JAVA

Posted 29 November 2009 - 08:24 PM

I have posted the code I have so far but I am still unclear as to how to store each line of the inputted .txt file into a String variable. Thank you for the link to the information on Strings. Though it was informative, I was unable to find any information on reading in from an outside file.

I am by no means looking for free code just a little direction so I can complete my assignment and learn for the future.

Any help would be greatly appreciated.


torpatt
Was This Post Helpful? 0
  • +
  • -

#4 torpatt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-October 09

Re: Reading info from .txt files in JAVA

Post icon  Posted 30 November 2009 - 09:45 AM

[/color]I am currently working on a program for my Computer Science class where we need to create a Non Deterministic Finite Acceptor and have in process info that was stored on a .txt file that we also create.

In the .txt file we are to use # symbols to show a comment lines. The program needs to know to ignore those lines and search for the important ones. The NFA will be processed based on that information.

I believe I can handle programming the NFA but I need help storing the info from the txt file a String variable. I'm thinking I need ome sort of ArrayList but we haven't covered their use so I'm a little shaking on setting it up.

This is what I've come up with so far. I have extra comments because I was trying a few different things:



import javax.swing.JOptionPane;
import java.util.Scanner;
import java.io.*;
public class  COMP385NFA{//sets the file name to COMP 385NFA

	public static void main(String args[]){//starts the main method for the program
	/*  try{ // Open the input stream by accessing the file 
			FileInputStream fstream = new FileInputStream("COMP385program.txt"); // Get the object of DataInputStream
			 DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
	
	 //Read File Line By Line
  
	while ((strLine = br.readLine()) != null)   {
		  System.out.println(strLine);
	}
	
	in.close();//Close the input stream
	}catch (Exception e){//Catch exception if any
	  System.err.println("Error: " + e.getMessage());
	}
	
	for(int i=0; i<test.length(); i++){
		if ((test.charAt(test.length()-1)).equalsIgnoreCase (words.charAt(words.length-1))&&(test.charAt(test.length-2)).equalsIgnoreCase(words.charAt(words.length-2))){
		System.out.println("Accepted");//display message to user
		}
		
		else{
		System.out.println("Not Accepted");//display message to user
		}
	}
	*/

	File file= new File("COMP385program.txt");//pulls file Numbers.txt
	Scanner inputFile= new Scanner(file);//tells computer to import info from file

	java.util.ArrayList user = new java.util.ArrayList();
	


	
	while(inputFile.hasNext()){//reads input from file
		 for(int i=0; i<user.size();i++){//sets for loop to catch the numbers
			user.add(inputFile.next());//set number as the next double
		}
	}
	
	inputFile.close();//closes the file

	}
}



Any help or direction would be greatly appreciated.

Was This Post Helpful? 0
  • +
  • -

#5 TriggaMike  Icon User is offline

  • Using up all your 1's and 0's
  • member icon

Reputation: 85
  • View blog
  • Posts: 1,103
  • Joined: 26-September 08

Re: Reading info from .txt files in JAVA

Posted 30 November 2009 - 09:55 AM

Just use

import java.util.*;

and then you can just declare and use array lists using the command

ArrayList user = new ArrayList();

Other than that, outputting to a file you will still use a File object, but you will pass it to a PrintWriter like so

File outputFile = new File("FileName.txt");
PrintWriter writer = new PrintWriter(outputFile);

writer.println("This is a line of text");

writer.print("Do not use escape characters in here though \n because that will NOT print a new line")

writer.println();


and you can just use ArrayList.add(objectHere); and that is essentially unbounded, but if you get a heap overflow then you're loading too much data.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12134
  • View blog
  • Posts: 45,114
  • Joined: 27-December 08

Re: Reading info from .txt files in JAVA

Posted 30 November 2009 - 10:03 AM

Both your commented and your non-commented attempts have some merits. In your top approach, I like how you use a temp variable to store in each line; and in your bottom approach, I like the use of an ArrayList to store each line. Now let's work on combining them a little bit. I'll start by throwing some logic out there:
0. Begin
1. scan <-- new Scanner for the File
2. temp <-- new String with initial value of null
3. list <-- ArrayList of Strings to hold the valid lines
4. While scan has another line
5.	  temp <-- the next line
6.	  if the first char in temp is not a '#'
7.		  then add temp to the list
		 end if
   end while
end algorithm



In this way, you are ignoring lines beginning with a # symbol. Consider this- how would you deal with a comment in-line. For example: real text #this is a comment. How would you only get the real text but ignore the comment here?
Was This Post Helpful? 1
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8376
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Reading info from .txt files in JAVA

Posted 30 November 2009 - 06:52 PM

Duplicated topics merged
Please avoid double postings
Was This Post Helpful? 0
  • +
  • -

#8 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 565
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Reading info from .txt files in JAVA

Posted 30 November 2009 - 07:02 PM

Quote

0. Begin
1. scan <-- new Scanner for the File
2. temp <-- new String with initial value of null
3. list <-- ArrayList of Strings to hold the valid lines
4. While scan has another line
5. temp <-- the next line
6. if the first char in temp is not a '#'
7. then add temp to the list
end if
end while
end algorithm


Now this is what I call a perfect reply. I think we all should start providing psuedocode answers :D
Was This Post Helpful? 0
  • +
  • -

#9 torpatt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-October 09

Re: Reading info from .txt files in JAVA

Posted 30 November 2009 - 09:55 PM

Thank you all for your help. It is very much appreciated. You have given me a lot to go on. Seems like I'm going to pull an all nighter but I think I can hopefully get the project done in time. I think that I have most of what I need in place. This is my updated code:
//Tori R. Patterson
//
//

   import javax.swing.JOptionPane;
   import java.util.*;
   import java.io.*;
	public class  COMP385NFA{//sets the file name to COMP 385NFA
   
	   public static void main(String args[])	throws ClassNotFoundException,IOException{//starts the main method for the program
	  //		 try{ // Open the input stream by accessing the file 
		DataInputStream input= new DataInputStream(new BufferedInputStream(new FileInputStream("COMP385program.txt")));//imports info from text file and buffers it to maximize speed of input
	  
		ArrayList user = new ArrayList();//creates an array list call user	
	  
		int Line;
			String check;
		
		  while ((Line = input.read()) != -1){//continues loop while there are still lines on the file document
			for(int i=0; i<user.size(); i++){//starts for loop to read in strings into array
			   user.add(input.readLine());	
			  System.out.println(user.get(i));
			 }
		 }
	  
	  input.close();
	  //  }catch (Exception e){//Catch exception if any
	  //  System.err.println("Error: " + e.getMessage());
	  //}
	  
	  /*for(int i=0; i<test.length(); i++){
	  if ((test.charAt(test.length()-1)).equalsIgnoreCase (words.charAt(words.length-1))&&(test.charAt(test.length-2)).equalsIgnoreCase(words.charAt(words.length-2))){
	  System.out.println("Accepted");//display message to user
	  }
	  
	  else{
	  System.out.println("Not Accepted");//display message to user
	  }
	  }*/
	
	  
	  }
   }




and it is giving me the following error:

 ----jGRASP exec: javac -g G:\COMP 385\Program\COMP385NFA.java

Note: G:\COMP 385\Program\COMP385NFA.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: G:\COMP 385\Program\COMP385NFA.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

 ----jGRASP: operation complete.


From what I understand, I need to compile using javac -Xlint:deprecation COMP385NFA.java to find out what function I need to replace but I'm not sure how to compile using that line. Is it supposed to be placed somewhere in the code or is it I should be using in JGrasp to compile under?

Any assistance again would be greatly appreciated

This post has been edited by torpatt: 01 December 2009 - 01:03 AM

Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3420
  • View blog
  • Posts: 15,455
  • Joined: 20-September 08

Re: Reading info from .txt files in JAVA

Posted 01 December 2009 - 10:07 AM

See the following

http://technojeeves..../74-string-list
Was This Post Helpful? 0
  • +
  • -

#11 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: Reading info from .txt files in JAVA

Posted 01 December 2009 - 04:09 PM

View Posttorpatt, on 30 Nov, 2009 - 10:55 PM, said:

 ----jGRASP exec: javac -g G:\COMP 385\Program\COMP385NFA.java

Note: G:\COMP 385\Program\COMP385NFA.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: G:\COMP 385\Program\COMP385NFA.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

 ----jGRASP: operation complete.


From what I understand, I need to compile using javac -Xlint:deprecation COMP385NFA.java to find out what function I need to replace but I'm not sure how to compile using that line. Is it supposed to be placed somewhere in the code or is it I should be using in JGrasp to compile under?

Any assistance again would be greatly appreciated[/color][/font]


Your compiler is complaining because you are using a deprecated method. user.add(input.readLine()); is the line causing all of the problems. See what Sun says about it here: http://java.sun.com/...#readLine%28%29

You also wrote this:
DataInputStream input= new DataInputStream(new BufferedInputStream(new FileInputStream("COMP385program.txt")));



Which does not accomplish much. According to the link I gave you above, this should be the way:

Quote

BufferedReader d = new BufferedReader(new InputStreamReader(in));


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1