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"
.
You can use charAt() function to find out spaces in string.
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 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.
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.
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);
}
}
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With