Results 1 to 8 of 8
  1. #1
    TampaTechGuy is offline Member
    Join Date
    Jan 2008
    Posts
    8
    Rep Power
    0

    Default Displaying characters in many ways.

    I am having trouble getting this going....

    I am trying to:

    1. System.out every other letter on the first line of my text file.
    2. System.out (print out) every other line
    3. and finally just characters at positions 26-53 of my last line.

    Just an exercise I am working on to learn more about I/O files.

    Here is my text file:

    This is a basic test file to test out how
    a file can be read into the super duper
    application and how we can extract certain
    characters at several different positions
    to do whatever we need to with them.
    This java program should be able to open a file
    and print out every other letter on the first line;
    and then print out every other line, and then finally
    print characters 26-53 of this last line. I hope it has enough characters.

    import java.io.*;
    import java.util.*;

    public class demo
    {


    public static void main(String args[])
    {
    String data = null;
    int recCount = 0;

    try
    {
    FileReader fr = new FileReader("C:\\Development\\demo.txt");
    BufferedReader br = new BufferedReader(fr);
    data = new String();

    while ((data = br.readLine()) != null)
    {
    recCount++;
    System.out.println(data);
    }

    }

    catch (IOException e)
    {
    System.out.println("IOException error!");
    e.printStackTrace();
    }

    }

    }

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    You might want to use charAt of the String class for numbers 1 and 3, and simple logic for number 2(reading every other line).

  3. #3
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Seems like homework for me but dont want to discourage you.

    Java Code:
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class demo {
    
    	public static void main(String args[]) {
    		String data = null;
    		int recCount = 0;
    
    		try {
    			FileReader fr = new FileReader("C:\\Development\\demo.txt");
    			BufferedReader br = new BufferedReader(fr);
    			data = new String();
    
    			// For the first line thing
    			data = br.readLine();
    
    			if (data != null) {
    				char[] allCharacters = data.toCharArray();
    				for (int i = 0; i < allCharacters.length; i++) {
    					System.out.println(allCharacters[i]);
    				}
    			}
    			
    			
    			// For other lines 
    			while ((data = br.readLine()) != null) {
    				recCount++; // What is this for ??
    				System.out.println(data);
    			}
    
    			// This should help you with substring as well
    			System.out.println(data.substring(26, 53));
    
    		}
    
    		catch (IOException e) {
    			System.out.println("IOException error!");
    			e.printStackTrace();
    		}
    
    	}
    
    }
    Hope it helps
    dont worry newbie, we got you covered.

  4. #4
    TampaTechGuy is offline Member
    Join Date
    Jan 2008
    Posts
    8
    Rep Power
    0

    Default

    Like so...? sorry in advance about the formatting

    public class demo
    {

    /**
    * @param args
    */

    ;
    public static void main(String args[])
    {
    String data = null;


    try
    {
    FileReader fr = new FileReader("C:\\Development\\demo.txt");
    BufferedReader br = new BufferedReader(fr);
    data = new String();

    while ((data = br.readLine()) != null)
    {
    System.out.println(data);
    }

    int len = data.length();

    for (int j = 0; j < len; j = j + 2)
    {
    data.charAt(j);
    System.out.println(j);
    }

    for (int j = 0; j < len; j = j + 2)
    {
    data.charAt(26);
    System.out.println(j);
    }



    }

    catch (IOException e)
    {

    System.out.println("IOException error!");
    e.printStackTrace();
    }


    }

    }

  5. #5
    TampaTechGuy is offline Member
    Join Date
    Jan 2008
    Posts
    8
    Rep Power
    0

    Default

    kinda is like homework --- except no teacher to turn it in to!! ;-)

  6. #6
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    Is there something wrong with both of your codes or am I doing something wrong? They compile, but when ran, end up outputing the text file verbatim and then crashing with a NullPointerException.

    Tampa, try this one, albeit a bit naive, and I've left out the substring for you to figure out.
    Java Code:
     import java.io.*;
    import java.util.*;
    
    public class demo { 
      public static void main(String args[]) {
        String data = null;
        int recCount = 0;
    
        try {
          FileReader fr = new FileReader("C:\\Development\\demo.txt");
          BufferedReader br = new BufferedReader(fr);
          data = new String();
          
          while ((data = br.readLine()) != null) {
            int length = data.length();
            for (int i = 0; i < length; i++) {
              System.out.print(data.charAt(i) + " ");
              i++;
            }
            System.out.println("");
            data = br.readLine(); // skips a line, which is 
                                  //  naive, seek something better, but
                                  //  you get the idea        
            recCount++;
          }
    
        } catch (IOException e) { 
          System.out.println("IOException error!");
          e.printStackTrace();
        }
      }
    }

  7. #7
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Java Code:
    for (int j = 25 ; j < 56 ; j = j + 2) // +2 thing .. ?? 
    {
           char c = data.charAt(j);
           System.out.println(c);
    }
    You know where i made the changes ..
    dont worry newbie, we got you covered.

  8. #8
    TampaTechGuy is offline Member
    Join Date
    Jan 2008
    Posts
    8
    Rep Power
    0

    Default

    Ok I will give that a shot Captain. Thanks for the help. First time I have ever used a forum... i usually just google stuff and put it all together.

Similar Threads

  1. characters + strings
    By Gilgamesh in forum New To Java
    Replies: 3
    Last Post: 03-02-2008, 10:10 PM
  2. Displaying COPYRIGHT and REGISTERED characters
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-01-2008, 10:52 PM
  3. Removing characters
    By kDude in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 03:38 AM
  4. special characters
    By ravian in forum New To Java
    Replies: 2
    Last Post: 11-16-2007, 02:28 PM
  5. Getting all characters in a String
    By Alayna in forum New To Java
    Replies: 2
    Last Post: 05-20-2007, 12:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •