How to take a specific part from a line and write them to a new text file?
Started by toto_7, Oct 29 2011 06:25 AM
23 replies to this topic
#1
Posted 29 October 2011 - 06:25 AM
Hello,
I'm working on a program that reading information from one file and write to another. Program takes a line per time and store it to a String. Each line contains an ID(9 char) Name(random char) Degree(random char) module(5 char) module(5 char) module(5 char) module(5 char).
My question is how I can take just two of them and write them to a new text file? More specific want to write just the name and the degree. Until now writing the whole line. I can't use supstring() because there are different names and degrees(I mean in length). Please help
Thank you,
toto_7
Each section splinted by single space from previous and next
I'm working on a program that reading information from one file and write to another. Program takes a line per time and store it to a String. Each line contains an ID(9 char) Name(random char) Degree(random char) module(5 char) module(5 char) module(5 char) module(5 char).
My question is how I can take just two of them and write them to a new text file? More specific want to write just the name and the degree. Until now writing the whole line. I can't use supstring() because there are different names and degrees(I mean in length). Please help
Thank you,
toto_7
Each section splinted by single space from previous and next
"Programming is like sex. One mistake and you have to support it for the rest of your life."
-Michael Sinz|
|
|
#2
Posted 29 October 2011 - 07:21 AM
Since the length of each thing is static, just use String.subString(start, end); start index = inclusive, end index exclusive.
#3
Posted 29 October 2011 - 07:26 AM
Found another solution with !Character.isDigit(char); but please explain me these inclusive and exclusive because didn't understand
"Programming is like sex. One mistake and you have to support it for the rest of your life."
-Michael Sinz
#4
Posted 29 October 2011 - 09:40 AM
String str = "0123456789"; System.out.println( str.subString(0,4) ); //prints 0123.So the 2nd parameter (4) is exclusive, it won't take the character of that index in the result. To get the whole String back you need to pass "10"
str.subString(0, 10); Even though index 10 doesn't exist.
What seems easiest for you, if this is your String: "123456789NDmodu1modu2modu3modu4"
Then subString(0,9) == "123456789",
subString(9,10) == "N";
subString(10,11)=="D";
etc.
#5
Posted 29 October 2011 - 10:35 AM
Yes but the names have not all the same length, degrees either
ex.
ex.
123456789 Carson MSE 12345 23456 15651 21701 890123456 Crockett HCI 21701 23456 34567 13244
"Programming is like sex. One mistake and you have to support it for the rest of your life."
-Michael Sinz
#6
Posted 29 October 2011 - 10:58 AM
But it is split by spaces in the String?
#7
Posted 29 October 2011 - 11:01 AM
Yes, and using isDigit() I can do what I want but spaces from each section still remain and I don't know if this will cause me later (visually not but in code maybe) If check for spaces also then name and degree becoming one because remove their space as well.
"Programming is like sex. One mistake and you have to support it for the rest of your life."
-Michael Sinz
#8
Posted 29 October 2011 - 11:18 AM
The problem is easier to solve if your data is separated with spaces.
Using the split method from the String class should help you.
Using the split method from the String class should help you.
#9
Posted 29 October 2011 - 11:27 AM
And use as delimiter just
???
delimiter = " ";
???
"Programming is like sex. One mistake and you have to support it for the rest of your life."
-Michael Sinz
#10
Posted 29 October 2011 - 11:33 AM
toto_7 said:
And use as delimiter just
???
delimiter = " ";
???
#11
Posted 29 October 2011 - 12:46 PM
About the substring () method, isn't the second parameter the length? As in this?:
As for the split () method, I use it in JavaScript almost all the time:
substring ("abcdefghijklmnopqrstuvwxyz", 7, 3);
// Returns "hij"
As for the split () method, I use it in JavaScript almost all the time:
var s= "one and two and three and four";
var a= s.split (" and ");
// a is now ["one", "two", "three", "four"]
#12
Posted 29 October 2011 - 01:02 PM
In javascript maybe. Not in Java.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









