• 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

read int and encrypt it

 
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic