File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes read int and encrypt it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "read int and encrypt it" Watch "read int and encrypt it" New topic
Author

read int and encrypt it

Jason Attin
Greenhorn

Joined: Feb 02, 2013
Posts: 22
hi chaps,I am very new to java and I am reading the deitel and deitel book "java how to program" I would like to do this exercise taken from it, chapter 4 exercise 4.38, in brief: "Write an application that reads a 4 digit int and encrypt it replacing each digit with the result of addin 7 to it and getting the reminder after dividing the new value by 10. Then swap the first digit with the 3rd and swap the second with the fourt, then print the encrypted digit".
Now bearing in mind that I am only allowed to use a while statement (no for loops, no array, no switch and assuming the user enters numbers and not letters) because I haven't done them as yet, this is what I came up with so far, but I got somewhat stuck:



and


So basically I got to the point of reading the int in and encrypt it half way. What I am not entirely sure how to do, is to swap the digits according to the exercise. This becasue as you can see I am encrypting the int digit one by one. the function



returns the number to


but ideally I should return the number to

public void insertNumber(){
in order to be able to swap the digits (that by now are part of a string). However I am not able to return //return fullNumber; even if I make the due changes because the compiler tells me that the variable hasn't been initialize. ANyway, has anybody got any suggestion please?
thanks
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

You shouldn't start programming and then determine what a method has to do when you hit a problem. First think out what the responsibilities of each method are, and then simply let the method perform the few things it needs to do in order to fulfill those responsibilities.

You need three methods: The main method, a method that reads a 4 digit number from the console, and a method that encrypts a 4 digit number. You can make the work easier by adding another method that transforms the number to an int array.
Jason Attin
Greenhorn

Joined: Feb 02, 2013
Posts: 22
thanks, problem is I haven't got to arrays as yet, I have only done if statements, switch and while loops..., that's why I was trying with a while loop. The thing is I got a little confused when I wrote the methods I suppose...DO you think I should rewrite this in a different way?
thanks
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

I don't think there's much point in forcing yourself to do it without arrays and for-statements. I wouldn't hold yourself back just because you're not supposed to know about them yet. Focus on making nice simple solutions to the problems.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
And welcome to the Ranch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: read int and encrypt it