-2

This is a specific question, don't downvote it just because it doesn't help you.

  public class Answer {
  public static String answer(int n) {
    String nums="";
    int limit = 10005;
    int x=2;
    while(limit>0){
        if(isPrime(x)){
            limit-=String.valueOf(x).length();
            nums = nums + String.valueOf(x);
        }
        x+=1;
    }
    String out="";
    if(n==0){
        out="23571";
    }else{
        for(int i=1;i<6;i++){
            out += String.valueOf(nums.charAt(n+i));
        }
        //Problem Solved: instead of this loop, it should be out = nums.substring(n,n+5)
    }
    return out;
}
public static boolean isPrime(int number) {
    for(int check = 2; check < number; ++check) {
        if(number % check == 0) {
            return false;
        }
    }
    return true;
  }
}

Nothing is wrong with this code as far as I know, I'm just using it as an example for you to use.

"It must implement the answer() method in the solution stub." was in the directions for me, but I don't know much about the vocabulary of programming, I only understand logic behind programming, so this is the only thing I don't know how to solve. So what I am asking is where do I put the "answer()" at in this program?

It was looking for substring, which I didn't include because I haven't used java in about a year and simply forgot about it.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Your question is a bit vague!!! Please explain it that how do you want the `answer()` method to use i.e. display the contents or something... –  Oct 15 '17 at 08:21
  • I just don't understand what the "stub" is. I can't really answer your question because the directions were vague as well. – Mr. Hopeless Oct 15 '17 at 08:34
  • See as far as I see you are confused about the meaning of "stub". So here is the answer which will explain your doubts [**stub**](https://stackoverflow.com/questions/9777822/what-does-to-stub-mean-in-programming). –  Oct 15 '17 at 08:36
  • I sort of understand it, so right now, to give you some context on my programming life, I am mostly working in python and haven't touched java in about a year, is it possible to run the "answer()" method in the class "Answer"? – Mr. Hopeless Oct 15 '17 at 08:42
  • Yes I have mentioned in my answer!!!! Please have a look at it and do tell if it served your purpose or your are expecting some other fix!!! –  Oct 15 '17 at 08:43
  • Please put your answer below rather than updating the question with `[SOLVED]` – OneCricketeer Oct 15 '17 at 21:53
  • I'm new as you can tell, and I was going to include the edits as comments within the original, just never got to do that because you edited my question before I could save. – Mr. Hopeless Oct 15 '17 at 21:55

1 Answers1

0

Here as I can figure out you have problems in understanding the meaning of "stub". It is simply the test method as provided by the answer here. And if you want to test the above code you have to implement the main method in your code to do the same. Something like this

 public static void main(String [] args){
  //Either use Scanner object or provide the hard coded input as per your requirements
  Scanner sc = new Scanner(System.in);
  int n = sc.nextInt();
  System.out.println(answer(n));
 }

EDIT AS PER OP REQUIREMENT

Okay so as per your requirement it is asking you for the unit test. There are many ways to do it but my preferred is to make stub concrete class

Implementation an stub concrete class in JUNIT

class Answer {
 public String answer(int n){
    // Code body
  return "result"// in your case out variable
  }
}

class solution extends Answer {
@Override
public String answer(int n){
    //return "your stubbed result";


  }
}
  • That isn't wrong, it's just not exactly what my challenge was looking for. And I am sorry that I can't specify what it is looking for because the directions are extremely vague. Thank you for trying to help. – Mr. Hopeless Oct 15 '17 at 09:08
  • Something you can explain so that I could rectify your problem.I would be happy to help. –  Oct 15 '17 at 09:09
  • I just updated the question with the prompt I was given, if it might help you understand. – Mr. Hopeless Oct 15 '17 at 09:13
  • I have edited my answer as per your requirement. Please have a look at it and it might rectify your problem –  Oct 15 '17 at 09:28
  • So I don't have access to what they are inputting, so would I literally return the variable name? – Mr. Hopeless Oct 15 '17 at 09:48
  • No in that case just `return super.answer(n)` –  Oct 15 '17 at 09:50
  • I figured it out, you didn't help me, but at least you were nice enough to try. The problem was I was supposed to use substring instead of using a for loop to go through the next 5 digits. – Mr. Hopeless Oct 15 '17 at 21:50
  • The problem was that I didn't recieve specific input from the execution – Mr. Hopeless Oct 16 '17 at 13:05
  • @Mr.Hopeless - I’m facing the same problem, how did you fix the issue of “ Error compiling the code, please try again later “. My code is compiling without errors on my machine and I’m making use of substring in my answer method. But every time I verify the solution I get same error “error compiling code , please try again later” – Rishab777 Nov 18 '17 at 23:53
  • @Mr.Hopeless what does this mean ? – Rishab777 Nov 19 '17 at 01:31