aspose file tools*
The moose likes Beginning Java and the fly likes Newbie and my question is probably pretty silly, but still stumped Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Newbie and my question is probably pretty silly, but still stumped" Watch "Newbie and my question is probably pretty silly, but still stumped" New topic
Author

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

Joseph Burke
Greenhorn

Joined: Feb 01, 2013
Posts: 4
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!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10925
    
  12

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joseph Burke
Greenhorn

Joined: Feb 01, 2013
Posts: 4
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
Bartender

Joined: Oct 02, 2003
Posts: 10925
    
  12

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

Joined: Feb 01, 2013
Posts: 4
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.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

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 - [How to Ask Questions] [How to Answer Questions]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

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

Joined: Feb 01, 2013
Posts: 4
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
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
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Newbie and my question is probably pretty silly, but still stumped
 
Similar Threads
String and StringBuffer
Reordering Alphabets
Replace a char in a String
AES Encrypter/Decrypter
How to print ASCII values of all characters?