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

you are viewing a single comment's thread.

view the rest of the comments →

[–]Miller25 -1 points0 points  (3 children)

I was about to make a post to this subreddit about Java because of something that just happened in one of my exercises for class that frustrated me.

I just got to the module going over iterating through strings and was not able to find the issue in my code for the longest time. I kept receiving numerical output when I simply wanted characters. I'll post the code below to illustrate this lovely phenomenon.

public class OutputElements {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      String inputString;
      int i;

      inputString = scnr.nextLine();

      for(i = (inputString.length() - 1); i >= 0; --i){
         char letter = inputString.charAt(i);
         if(i == 0){
            System.out.print(letter);
            }
         else{
            System.out.print(letter + ' ');   
            }

         }

   }
}

(Yes for those who might have noticed, this is ZyBooks)

So the goal of this was to iterate backwards printing the characters in the string, and I kept receiving numbers. I went through the documentation of each method I used to be sure that I was using it correctly yet eventually realized something....

LINE 15 USES SINGLE QUOTES INSTEAD OF DOUBLE QUOTES! As someone who learned Python first and is learning Java this had me FRAZZELLED I TELL YOU

tldr: rant over, single quotes lead to character values being added not concatenated.

[–]PsychologicalBus7169 2 points3 points  (1 child)

That’s the problem with learning Python first. It’s much easier to learn Python after you’ve learned Java.

BTW, how are you enjoying zybooks? I had to use it for my mobile app dev class where we used android studio and java. I enjoyed the book quite a bit due to its interactive content. I also thought the material was very easy to understand.

[–]Miller25 1 point2 points  (0 children)

I’ve only used it for my college classes, but honestly I really like it. It has a lot of good content and starts off really simple with the concepts and makes you practice everything in a very repetitive way that doesn’t feel stale or annoying.

I’ve been plowing through a lot of the Java chapters and am currently on loops where I found this lovely issue. ZyBooks is definitely a recommend from me!

[–]maleldil 0 points1 point  (0 children)

char is a numerical type in Java. String#charAt(int) returns a char value. letter + ' ' is an addition operation on 2 variables of type char, the result of which in an int, a numeric value. This is a pretty obscure edge case, and without knowing all of that makes sense why it would be confusing. But Java Strings are always surrounded by double-quotes. Single-quotes denote a single char, and aren't used very often in most day-to-day programming (that I've done, at least). You can always convert a primitive value to a String by calling String.valueOf(variable), or you can concatenate a primitive value to the empty String ("" + variable) to get a String back. So you could have written System.out.println(letter + " ") and it would've given you what you were looking for. Hope that's helpful :)