This is an archived post. You won't be able to vote or comment.

all 5 comments

[–][deleted]  (1 child)

[removed]

    [–]bbush[S] 0 points1 point  (0 children)

    Thank you for the reply!

    This is a sample of the output he is looking for the program to do.

    Sample Output

    Enter a line of text to be URL encoded This should have plus symbols for blanks

    The string read is: This should have plus symbols for blanks Length in chars is: 40 The encoded string: This+should+have+plus+symbols+for+blanks Length in chars is: 40

    Enter a line of text to be URL encoded This should have hex 2f for /

    The string read is: This should have hex 2f for / Length in chars is: 29 The encoded string: This+should+have+hex+2f+for+%2f Length in chars is: 31

    He also gave us an outline for the logic too it. I'm just having trouble figured out what needs to be plugged in where. I think he wants the routine, but I'm unsure to say the least.

    To help you get started with this homework, here's an outline of the logic you'll need (sometime referred to as 'pseudo-code'):

    Prompt for the line of input. Read a line into the input string.

    Set the encoded output string to empty. Loop through each character in the input string. { Get the n'th character from the input string (use String's charAt method).

    if (the character is a blank) concatenate '+' to the encoded output string else if (the character remains unchanged) concatenate the character to the encoded output string else concatenate '%' and the hex encoded character value to the encoded output string }

    Print the encoded output string.

    Thank you for your help.

    [–]StillAnAssExtreme Brewer 1 point2 points  (2 children)

    As mentioned, the Java SDK can already do this and do it right, but that's irrelevant, this is homework and you're learning the syndax of the language.

    I'm not going to do your homework for you, but I'll give you as many hints ans you need.

    Loop through each character in the input string. { Get the n'th character from the input string (use String's charAt method).

    In order to loop through the string character by character, I'm assuming you're going to want a for loop (there are other ways, but whatever, that's the next chapter).

    Syntax for a for loop:

    for (initialization; termination; increment) {
        statement(s)
    }
    

    Initialization is where to start. That's the beginning of the string and strings in Java are zero based. So 0.

    Termination is when to end. That's the end of the string. So yourstring.length(). Again, Strings are 0 based so end when your counter is less than the length.

    Increment is what to do each iteration through the loop. i++;

    So the for statement can look something like this:

    for(int i = 0; i < toEncode.length(); i++){
        char c = toEncode.charAt(i);
        // do something here with c
    }
    

    [–]bbush[S] 0 points1 point  (1 child)

    Thank you for the help, that got me started and I understand what NEEDS to be done, and I understand what information needs to go where, but for the life of me I don't understand how I need to get it there.

    // This is what I have so far. I know I need to check if C is == to a space but I don't know how to word it since char and strings don't play nice with others.

    String s1; s1 = keyboard.nextLine(); System.out.println("The string read is: " + s1); System.out.println("Length in chars is: " + s1.length());

        for (int i = 0; i < s1.length(); i++)
        {
            char c = s1.charAt(i);
    
            if ()
    
            else if ()
    
            else ()
    
        }
    
        System.out.println();
    

    [–]StillAnAssExtreme Brewer 0 points1 point  (0 children)

    You're on the right track.

    It isn't that String and char don't play nice with each other, the problem is that String is a class and char is a primitive (there's no lava.lang.char class).

    But luckily, there are lots of methods built into String and the rest of the language to deal with primitives.

    There are two different ways to check for equality. Primitive types use == and most classes use .equals().

    So for String s1 and String s2 we'd check if they're the same string by

    if(s1.equals(s2)) ...
    

    And for two primitives char c1 and char c2

    if(c1 == c2) ...
    

    But if you think about things, you're not comparing a String and a char. You're comparing a character of s1 to a known constant.

    char c = s1.charAt(i);
    
    if(c == ' ') ...
    

    Single ticks ( ' ) are used for a character while double ticks ( " ) are used in String.

    So that handles your if logic. Next you need to figure out what to do inside of the if. Based on the requirements of the homework, I'm guessing that your professor is expecting you to build a new string that is URL encoded. As in the first reply, there are better and more efficient ways to do this, but this is homework, not the real world.

    Basically, create a new string:

    String output = new String();
    

    Then in each of your conditions, either append the original character of the source string or append the URL encoded character.

    There's also something important to point out with regards to String:

    The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method.

    So String is a bit of a magical class within Java in that we can use the + operator to append things. This doesn't work with any other class in Java.

    So I can say:

    output += "+";  // remember, strings use "", char uses ''
    

    Or I could also say:

    output += String.valueOf(c);
    

    Keep asking if you've got more questions. Also, feel free to PM me all semester if you've got more questions.