I don't understand why I get mistakes.

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 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:21 AM

Line 9 ? You mean 6 and 7 ?

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

Was This Post Helpful? 0
  • +
  • -

#17 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:37 AM

Well at least you fixed lines 6 and 7. Line 8 doesn't make any sense, does it? Think about what you're doing...
Was This Post Helpful? 0
  • +
  • -

#18 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:42 AM

But shouldn't I add those letters to my empty string ?? to create a new string and return the result ??
Was This Post Helpful? 0
  • +
  • -

#19 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:45 AM

That line is the wrong way round. You have to assign to result and not the value that is returned by charAt (i.e. result += str.charAt(i)).

You're also still attempting to use a field called length. Again, it's a method, so you need to call it.
Was This Post Helpful? 1
  • +
  • -

#20 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:51 AM

Finnaly I wrote the code, but now it is not uppercasing letters.
public String toUpperCase(String str) 
    {
    String result = "";
    for(int i = 0; i < str.length(); i++)     
        {
            Character.toUpperCase(str.charAt(i));
            Character.toString(str.charAt(i));
            result+= str.charAt(i);
        }
    return result;     
    }
 

Was This Post Helpful? 0
  • +
  • -

#21 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 - 08:02 AM

The methods toUpperCase and toString both return values, which you're throwing away. You need to store them.
Was This Post Helpful? 1
  • +
  • -

#22 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 - 08:11 AM

Store them as strings and chars right ? So after doing that, am I going to add them to result+= str.charAt(i); ??
Was This Post Helpful? 0
  • +
  • -

#23 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 - 08:17 AM

Well no, presumably you want to append the uppercased characters..

You need to be trying things out and seeing how they work. I'd strongly advise you to print the values out to see what's going on (or use a debugger).
Was This Post Helpful? 2
  • +
  • -

#24 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 - 08:23 AM

I made it, Thank you very much :))
Was This Post Helpful? 0
  • +
  • -

#25 g00se   User is offline

  • D.I.C Lover
  • member icon

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

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

Posted 03 February 2018 - 09:46 AM

You can simplify your code as well as make it perform better thus:

    public String toUpperCase(String str) {
        StringBuilder sb = new StringBuilder(str);
        for (int i = 0; i < sb.length(); i++) {
            sb.setCharAt(i, Character.toUpperCase(sb.charAt(i)));
        }
        return sb.toString();
    }


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2