• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Newbie and my question is probably pretty silly, but still stumped

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, I'm taking Java as a side part of a college degree. Made the mistake of taking it as an online class, so the ability to ask questions is severely limited.

We have to write a Java program to accept a user inputted text string and a user inputted shift amount. The program is then supposed to convert the text to upper case, remove any spaces, convert the text to ASCII values, use the shift amount to move that many number up or down the alphabet and then output the "encrypted" string as letters again. We have to make sure that it loops around to the other end if it reaches the end of the letters. It then repeats the whole process again, with the idea being that you input text which it "encrypts" and then you input its output and a reversed shift value to "decrypt" it again.

Here's the code as I figured it out.



I'm pretty sure that my problem has to do with the array stringArray and it getting passed to the encryptionHelper method, but after 2 days of trying, I haven't been able to spot my mistake.

Can someone tell me where I've gone sideways?

Thanks!
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You haven't exactly said what your problem is. Remember, your job when you post here is to make it as easy as possible for someone to help you. Tell us what exactly you EXPECT to happen, and what actually DID happen.

Generally speaking, I think your java is over-commented. this:

doesn't really add any value. I can see you are calling the method on the next line, where you actually call the method. Comments like this (and the "END XYZ METHOD" - uggghh) generally make your code harder to read.
 
Joseph Burke
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I apologize.

The issue is a compile issue. On compiling, it tells me that this line


gets error: cannot find symbol. This makes all the rest of the math in that helper method fail throwing errors, of course.

The commenting is partially me being so new at this that I have to flowchart, then organize the application by placeholder comments, THEN start coding just to keep my head straight. The rest of it is that my instructor will take off HUGE amounts of points if he feels we haven't commented enough. I've never felt like figuring out where the "enough" point lies. So, I beg your indulgence.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joseph Burke wrote:I apologize.

The issue is a compile issue. On compiling, it tells me that this line
gets error: cannot find symbol.


it actually tells you more than that...it tells you the exact symbol it can't find:


So ask yourself - where do you declare that variable? Inside your convertToArrayHelper method. Java variables have scope. For a variable declared inside a method, the scope is that method, so once you exit it, the variable is gone.

Even if you fix this, you're going to have a lot of other issues. Your convertToArrayHelper method is declared to return an int[], but it doesn't actually return anything.

Honestly, I think your biggest problem is that you are doing too much coding at once. I never - and I mean NEVER - write more than 2-3 lines of code before I compile and test. So let's assume that everything works up to the point where you are going to start writing your convertToArrayHelper method.

My first pass would do NOTHING but declare the method as a public static void, and have it print "I'm in the convertToArrayHelper method!!!", and then call it. If that worked, I'd then change it to have a return value. I'd then create a dummy int[] to return inside the method (note this iteration changed one line, and added only two more - the declaring of the array, and the returning of it.

Hopefully, I have a method that will print the contents of an int array written, so that I can test that what I return really has what I think it has.

The idea is that you build up your code in teeny, tiny pieces. Each time you recompile and test, you know withing 2-3 lines where the problem has to be.
 
Joseph Burke
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd never thought of the few-lines-at-a-time method and neither my instructor nor the book made a suggestion along those lines. I've always just tried to suss the thing out the best I can in its entirety and then start compiling and troubleshooting.

Of course, now that you've said that, it makes me afraid that I'm going to find all kinds of OTHER issues as I do a line by line retest. *sigh* Another Saturday sacrifice to the gods of higher education.

Thank you for the new way of looking at the issue though. I'll start wending my way through it this afternoon.
 
Sheriff
Posts: 17644
300
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
There are useful comments and not-so-useful comments. Your comments are redundant and therefore not very useful. Comments are more useful when they help inform the reader WHY something is being done. The code itself and the names you chose should make WHAT is going on apparent. That is, well-chosen names help make the code self-documenting. As for your problem, check if stringArray is something you declared in some scope that is visible to that line of code. Another thing: "y = y++" can be written simply as "y++"
 
Junilu Lacar
Sheriff
Posts: 17644
300
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
Nature does not produce an oak tree overnight -- it grows it slowly over time. Then there's the Great Barrier Reef, the Grand Canyon, and many more. Take a hint from nature: grow your software incrementally. I am a big fan of the book "Growing Object-Oriented Software, Guided by Tests" maybe you and your professor should check it out sometime.
 
Joseph Burke
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suspect that my declaration of the array being inside a helper method might be causing the issue. I did try to find a way to move the declaration outside of the helper method, but then I ran into a hole in my knowledge. Can I declare the array outside of the helper method but then set the size of the array INSIDE a helper method? If I can, then I could declare the array along with my other variables at the start of the class, and then set the size in the helper method once I'd determined the length of the entered string.

I know I could go old-school on this problem and just do the whole thing without helper methods as one huge block of repetitive code, but I gather that doing so would upset Java coders everywhere and seriously hamper my chances for a good grade.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joseph Burke wrote: . . . one huge block of repetitive code . . .

One of the things that dreadful language C did was free operating system writers from having to write long blocks of code. That is why it was so successful.

You have been given good advice about writing a few lines at a time. And you are going about it the wrong way, starting from the centre and working towards the periphery. Try methods to shift letters. Also, get as much of the code as possible out of a static context. Particularly those fields; it is probably not appropriate for any of your fields to be static (though I am not absolutely certain). Work out how to shift a letter and write a method which might do that. It will look like this:-You will have to work out how to do that, which you can do with pencil and paper. Warning: you will probably need a cast.

If I were doing that, I would put that method into a utility class. There are some very dubious classifications of methods, this being the most dubious of the lot, and that method comes out as 1368. So it is a suitable candidate for being static, despite what I wrote earlier.
Get that working, and forget about arrays until you can shift a single letter.

By the way: is this called a Caesar cipher?

And welcome to the Ranch
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Another thing: "y = y++" can be written simply as "y++"



Um, it should be written y++. Or the equivalent y = y + 1. (edit) Or y += 1.

y = y++ can be written y = y. Or not written at all.
 
reply
    Bookmark Topic Watch Topic
  • New Topic