Question

  • Creator
    Topic
  • #4011757

    I can’t find an error in the Java code

    Locked

    by HarryStoun ·

    I wrote the source code for a new application on IOS, when writing there were no errors, but when I ran the code, an error appeared in this place:
    Code:
    var t = readLine()!
    var s = readLine()!
    var len_s = s.count
    var t_lis = Set(t)
    var c_s = Counter(s)
    var c_t = Counter(t_lis[len_s])
    var c_res = [String: String]()
    var summ = 0
    for e in c_s{
    c_res[e] = [c_s[e], min( c_s[e], c_t[e] )]
    summ += c_res[e][1]}
    for i in (t.count-s.count) + 1 {
    if summ == len_s-1{
    print(i)
    break

    }
    if t[i] in c_res
    if c_res[t[i]][1] > 0{
    c_res[t[i]][1] -= 1
    summ -= 1
    }
    if i+len_s < t.count && t[i+len_s] in c_res
    if c_res[ t[i+len_s] ][1] < c_res[ t[i+len_s] ][0]{
    c_res[ t[i+len_s] ][1] += 1
    summ += 1

    }
    else
    print(-1)
    }
    Who can tell what is wrong?

    • This topic was modified 2 years, 1 month ago by Avatar photokees_b.

All Answers

  • Author
    Replies
    • #4011815
      Avatar photo

      Re: java

      by kees_b ·

      In reply to I can’t find an error in the Java code

      This doesn’t look like a full Java method, so it’s quite understandable the Java interpreter gives an error. Then it’s up to you to fix it.

      May I remark that print(-1) doesn’t make much sense in an iOS app?

      • #4011825

        Reply To: I can’t find an error in the Java code

        by freddiec341 ·

        In reply to Re: java

        The error is likely due to the fact that you are attempting to access elements in an array that do not exist. In the code, you are accessing elements in the array t_lis with the index len_s. However, since len_s is the length of the string s, it will be larger than the length of t_lis, which will result in an out of bounds error. To fix this, change the line of code to access the last element in t_lis instead of using the index len_s.

        Note: irrelevant link removed by moderator.

        • This reply was modified 2 years, 1 month ago by freddiec341.
        • This reply was modified 2 years, 1 month ago by Avatar photokees_b.
    • #4079669

      Java Code Error Solution

      by jongore219 ·

      In reply to I can’t find an error in the Java code

      It seems like the error is due to a syntax issue in the following line:

      css
      Copy code
      c_res[e] = [c_s[e], min( c_s[e], c_t[e] )]
      The issue is that the square brackets [] are being used to create a list, but the type of c_res[e] is a dictionary. To fix this issue, you can change the square brackets to regular parentheses to create a tuple instead, like this:

      css
      Copy code
      c_res[e] = (c_s[e], min( c_s[e], c_t[e] ))
      In addition to this, there are a few other things that you may want to consider:

      1- The loop condition for i in (t.count-s.count) + 1 may not be what you intended. It will only loop once if t.count-s.count is equal to zero, and it will loop one time too many if t.count-s.count is greater than one. You may want to revise this condition to ensure that it loops over the correct range.

      2- The if statements that check if t[i] in c_res and t[i+len_s] in c_res are missing colons at the end. You should add colons to the end of these statements to make them syntactically correct.

    • #4129770

      Reply To: I can’t find an error in the Java code

      by amandeep.janbask@gmail.com ·

      In reply to I can’t find an error in the Java code

      If you’re having trouble finding an error in your Java code, here are some steps you can take to help identify and resolve the issue:

      Check for error messages: Compile and run your Java code, and carefully read any error messages or exceptions that are displayed. The error message will often provide helpful information about the location and nature of the error.

      Review the code logic: Review your code’s logic and algorithms to ensure they are correctly implemented. Look for any logical errors or mistakes in your program flow that might be causing unexpected behavior.

      Verify variable assignments: Check if all variables are properly declared and assigned the correct values. Ensure that variables are of the correct type and that they are used consistently throughout your code.

      Check syntax and punctuation: Carefully review the syntax and punctuation in your code. Look for missing or mismatched parentheses, brackets, semicolons, or other punctuation marks that could be causing a syntax error.

      Use a debugger: Debuggers can be powerful tools for identifying errors in your code. Set breakpoints at critical points in your code and step through it line by line, observing the values of variables and checking if they match your expectations.

      Review the documentation: Consult the documentation for any libraries or APIs you are using to ensure you are using them correctly. Incorrect usage of methods or parameters can lead to errors.

      Ask for help: If you’re still unable to identify the error, consider asking for help from peers, online communities, or forums dedicated to Java programming. Provide them with the relevant portions of your code and any error messages you have received.

      Remember that providing the specific error message or a code snippet that you’re having trouble with can help others assist you more effectively.

Viewing 2 reply threads