I don't understand why I get mistakes.

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 647 Views - Last Post: 03 February 2018 - 09:46 AM Rate Topic: -----

#1 Xhertus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 03-February 18

I don't understand why I get mistakes.

Posted 03 February 2018 - 05:27 AM

public String toUpperCase(String str) 
    {
    String result = "";
[b]   int length = str.length; [/b]
    for(int i = length; i < 0; i--)     
        {
            char cur = str.charAt(i);
            Character.toUpperCase(cur);
            Character.toString(cur);
             [b]cur+= result;[/b]  
        }
    return result;     
    }



Hello dear members, I am new on this forum and new at coding. I don't understand why, but I got some errors at line 4 and line 12. It says length is out of scope at line 4 and at line 12, it says String cannot be converted to char (but I have informed that Character.toString method does that job). I am waiting for advice and solutions.

This post has been edited by ndc85430: 03 February 2018 - 05:39 AM
Reason for edit:: Added code tags. Please do this yourself in future.


Is This A Good Question/Topic? 0
  • +

Replies To: I don't understand why I get mistakes.

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3566
  • View blog
  • Posts: 16,258
  • Joined: 20-September 08

Re: I don't understand why I get mistakes.

Posted 03 February 2018 - 05:34 AM

Please use code tags as you were asked. andrewsw's Posted Image instructions will show you how to use code-tags. It isn't difficult.
EACH JAVA FILE should have its own code tags

Why are you writing a routine to convert a String to upper case? It already has one
Was This Post Helpful? 0
  • +
  • -

#3 Xhertus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 03-February 18

Re: I don't understand why I get mistakes.

Posted 03 February 2018 - 05:51 AM

I know it has but the program says me to do it with my own codes...

This post has been edited by ndc85430: 03 February 2018 - 06:59 AM
Reason for edit:: Removed quote of previous post. Just press "Reply".

Was This Post Helpful? 0
  • +
  • -

#4 Xhertus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 03-February 18

Re: I don't understand why I get mistakes.

Posted 03 February 2018 - 06:05 AM

I realized that I am writing the word from the end /fp...
Please ignore that mistake, I fixed that.
Was This Post Helpful? 0
  • +
  • -

#5 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3566
  • View blog
  • Posts: 16,258
  • Joined: 20-September 08

Re: I don't understand why I get mistakes.

Posted 03 February 2018 - 06:19 AM

You need to be more specific:
http://technojeeves....java1/17-errors
Was This Post Helpful? 0
  • +
  • -

#6 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7205
  • View blog
  • Posts: 15,018
  • Joined: 16-October 07

Re: I don't understand why I get mistakes.

Posted 03 February 2018 - 06:44 AM

With what's shown:
public String toUpperCase(String str) {
    String result = "";
    int length = str.length; // this is not a method call
    for(int i = length; i < 0; i--) {
        // there is no char at length
        // the chars will be at 0..length-1
        // so, it should crash here, regardless
        char cur = str.charAt(i);
        // this makes no sense, as you don't store the result
        Character.toUpperCase(cur);
        // see above
        Character.toString(cur);
        // so, cur is of type char
        // this is simply wrong
        // also, cur is declared in scope, this this is pointless
        cur+= result;
    }
    return result;     
}


This post has been edited by baavgai: 03 February 2018 - 06:46 AM

Was This Post Helpful? 1
  • +
  • -

#7 Xhertus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 03-February 18

Re: I don't understand why I get mistakes.

Posted 03 February 2018 - 06:46 AM

Grader.java : Line 4: You may forgotten to declare or it's out of scope.

Grader.java: Line 12: Incompatible types: String cannot be converted to char.

Those are exact errors..
Was This Post Helpful? 0
  • +
  • -

#8 Xhertus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 03-February 18

Re: I don't understand why I get mistakes.

Posted 03 February 2018 - 06:58 AM

public String toUpperCase(String str) 
    {
    String result = "";
    for(int i = 0; i < str.length; i++)     
        {
            Character.toUpperCase(charAt(i));
            Character.toString(charAt(i));
            charAt(i)+= result;
        }
    return result;     
    }



I changed my code as this, but I still get some errors.

Line 4: You may forgotten to declare length or it's out of scope.
Line 6,7,8: You may forgotten to declare charAt(int) or it's out of scope.

I didn't understand why it's called charAt(int), and can't I use same charAt(i) at different statments? and why? I said that I am new.

Note: Thanks for your reply baavgai, it was really helpful to me.
Was This Post Helpful? 0
  • +
  • -

#9 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 869
  • View blog
  • Posts: 3,491
  • Joined: 13-June 14

Re: I don't understand why I get mistakes.

Posted 03 February 2018 - 07:02 AM

charAt is a method in the String class. You get the error because you're attempting to call a method called charAt in your own class. Presumably you wanted to call it on str...
Was This Post Helpful? 0
  • +
  • -

#10 Xhertus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 03-February 18

Re: I don't understand why I get mistakes.

Posted 03 February 2018 - 07:07 AM

#9 ndc85430

Line 4: You may forgotten to declare length or it's out of scope.

How about this ??
Was This Post Helpful? 0
  • +
  • -

#11 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 869
  • View blog
  • Posts: 3,491
  • Joined: 13-June 14

Re: I don't understand why I get mistakes.

Posted 03 February 2018 - 07:09 AM

String doesn't have a field called length - it's a method.
Was This Post Helpful? 0
  • +
  • -

#12 Xhertus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 03-February 18

Re: I don't understand why I get mistakes.

Posted 03 February 2018 - 07:10 AM

So how can I say to do it length of string ???
Was This Post Helpful? 0
  • +
  • -

#13 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 869
  • View blog
  • Posts: 3,491
  • Joined: 13-June 14

Re: I don't understand why I get mistakes.

Posted 03 February 2018 - 07:11 AM

How do you call a method?
Was This Post Helpful? 1
  • +
  • -

#14 Xhertus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 03-February 18

Re: I don't understand why I get mistakes.

Posted 03 February 2018 - 07:14 AM

What do you mean by call a method ??
Was This Post Helpful? 0
  • +
  • -

#15 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 869
  • View blog
  • Posts: 3,491
  • Joined: 13-June 14

Re: I don't understand why I get mistakes.

Posted 03 February 2018 - 07:15 AM

Exactly what I said. If you've been working with Java, you should have come across the terms "method" and what it means to call one. You're doing it on lines 8 and 9...
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2