Reading from a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Fazana
    New Member
    • Oct 2006
    • 8

    #1

    Reading from a file

    I need help for reading a text file into in my java program.

    The file contains this information:

    ID# Student’s Answers
    ---------------------------
    236499 TFTFTFTFFFFTFTF FFTF
    643828 TFTFTTTTTF FTFTFTTF
    917057 FTF FTTTFFFTF FTFTFT
    656565 FTF FTFTFTF TTFT TTT
    183742 FFTF FFT TFFFTFTFTFF

    I have written the code and it compiles well. But when i execute the program it gives me exception errors.

    The program reads only upto this place and from here it shows exception errors. My program is not able to read in space that is present in the text file.

    ID# Student’s Answers
    ---------------------------
    236499 TFTFTFTFFFFTFTF FFTF
    643828 TFTFTTTTTF

    Help is needed........p liz

    The code i have written for reading this file is:

    Code:
    import java.util.*;
    import java.io.*;
    
    public class read
    {
         public static void main(String[] args) throws IOException
         {
        Scanner inFile = new Scanner(new FileReader("Z:\\StudentResponses.txt"));
    		
    		String student_responses;
    		String header1, header2;
    		int id;
    		
    		System.out.println("ID#\tStudent's Answers");
    		System.out.println("----------------------------");
    		
    		header1 = inFile.nextLine();
    		header2 = inFile.nextLine();
    		
    		while (inFile.hasNext())
    		{ 		
    			id = inFile.nextInt();
    			student_responses = inFile.next();
    			
    			System.out.println(id + "\t" + student_responses);
    		}				
    	
    		inFile.close();
    		
    		System.out.println();
    	
    	}
  • satan
    New Member
    • Oct 2006
    • 28

    #2
    u have to create the files first:
    student ID, answer key, etc etc

    Comment

    • satan
      New Member
      • Oct 2006
      • 28

      #3
      try this code

      while (inFile.hasNext ())
      {
      inputLine = inFile.nextLine ();
      blankPosition = inputLine.index Of(' ');
      ID = inputLine.subst ring(0, blankPosition);
      answerString =
      inputLine.subst ring(blankPosit ion + 1, inputLine.lengt h());
      score = 0;

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Fazana
        I need help for reading a text file into in my java program.

        The file contains this information:

        ID# Student’s Answers
        ---------------------------
        236499 TFTFTFTFFFFTFTF FFTF
        643828 TFTFTTTTTF FTFTFTTF
        917057 FTF FTTTFFFTF FTFTFT
        656565 FTF FTFTFTF TTFT TTT
        183742 FFTF FFT TFFFTFTFTFF

        I have written the code and it compiles well. But when i execute the program it gives me exception errors.

        The program reads only upto this place and from here it shows exception errors. My program is not able to read in space that is present in the text file.

        ID# Student’s Answers
        ---------------------------
        236499 TFTFTFTFFFFTFTF FFTF
        643828 TFTFTTTTTF

        Help is needed........p liz

        The code i have written for reading this file is:

        Code:
        import java.util.*;
        import java.io.*;
        
        public class read
        {
             public static void main(String[] args) throws IOException
             {
            Scanner inFile = new Scanner(new FileReader("Z:\\StudentResponses.txt"));
        		
        		String student_responses;
        		String header1, header2;
        		int id;
        		
        		System.out.println("ID#\tStudent's Answers");
        		System.out.println("----------------------------");
        		
        		header1 = inFile.nextLine();
        		header2 = inFile.nextLine();
        		
        		while (inFile.hasNext())
        		{ 		
        			id = inFile.nextInt();
        			student_responses = inFile.next();
        			
        			System.out.println(id + "\t" + student_responses);
        		}				
        	
        		inFile.close();
        		
        		System.out.println();
        	
        	}
        Yeah the integers and strings are getting mixed up.
        This should work though:

        Code:
        import java.util.*;
        import java.io.*;
        
        public class Read {
               public static void main(String[] args) throws IOException {
        	Scanner inFile = new Scanner(new FileReader(""Z:\\StudentResponses.txt""));
        	String header1, header2;
        	int id;
        	System.out.println("ID#\tStudent's Answers");
        	System.out.println("----------------------------");
        	header1 = inFile.nextLine();
        	header2 = inFile.nextLine();
        	while (inFile.hasNext()) {
           	       String current = inFile.nextLine();
        	       String[] data = current.split(" ");
        	       String student_responses = "";
        	       id = Integer.parseInt(data[0]);
        	       for(int i = 0; i < data.length; i++) {
        		student_responses += data[i];
        	       }
        	      System.out.println(id + "\t" + student_responses);
        	}
        	inFile.close();
        	System.out.println();
        	}
        }

        Comment

        • Fazana
          New Member
          • Oct 2006
          • 8

          #5
          Thanks very much for that code. Now my program is reading all the information from that text file. But still, i have got a small problem with that piece of code.

          As you can see from the text file that there is a space between the id# and student's answers. In the output, everything is clustered together with no space. Infact, the output should have first the id # and then a space which separates it from the students answers. In the students responses, there should also be spaces in between like this: TFTFTTTTTF FTFTFTTF
          There is a space between F F like this: FspaceF. This space does not appear on the screen.
          I tried it very hard to work it out but it does not work.

          Hope to get an answer for this soon.

          Comment

          Working...