0

I'm trying to input some test cases at once to this program and it just keeps outputting the result for the last input only without the remaining inputs

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;


public class BeautifulStrings2 {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        //File f=new File("input.txt");
        Scanner in=new Scanner(System.in);
        BufferedReader in1=new BufferedReader(new InputStreamReader(System.in));
        int n=in.nextInt();
        for (int i=1;i<=n;i++){
            String s=in1.readLine();
            System.out.println(s);
            String chars="";
            if (Character.isUpperCase(s.charAt(0))){
                chars+=s.charAt(0);
            }
            else {
                chars+=Character.toUpperCase(s.charAt(0));
            }
            for (int j=0;j<s.length();j++){
                boolean flag=true;
                for (int k=0;k<chars.length();k++){
                    if (Character.isLetter(s.charAt(j))){
                        if (chars.charAt(k)==Character.toUpperCase(s.charAt(j))){
                            flag=false;
                            break;
                        }
                    }
                }
                if (flag){
                    chars+=Character.toUpperCase(s.charAt(j));
                }
            }
            String temp=chars;
            chars="";
            for (int j=0;j<temp.length();j++){
                if (temp.charAt(j)>64&&temp.charAt(j)<91)
                    chars+=temp.charAt(j);
            }
            int [] numbers=new int[chars.length()];
            Arrays.fill(numbers, 0);
            for (int j=0;j<chars.length();j++){
                for (int k=0;k<s.length();k++){
                    if (chars.charAt(j)==Character.toUpperCase(s.charAt(k))){
                        numbers[j]++;
                    }
                }
            }
            Arrays.sort(numbers);
            int factor=26,sum=0;
            for (int j=chars.length()-1;j>=0;j--){
                sum+=numbers[j]*factor;
                factor--;
            }
            System.out.println("Case #"+i+": "+sum);
        }
    }

}

sample input

5
ABbCcc
Good luck in the Facebook Hacker Cup this year!
Ignore punctuation, please :)
Sometimes test cases are hard to make up.
So I just go consult Professor Dalves

what the output should be

Case #1: 152
Case #2: 754
Case #3: 491
Case #4: 729
Case #5: 646

the actual output

Case #1: 646

EDIT ! Now I've edited my code to use only Scanner object and added in.nextLine() to skip the first line

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;


public class BeautifulStrings2 {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        //File f=new File("input.txt");
        Scanner in=new Scanner(System.in);
        //BufferedReader in1=new BufferedReader(new InputStreamReader(System.in));
        int n=in.nextInt();
        in.nextLine();
        for (int i=1;i<=n;i++){
            String s=in.nextLine();
            System.out.println(s);
            String chars="";
            if (Character.isUpperCase(s.charAt(0))){
                chars+=s.charAt(0);
            }
            else {
                chars+=Character.toUpperCase(s.charAt(0));
            }
            for (int j=0;j<s.length();j++){
                boolean flag=true;
                for (int k=0;k<chars.length();k++){
                    if (Character.isLetter(s.charAt(j))){
                        if (chars.charAt(k)==Character.toUpperCase(s.charAt(j))){
                            flag=false;
                            break;
                        }
                    }
                }
                if (flag){
                    chars+=Character.toUpperCase(s.charAt(j));
                }
            }
            String temp=chars;
            chars="";
            for (int j=0;j<temp.length();j++){
                if (temp.charAt(j)>64&&temp.charAt(j)<91)
                    chars+=temp.charAt(j);
            }
            int [] numbers=new int[chars.length()];
            Arrays.fill(numbers, 0);
            for (int j=0;j<chars.length();j++){
                for (int k=0;k<s.length();k++){
                    if (chars.charAt(j)==Character.toUpperCase(s.charAt(k))){
                        numbers[j]++;
                    }
                }
            }
            System.out.println(chars);
            Arrays.sort(numbers);
            int factor=26,sum=0;
            for (int j=chars.length()-1;j>=0;j--){
                sum+=numbers[j]*factor;
                factor--;
            }
            System.out.println("Case #"+i+": "+sum);
        }
    }

}

the output

Case #1: 152
Case #2: 754
Case #3: 491
Case #4: 729

and after I press enter the fifth line appear

Case #5: 646

how can I make it appear without having to press enter ?!

user1712638
  • 201
  • 2
  • 8
  • 22
  • *Related Question*: [Scanner issue when using nextLine after nextInt](http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextint) – Eng.Fouad Jan 27 '13 at 02:38
  • I tried adding in.nextLine(); before the main loop .. still the same !! – user1712638 Jan 27 '13 at 02:43
  • Ok I removed the bufferedreader and kept the in.nextLine() before the main loop .. now it outputs 4 outputs and I still have to press enter for the fifth output to appear !! – user1712638 Jan 27 '13 at 02:48
  • If you've changed the code and now have a new problem, then you will want to post the *latest* code and edit your question. I'd leave your current code in place and add your new code to the bottom so as to not make our current answers look irrelevant. – Hovercraft Full Of Eels Jan 27 '13 at 02:49

1 Answers1

2

You are using the standard input stream, System.in, twice in two different stream readers, one a Scanner, and the other a BufferedReader:

Scanner in=new Scanner(System.in);
BufferedReader in1=new BufferedReader(new InputStreamReader(System.in));

That looks to be a very dangerous thing to do and prone to error. Why not simply use the Scanner (or BufferedReader) for everything?

If you do use a Scanner for all, then yes, use nextLine() after nextInt() if the next token will be on the next line. If using the Scanner, you wouldn't even need the 5 number at the top of the file since you'd read lines until the scanner's hasNextLine() returns false.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373