Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return my string value with whitespace

How can I modify my main class so that I can receive values without losing the whitespace?

Here is a portion of my main class:

StringBuilder sb = new StringBuilder();
Scanner s = new Scanner(new FileReader(new File("C:/User/waithaka/Documents//duka.json")));
while (s.hasNext()) {
    sb.append(s.next());
}
s.close();

GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();

String jstring = sb.toString();
Result results = gson.fromJson(jstring, Result.class);
List<data> dat = response.getData();
for (data data : dat) {
    System.out.println("The item is " + data.getItem());
}

The last line prints the values without whitespace. For example, a value with the string "Black and yellow" will be changed to "Blackandyellow".

like image 323
Anju Blax Avatar asked Jul 06 '15 14:07

Anju Blax


People also ask

How do you get whitespace string?

You can use charAt() function to find out spaces in string.

How do you check a string with spaces?

In order to check if a String has only unicode digits or space in Java, we use the isDigit() method and the charAt() method with decision making statements. The isDigit(int codePoint) method determines whether the specific character (Unicode codePoint) is a digit. It returns a boolean value, either true or false.

How do I find a whitespace character in a string?

How do you find the whitespace of a string? s = '\t \n' if s. isspace() == True: print('All whitespace characters') else: print('Contains non-whitespace characters') s = '2+2 = 4' if s. isspace() == True: print('All whitespace characters') else: print('Contains non-whitespace characters.

How do you split a string by whitespace?

You can split a String by whitespaces or tabs in Java by using the split() method of java. lang. String class. This method accepts a regular expression and you can pass a regex matching with whitespace to split the String where words are separated by spaces.


2 Answers

If you look at Scanner.java class you can see white space treated as new line characters.

public Scanner(InputStream source) {
        this(new InputStreamReader(source), WHITESPACE_PATTERN);
    }

You need to set line separator as

s.useDelimiter(System.getProperty("line.separator"));

I was able to replicate and fix the problem, below is the code -

JSON

{
name:"Stack-overflow",
url:"http://www.stack-overflow   .com"
}

And here's the class

public class GsonTest {

    public static void main(String[] args) throws FileNotFoundException {
        StringBuilder sb = new StringBuilder();

        Scanner s = new Scanner(new FileReader(new File(
                "/Users/chatar/Documents/dev/projects/stack-overflow/stack-overflow/bin/C:/User/waithaka/Documents/duka.json")));

        s.useDelimiter(System.getProperty("line.separator"));
        while (s.hasNext()) {
            String nxt = s.next();
            sb.append(nxt);
        }
        s.close();

        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.create();

        String jstring = sb.toString();
        Result results = gson.fromJson(jstring, Result.class);

        System.out.println("The item is " + results);
    }
}
like image 100
TheCodingFrog Avatar answered Sep 29 '22 07:09

TheCodingFrog


I think that your problem comes from using Scanner, since it may be trimming white spaces. As you are already using, some Google libraries (Gson), I would also use Google's guava, its Files class has a toString() method that will read a whole file to a String:

Files.toString()

like image 23
crigore Avatar answered Sep 29 '22 06:09

crigore