MyRanger20 MyRanger20 - 9 months ago 40
Java Question

how to call a method within a method while calling one method in the main

In my assignment, I have to encrypt a word by shifting its characters by on the ASCII table and then print what I have done in reverse.

I have created 2 methods one called encrypt and the other called reverse that do just that however, I cant get the proper output, but I'm sure my methods are 100% working (the program should also work with sentences as in spaces should not be encrypted)

Sample run of what I should have:

=> java Encrypt

Please enter a sentence or word: Hello World

pmmfI emspX

and conditions i should follow to recall the methods:

  1. If the user supplies more than one word, they should be treated as one string

  2. main() calls encrypt(), it does not call reverse()

  3. encrypt() calls reverse() before returning the string

  4. The printing of the string happens in main()

my code:

import java.util.*;
public class Encrypt {

public static void main(String[] args) {
Scanner console = new Scanner(;
System.out.println("Please enter a sentence or a word: ");

public static String reverse (String text){

for( int i = text.length()-1 ; i >= 0; i--){


return text;
public static String encrypt (String text){

for( int i =0 ; i < text.length(); i++){

char X = text.charAt(i);
int ascii = (int)X;
if(ascii == 32){
System.out.print(" ");
else {
System.out.print((char) (ascii+1) );

return text;

I didn't know what to do in the main since I don't know how to get that output while only calling 1 of the methods which is encrypt

hopefully I wasn't very confusing

Answer Source

Instead of calling System.out.println() you should add the chars to the String returned:

public static String reverse (String text){
        String ret = "";
        for( int i = text.length()-1 ; i >= 0; i--){


        return ret;

Do the same with the other method, and then, in your main, call