Results 1 to 8 of 8
- 01-02-2008, 08:07 PM #1
Member
- Join Date
- Jan 2008
- Posts
- 8
- Rep Power
- 0
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();
}
}
}
- 01-02-2008, 08:47 PM #2
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).
- 01-02-2008, 08:50 PM #3
Seems like homework for me but dont want to discourage you.
Hope it helpsJava 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(); } } }dont worry newbie, we got you covered.
- 01-02-2008, 09:06 PM #4
Member
- Join Date
- Jan 2008
- Posts
- 8
- Rep Power
- 0
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();
}
}
}
- 01-02-2008, 09:08 PM #5
Member
- Join Date
- Jan 2008
- Posts
- 8
- Rep Power
- 0
kinda is like homework --- except no teacher to turn it in to!! ;-)
- 01-02-2008, 09:12 PM #6
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(); } } }
- 01-02-2008, 09:13 PM #7
You know where i made the changes ..Java Code:for (int j = 25 ; j < 56 ; j = j + 2) // +2 thing .. ?? { char c = data.charAt(j); System.out.println(c); }dont worry newbie, we got you covered.
- 01-02-2008, 09:16 PM #8
Member
- Join Date
- Jan 2008
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
characters + strings
By Gilgamesh in forum New To JavaReplies: 3Last Post: 03-02-2008, 09:10 PM -
Displaying COPYRIGHT and REGISTERED characters
By Java Tip in forum Java TipReplies: 0Last Post: 03-01-2008, 09:52 PM -
Removing characters
By kDude in forum New To JavaReplies: 3Last Post: 12-03-2007, 02:38 AM -
special characters
By ravian in forum New To JavaReplies: 2Last Post: 11-16-2007, 01:28 PM -
Getting all characters in a String
By Alayna in forum New To JavaReplies: 2Last Post: 05-20-2007, 11:49 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks