Jump to content

How to take a specific part from a line and write them to a new text file?

- - - - -

  • Please log in to reply
23 replies to this topic

#1
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
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

"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Since the length of each thing is static, just use String.subString(start, end); start index = inclusive, end index exclusive.

#3
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
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
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
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
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Yes but the names have not all the same length, degrees either

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
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
But it is split by spaces in the String?

#7
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
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
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
The problem is easier to solve if your data is separated with spaces.
Using the split method from the String class should help you.

#9
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
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
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

toto_7 said:

And use as delimiter just
delimiter = " ";


???
Try it.

#11
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
About the substring () method, isn't the second parameter the length? As in this?:
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
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
In javascript maybe. Not in Java.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users