Pliz help....

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

    #1

    Pliz help....

    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.

    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();
    	} 
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Fazana
    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.

    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();
    	} 
    }

    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 = 1; i < data.length; i++)
    			{
    				student_responses += data[i] + " ";
    			}
    
    			System.out.println(id + "\t" + student_responses);
    		}
    
    		inFile.close();
    
    		System.out.println();
    	}
    }
    Should be alright now

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Didn't have to start a new thread for it though.

      Comment

      Working...