2

I would like to convert a body of text which is in a form similar to this:

text here
text there
text everywhere

into a single string which would look like the following:

textheretexttheretexteverywhere

EDIT: The text on multiple lines is to be copied from one file and pasted into the input of the program, however it isn't necessarily a .txt file.

Here's what I have so far:

public static void converter(String inputString){

    String refinedString = inputString.replaceAll("\\s+","").replaceAll("\\\\n+", "");
    System.out.println();
    System.out.println("Refined string: " + refinedString);
}

Here is my main function where I am calling my converter method:

public static void main(String [] args){
        System.out.println("Enter string: ");
        Scanner input = new Scanner(System.in);
        String originalString = input.nextLine();
        System.out.println("Original string: " + originalString);
        converter(originalString);
    }

Many thanks in advance!

(I'm new to programming so sorry if I'm missing something really obvious, I've tried everything I could find on Stack overflow)

hopper19
  • 153
  • 1
  • 9
  • What's going wrong with this solution? – Cache Staheli Jun 03 '16 at 14:48
  • Just realized, your `"\\s+"` replacement should suffice actually. – Mena Jun 03 '16 at 14:49
  • Is there an issue with the code you wrote? There's no need for the second replace AFAIK, but the first replace should take care of all whitespaces. See: http://stackoverflow.com/questions/5455794/removing-whitespace-from-strings-in-java – Vlad Schnakovszki Jun 03 '16 at 14:50
  • 1
    "sorry if I'm missing something really obvious".. Yes, the obvious thing you are missing is that you have already solved your problem. :) – Redtama Jun 03 '16 at 14:51
  • When I copy and paste a body of text as the input, the program only follows this process for the first line and then terminates. I tried using a loop but I must be doing it wrong – hopper19 Jun 03 '16 at 14:51
  • 1
    Please post the code where you call your `converter` method since it sounds like the problem you're describing is related to your input. – Redtama Jun 03 '16 at 14:53
  • 'public static void main(String [] args){ System.out.println("Enter string: "); Scanner input = new Scanner(System.in); String originalString = input.nextLine(); System.out.println(); System.out.println("Original string: " + originalString); converter(originalString); }' – hopper19 Jun 03 '16 at 14:56
  • (Sorry I don't know how to format the code for comments) – hopper19 Jun 03 '16 at 14:57
  • add the back-ticks around the code. these: ` – Cache Staheli Jun 03 '16 at 15:04
  • ` public static void main(String [] args){ System.out.println("Enter string: "); Scanner input = new Scanner(System.in); String originalString = input.nextLine(); System.out.println(); System.out.println("Original string: " + originalString); converter(originalString); } ` – hopper19 Jun 03 '16 at 15:10
  • Edit your question and post the code in there - makes it much easier for everyone to see! – Rossiar Jun 03 '16 at 15:13
  • Alright, thanks for the advice, will do – hopper19 Jun 03 '16 at 15:14

3 Answers3

3

Try this:

String line = "";
File f = new File("Path to your file");
FileReader reader = new FileReader(f)
BufferedReader br = new BufferedReader(reader);
String str = "";
while((line = br.readlLine())!=null)
{
    str += line; 
}

System.out.println(str);

for spaces:

str = StringUtils.replace(str," ","");
Mahbub Rahman
  • 1,295
  • 1
  • 24
  • 44
1

You pretty much got it! The only thing you are "missing" is that you added the extra .replaceAll when you didn't need to.

Also, it sounds like you may have different types of input, but here is a solution based on your code:

EDIT: Here is the solution below:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String [] args){
        System.out.println("Enter string: ");
        Scanner input = new Scanner(System.in);
        List<String> lines = new ArrayList<> ();
        String line = null;
        while (!(line = input.nextLine()).isEmpty()) {
            lines.add(line);
        }
        StringBuilder myOriginalString = new StringBuilder();
        for (String s : lines) {
            myOriginalString.append(s);
            myOriginalString.append(" ");
            myOriginalString.append("\n");
        }
        String originalString = myOriginalString.toString();
        System.out.println("Original string: \n"  + originalString);
        converter(originalString);
    }

    public static void converter(String inputString){
        String refinedString = inputString.replaceAll("\\s+","");
        System.out.println();
        System.out.println("Refined string: " + refinedString);
    }

}

Output:

Enter string: 
text here
text there
text everywhere

Original string: 
text here 
text there 
text everywhere 


Refined string: textheretexttheretexteverywhere

Process finished with exit code 0

I did my best to model it around the type of input you would be giving. Based on your question, I assume you would be copying and pasting text to the prompt when you run your program. If you are looking to read from a file, then Mahbubur Rahman's answer is correct (and I won't replicate it in this answer as he deserves the credit.)

abhi
  • 1,760
  • 1
  • 24
  • 40
  • Thank you, there's just one problem: I can't enter the string with many linebreaks (\n) because the purpose of the program is to copy a body of text straight into the input and have it converted. I'll try post my main function and show you what I mean – hopper19 Jun 03 '16 at 15:01
  • `public class Example { public static void main(String [] args){ System.out.println("Enter string: "); Scanner input = new Scanner(System.in); String originalString = input.nextLine(); System.out.println("Original string: " + originalString); converter(originalString); } public static void converter(String inputString){ String refinedString = inputString.replaceAll("\\s+","").replaceAll("\\\\n+", ""); System.out.println(); System.out.println("Refined string: " + refinedString); }` – hopper19 Jun 03 '16 at 15:03
  • @MatijaMilenovic thanks for the additional info. Edited your question for you and added in the updated solution above. It should work for you. – abhi Jun 03 '16 at 15:32
  • You're welcome @MatijaMilenovic! Good luck and welcome to programming and StackOverflow :) – abhi Jun 03 '16 at 16:41
0

This should work.

String list = "text here\n";
list += "text there\n";
list += "text everywhere\n";

System.out.println("Original :\n"+list);

list= list.replace("\n", "").replace(" ", "");
System.out.println("New :\n"+list);
Shank
  • 1,387
  • 11
  • 32