0

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();
    }
}
ATP
  • 2,939
  • 4
  • 13
  • 34
Charis
  • 1
  • 2
  • 2
    Extra line of what. What' the expected behaviour? Hard to explain whats wrong without knwowing whats should be right. – Lekan Swansons Jun 28 '22 at 20:51
  • Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – sorifiend Jun 28 '22 at 21:24

3 Answers3

1

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");
}
Cheng Thao
  • 1,467
  • 1
  • 3
  • 9
0

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();
}

}
Bozon
  • 46
  • 3
  • Yes, I couldn't make out what your code was supposed to do, and I didn't have access at work to the day 6 ... So include sample input and expected output in the future, please. – Bozon Jun 28 '22 at 21:07
0

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.