1

If the substring position (6,7) is a blank space, I want that blank space to be replaced with a hyphen (used while loop because there are many in this file). I tried the below, trying to use concatenation to link together the new line but the blank space still exists.

Right Now: ABCDEF YZ

What I need: ABCDEF-YZ

Here is a block of code:

String line= inputFile.readLine();

   while(line!= null)
   {
       if (line.charAt(6) == ' ')           {
           String outputWithHyphen = line.substring(0,6) + '-' + 
           line.substring(7);
           outputFile.println(outputWithHyphen);
       }
       outputFile.println(line); 
       line= inputFile.readLine();
    } 

Removed if clause, now works, really want the if clause though.

           String outputWithHyphen = lineOfText.substring(0,6) + '-' + 
           lineOfText.substring(7);

           outputFile.println(outputWithHyphen);
           lineOfText = inputFile.readLine();

Thank you for all of your support!

TL;DR: Replace all hyphens with spaces in a certain position in a text file

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
ChosenForWorlds
  • 85
  • 1
  • 2
  • 8
  • 1
    Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Tom Jan 03 '17 at 21:43
  • Why use substring() to check for a single character (it unnecessarily creates a new String)? Why not just use `if (lineOfText.charAt(6) == ' ' )` ? – FredK Jan 03 '17 at 21:44
  • Then you have more issues. Like your strange mix of `line` and `lineOfText` and the fact that you print the original `lineOfText` with the whitespace right after the `if`. – Tom Jan 03 '17 at 21:47
  • @Tom yes, I didn;t notice that. The OP's code doesn't seem to define a variable named "lineOfText", but just "text". My comment still applies - use `if ( line.charAt(5) == ' ' )` – FredK Jan 03 '17 at 21:50
  • Used if ( line.charAt(5) == ' ' ), the new file is identical to the original – ChosenForWorlds Jan 03 '17 at 21:51
  • Why 5 when the space is at position 6? – FredK Jan 03 '17 at 21:52
  • @FredK My comment wasn't about your comment. – Tom Jan 03 '17 at 21:52
  • Yeah, the space is at 6, updated the code. Something interesting just happened in the file, on line 3 (blank space), it got replaced with a hyphen. On line 4, where there was a hyphen, now has a blank space. – ChosenForWorlds Jan 03 '17 at 21:54
  • @Tom Yes, I know - was just trying to clarify my comment to the OP. Also, the OP should note the difference between his comment using `''` and my comment using `' '` (Note the space between the single quotes). – FredK Jan 03 '17 at 21:54
  • The updated code has been replaced in the post, with the suggestions above. The hyphen is still not being added when a blank space is met. – ChosenForWorlds Jan 03 '17 at 21:58
  • Removed if clause, and now it works. But i really want the if clause, any way to implement? Updated code in post header. – ChosenForWorlds Jan 03 '17 at 22:03
  • I know whats the problem – Mohsen_Fatemi Jan 03 '17 at 22:04

2 Answers2

1

try this , this is gonna be work :

String line= inputFile.readLine();

   while(line!= null)
   {
       String outputWithHyphen = line;
       if (line.charAt(6) == ' '){
           outputWithHyphen = line.substring(0,6) + '-' + 
           line.substring(7);
       }
       outputFile.println(outputWithHyphen);
       line= inputFile.readLine();
    } 
Mohsen_Fatemi
  • 2,183
  • 2
  • 16
  • 25
  • I don't know what is your main purpose , tell me what you want so that I can help you ... – Mohsen_Fatemi Jan 03 '17 at 22:07
  • what was the output? – Mohsen_Fatemi Jan 03 '17 at 22:09
  • look , you want to write something in your file , lets name it outputWithHyphen , I first initiated it with value of line , I assume that there is no space in the output , then I checked if there is an space in the line , I replaced it with '-' , then I write it in the file – Mohsen_Fatemi Jan 03 '17 at 22:16
0

If I understand this correctly from the comments above, you want to replace all spaces you have with a dash - not just in one specific location. You can use the replaceAll function to do this operation instead of looping through character by character:

  String myLine = line.replaceAll(" ", "-");
Abdulgood89
  • 359
  • 2
  • 9
  • Yes, this works, sorry I should have added that there are many blank spaces that I actually need. Just the blank spaces at this certain index are the ones that I want to remove. – ChosenForWorlds Jan 03 '17 at 22:06