-6

the link of the problem : http://codeforces.com/contest/734/problem/A and what is the problem of my code

My Code :

import java.util.Scanner;
import java.lang.String;
public class Main
{

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int n=in.nextInt();
        String s=in.nextLine();
        int d=0;
        int a=0;
        for(int i=0;i<n;i++)
        {
            if(s.charAt(i)=='A')
                a++;
            else
                d++;
        }

        if(a>d)
            System.out.println("Anton");
        else if(a<d)
            System.out.println("Danik");
        else
            System.out.println("Friendship");
    }
}
Andy Turner
  • 122,430
  • 10
  • 138
  • 216
  • So what exactly is the problem? Are you getting an error? The wrong result? – Mureinik Jan 01 '19 at 12:55
  • Did you have a question? Is something not working? – David Jan 01 '19 at 12:55
  • I've got a run time error – Aymen Mellah Jan 01 '19 at 12:57
  • 1
    "I've got a run time error" Unless you give details of the error (specifically: the full stack trace), it is very hard to help you. – Andy Turner Jan 01 '19 at 12:58
  • @AymenMellah: Reading the error message and debugging at the line where it happens would be a good first step. It's still not clear what exactly you're expecting from us here. – David Jan 01 '19 at 12:58
  • 1
    I imagine the problem you're having is that `s` is empty, so you're getting a `StringIndexOutOfBoundsException`. [See this](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo). – Andy Turner Jan 01 '19 at 13:00
  • @AymenMellah don't put that in a comment: [edit] the question. – Andy Turner Jan 01 '19 at 13:01

2 Answers2

0

The Scanner.nextInt() method does not read the newline character in your input created by hitting Enter, So change your code like below instead of using nextInt();

Scanner in = new Scanner(System.in);
int n = Integer.parseInt(in.nextLine());
String s = in.nextLine();

This will fix the issue

Sandeepa
  • 2,522
  • 3
  • 14
  • 34
-1

Scanner in=new Scanner(System.in); does not read the new line character , so i had created new object for the next line read , it will solve the problem

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    in = new Scanner(System.in);
    String s = in.nextLine();
    if (n == s.length()) {
        int d = 0;
        int a = 0;
        for (int i = 0; i < n; i++) {
            if (s.charAt(i) == 'A')
                a++;
            else
                d++;
        }

        if (a > d)
            System.out.println("Anton");
        else if (a < d)
            System.out.println("Danik");
        else
            System.out.println("Friendship");
    }
}
  • "Should" Are you unsure? Why do you think it does? Please explain your answer, rather than just dumping code (and, by the way, [it doesn't work](https://ideone.com/bdJHXf)). – Andy Turner Jan 01 '19 at 13:11
  • new Scanner(System.in); does not read the new line character , so i had created new object for the next line read ..its working i have checked..as per problem statement input number and string length should be same ..added check for the same..hope this will be more clear – Vitthal Devkar Jan 01 '19 at 13:14
  • "its working i have checked" It's not working, [I've checked](https://ideone.com/bdJHXf). – Andy Turner Jan 01 '19 at 13:18
  • I don't see any issue anyway .. – Vitthal Devkar Jan 01 '19 at 13:23