• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Newbie having a problem with my java code assignment

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I'm taking a java class for college credit. I haven't had much experience so forgive if this question comes off as dumb. I'm writing some code as part of a lesson and I have most all of it done and working except one very annoying piece that is prohibiting me from making the program work exactly as I've been told it should. Basically, when my for loop below finishes, I want to add a new System.out.println("enter text for new input here) and a new line of input. What I'm seeing happening is when my value is returned, it seems to be overwriting part of my next println statement. I'm also attaching the code I've written - I know it isn't the most 'elegant' but please be kind!

Thank you in advance for whatever pointers you can throw my way!

screenshot.jpg
[Thumbnail for screenshot.jpg]
 
Ranch Hand
Posts: 250
1
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider what you know about the differences between print() and println(). You never print out a newline character after printing out your encrypted message.
 
Charles Todd
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Joel -


I've tried all the combinations of print and println, but this sill seems to be doing this - any other suggestions?
 
Joel Christophel
Ranch Hand
Posts: 250
1
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method println(x) (with some String variable x) prints out x and then a newline character. print(x) doesn't prints out just x. In Java, the newline character is '\n'. So print("\n") and println() are equivalent.

In your example, the for loop iterates three times and print(b) prints out E, B, and O. Because you are using the print() method, no newline characters are involved, and thus all the characters remain on the same line. After the loop is done, you go right to println("Please Enter Text to Decrypt: "). We are still on the same line as EBO because we had been using the print() method, so the entire line becomes "EBOPlease Enter Text to Decrypt: \n". Note the newline character at the end of the line that came from the println() method. As you can see, you should also have a '\n' character between the O and the P.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unrelated to your question, but you don't need to import java.lang.* as the compiler does that for you. See this tutorial (about halfway down the page, just above 'Apparent Hierarchies of Packages')
 
Charles Todd
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys for help. I see that \n inserted prior to the next println does indeed get me on a new line, but now the results from the for loop won't return until after that line has been printed. The desired behavior was that I would start a new set of inputs (decrypt this time) and re-use the logic from the for loop. So:

1. Asks for text to encrypt
2. Asks for a shift value (positive or negative integer)
3. Assign these to variables I can manipulate (1 is string / 2 is integer)
4. Perform the for loop in my code
5. Output the results on the screen
6. Ask for another set of inputs - 1st text to decrypt, then another shift value
7 Perform the for loop in my code again
8. Output the next results on my screen.


What I was initially seeing happen was between step 5 and 6 - those lines were running together. Putting the suggested \n in my print statement in the for loop isn't an option because i'll get:

E
B
O

which isn't desirable so I'll have to insert somewhere else later in my code. Thanks everyone for help.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Charles Todd wrote:Putting the suggested \n in my print statement in the for loop isn't an option because i'll get:

E
B
O

which isn't desirable so I'll have to insert somewhere else later in my code. Thanks everyone for help.



I never suggested you put \n in your print statement because you only need one additional '\n' whereas the print() statement gets iterated many times. You are correct in that you'll have to insert it later, but where exactly do you think that would be?

 
Ranch Hand
Posts: 235
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Couple of things. Could we get an example of what your output should like? Although, looking at your code and reading through the conversation between you and Joel, the solution seems fairly straightforward. And secondly, just because I'm anal, here is a link to the Style Guide; specifically item 1.4 and the maximum line length. Commenting is good, but when the comments interfere with being able to read/see the code clearly --- anyway, enough said on that.

Regards,
Robert
 
Marshal
Posts: 80735
485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Sorry I haven't managed to look at this thread properly earlier. I shall try editing your comments to get rid of the line length problem.
Have you been told to write so many comments? Apart from them making the lines too long, as RDS says, some appear to add nothing to the explanation, e.g. that on line 11. That on line 08 is simply incorrect. Comments should explain what is not obvious from reading the code; it is obvious on line 11 that you are setting up a Scanner. On line 08 you are not starting user entry but starting the whole program.
// comments are intende for short things at the end of the line. For comments of any length you should use /* format */; that can be extended over several lines if necessary.

There are all sorts of style things, I am afraid. Declaring several variables in the same line is poor style. The names are not helpful; what is the difference between input1 and input2, for example?
Don't try converting the char values to ints. Remember a char IS NOT a letter, but a number. You can say < 'a' for example.

Only use the \n or \r characters if somebody has told you they want CR or LF, because the result may be operating system specific. Use println or the %n tag. You can use %n in methods like this:-
String text = String.format("%s%n%s%n%s%n%s", "Campbell", "is", "a", "nuisance");

There is, I am afraid, a problem even more serious. You have a method far too long. You should have separate methods to set up the input, to take the input, to encrypt or decrypt, and display the results.
 
Campbell Ritchie
Marshal
Posts: 80735
485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what you r comments ought to have looked like:-
 
Campbell Ritchie
Marshal
Posts: 80735
485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is always unnecessary to import anything in java.lang.
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:This is what you r comments ought to have looked like


Honestly, all these comments, the original ones the OP has and these one you offered, are just a lot of code cruft that my brain desperately tries to ignore and push out of the way so I can read the code.

The problem with the OP's end-of-line comments is that it forces me to scan the line left to right to left to right to left, etc. My brain immediately puts a blinder up to block out the right side of the code listing. The majority of these comments are also redundant and unnecessary.

The problem with the interspersed ones that you offered is that they don't align with the code that they are related to and makes for a jagged edge that my programmer's brain abhors. And the comments that the refactoring part of my brain does spend time to read are immediately marked for dismissal and elimination with extreme prejudice because they are either redundant or do not add any more clarity to what the code could potentially express by itself.

My rules of thumb for making comments:

1. Eliminate most of them by making the code more expressive and self-documenting. The code needs to tell a story and it shouldn't need the help of comments to do that.

2. If you do have to comment:

2a. JavaDocs should be resilient to changes in the implementation. That means they should NOT be explaining how the implementation works but rather what the intent of the code is. Implementation details should be called out like so:

2b. More detailed developer notes are placed at the top of a method and distinctly marked as such. These notes should explain
why you did what you are doing. Ideally, they would reference unit tests that support the decision. Including some idea
of when the comment was made is helpful for other developers, too.

 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Charles, if you just use System.out.print() instead of System.out.println() to display your user input prompts on lines 17 and 19, you should be fine.

Charles Todd wrote:I know it isn't the most 'elegant' but please be kind!


You have to learn to take criticism in stride if you're going to succeed and grow as a programmer. Most criticism is given with good intentions, at least around here at the Ranch, it is. Criticism may not always be given tactfully but in my experience, getting constructive criticism is one of the best ways for you to learn and get better.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic