Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi , I was wondering if you can please help me with my java code. I\'m trying to

ID: 3815776 • Letter: H

Question

Hi , I was wondering if you can please help me with my java code. I'm trying to make a Caesar Cipher , and run it from my cmd (windows), but it's acting weird. Everytime I run it and i try to copy and paste a text , it ciphers everything ( before i ENTER). Also, it get's stuck in the while loop, so te program never finishes.

Here is my code

import java.util.Scanner;
public class CaesarShift {
   public static void main(String [] args){

       if (args.length ==0) return;
       int shift = Integer.parseInt(args[0]);
       String Text="";
       char c = ' ';
       Scanner textIn= new Scanner(System.in);
       System.out.println();
       while (textIn.hasNext())
       {
           Text =textIn.nextLine();
           for (int i = 0 ; i < Text.length(); i++)
               {
                   c=Text.charAt(i);
                   if(Character.isLetter(c)){
                       if (Character.isLowerCase(c)){
                           c=Character.toLowerCase(c);
                       }else c=Character.toLowerCase(c);
                   }
                   if(c>='a' &&c<='z'){
                       c=(char)(c+shift);
                       if(c>'z'){
                           c=(char)(c-26);
                       }else if(c<'a'){
                           c=(char)(c+26);
                       }
                   }
                       System.out.printf("%c",c);
               }
               System.out.println();
           }
       }
   }

Explanation / Answer

HI, I have fixed the issue.

To stop just press enter with no input.

Please let me know in case of any issue.

import java.io.BufferedInputStream;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class CaesarShift {

   public static void main(String [] args) throws IOException{

       if (args.length ==0) return;

       int shift = Integer.parseInt(args[0]);

       String Text="";

       char c = ' ';

       //Scanner textIn= new Scanner(System.in);

       BufferedInputStream bf = new BufferedInputStream(System.in);

       BufferedReader reader = new BufferedReader(new InputStreamReader(bf));

      

       System.out.println();

       while (! (Text = reader.readLine()).trim().equals(""))

       {

           //Text =textIn.nextLine();

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

           {

               c=Text.charAt(i);

               if(Character.isLetter(c)){

                   if (Character.isLowerCase(c)){

                       c=Character.toLowerCase(c);

                   }else c=Character.toLowerCase(c);

               }

               if(c>='a' &&c<='z'){

                   c=(char)(c+shift);

                   if(c>'z'){

                       c=(char)(c-26);

                   }else if(c<'a'){

                       c=(char)(c+26);

                   }

               }

               System.out.printf("%c",c);

           }

           System.out.println();

       }

   }

}

/*

Sample run:

pravesh kuamr

tveziwl oyeqv

THis is my firt line

xlmw mw qc jmvx pmri

*/