Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-02-2008, 09:07 PM
Member
 
Join Date: Jan 2008
Posts: 6
TampaTechGuy is on a distinguished road
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();
}

}

}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-02-2008, 09:47 PM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 840
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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).
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-02-2008, 09:50 PM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 263
roots is on a distinguished road
Seems like homework for me but dont want to discourage you.

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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-02-2008, 10:06 PM
Member
 
Join Date: Jan 2008
Posts: 6
TampaTechGuy is on a distinguished road
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();
}


}

}
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-02-2008, 10:08 PM
Member
 
Join Date: Jan 2008
Posts: 6
TampaTechGuy is on a distinguished road
kinda is like homework --- except no teacher to turn it in to!! ;-)
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-02-2008, 10:12 PM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 840
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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.
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(); } } }
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-02-2008, 10:13 PM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 263
roots is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 01-02-2008, 10:16 PM
Member
 
Join Date: Jan 2008
Posts: 6
TampaTechGuy is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
characters + strings Gilgamesh New To Java 3 03-02-2008 10:10 PM
Displaying COPYRIGHT and REGISTERED characters Java Tip Java Tips 0 03-01-2008 10:52 PM
Removing characters kDude New To Java 3 12-03-2007 03:38 AM
special characters ravian New To Java 2 11-16-2007 02:28 PM
Getting all characters in a String Alayna New To Java 2 05-20-2007 12:49 PM


All times are GMT +3. The time now is 10:36 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org