I have been trying to solve day 6 of Hackerrank's 30 days of code in Java 8. My output is generally correct, but there is an extra line in the beginning. Is there any way I can get rid of it?
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt();
for(int i = 0; i <= T; i ){
String SJ = scanner.nextLine();
if(SJ.length() % 2 == 1){
for(int k = 0; k < (SJ.length() 1)/2; k ){
System.out.print(SJ.charAt(k*2));
}
System.out.print(" ");
for(int j = 0; j < (SJ.length()-1)/2; j ){
System.out.print(SJ.charAt(j*2 1));
}
}
else{
for(int k = 0; k < SJ.length()/2; k ){
System.out.print(SJ.charAt(k*2));
}
System.out.print(" ");
for(int j = 0; j < SJ.length()/2; j ){
System.out.print(SJ.charAt(j*2 1));
}
}
System.out.print("\n");
}
scanner.close();
}
}
CodePudding user response:
The problem is that scanner.nextInt()
reads an integer but leaves the linke break "\n" in the memory buffer. So, the first iteration of your loop reads the line break. The extra line is the result of sb.append("\n")
in the first iteration.
int T = scanner.nextInt();
for(int i = 0; i <= T; i ){
String SJ = scanner.nextLine(); //first iteration read is the empty line
...
sb.append("\n");
}
You need to consume the line break so that it's not read in the loop.
int T = scanner.nextInt();
scanner.nextLine(); // <=== consume line break
for(int i = 0; i <= T; i ){
String SJ = scanner.nextLine();
...
sb.append("\n");
}
CodePudding user response:
The easiest way, without changing your code structure is not to print until the end. Use a StringBuffer or StringBuilder to hold the output and then clean up what you don't want before you print.
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
int T = scanner.nextInt();
for(int i = 0; i <= T; i ){
String SJ = scanner.nextLine();
if(SJ.length() % 2 == 1){
for(int k = 0; k < (SJ.length() 1)/2; k ){
//System.out.print(SJ.charAt(k*2));
sb.append(SJ.charAt(k*2));
}
//System.out.print(" ");
sb.append(" ");
for(int j = 0; j < (SJ.length()-1)/2; j ){
//System.out.print(SJ.charAt(j*2 1));
sb.append(SJ.charAt(j*2 1));
}
}
else{
for(int k = 0; k < SJ.length()/2; k ){
//System.out.print(SJ.charAt(k*2));
sb.append(SJ.charAt(k*2));
}
//System.out.print(" ");
sb.append(" ");
for(int j = 0; j < SJ.length()/2; j ){
//System.out.print(SJ.charAt(j*2 1));
sb.append(SJ.charAt(j*2 1));
}
}
//System.out.print("\n");
sb.append("\n");
}
String str = sb.toString().replaceFirst("^\\s ", "") ;
System.out.print(str) ;
scanner.close();
}
}
CodePudding user response:
You can use the formatter option to format the value as currency:
series: [{
type: 'map',
name: 'Purchases $',
states: {
hover: {
color: '#BADA55',
},
},
dataLabels: {
enabled: true,
format: '{point.name}',
formatter: function() {
return '$' this.y;
}
},
allAreas: true,
data: this.model,
}],
You can't access the data directly from the query. You need to use the snapshot listener to get the data.
You can use the query method to get the data from the query.
query(collectionRef, where(...q), orderBy(...ob), limit(...lmt))
You can use the onSnapshot method to get the data from the snapshot listener.
onSnapshot(collectionRef, snapshot => {
let results = []
snapshot.docs.forEach(doc => {
results.push({ ...doc.data(), id: doc.id })
})
// update values
documents.value = results
})
The snapshot listener will return the data from the query.
One potential issue could be that your CSV file has an extra line at the end. Try removing that extra line and see if that fixes the issue.