Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8726339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:13:29+00:00 2026-06-13T08:13:29+00:00

In this little project for school, I am doing a Caesar cipher. What will

  • 0

In this little project for school, I am doing a Caesar cipher. What will be done is that the user will punch in a word, and it will be converted to an character array, then into its respective ascii numbers. Then this equation will be performed upon each number:

new_code = (Ascii_Code + shift[A number that the user picks out]) % 26

So far, here is the code I’ve written out:

import javax.swing.*;
import java.text.*;
import java.util.*;
import java.lang.*;

public class Encrypt {


public static void main(String[] args) {

String phrase = JOptionPane.showInputDialog(null, "Enter phrase to be messed with ");
String shift =  JOptionPane.showInputDialog(null, "How many spots should the characters be shifted by?");
int shiftNum = Integer.parseInt(shift);  //converts the shift string into an integer
char[] charArray = phrase.toCharArray(); // array to store the characters from the string
int[] asciiArray = new int[charArray.length]; //array to store the ascii codes

//for loop that converts the charArray into an integer array
for (int count = 0; count < charArray.length; count++) {

asciiArray[count] = charArray[count];

System.out.println(asciiArray[count]);

} //end of For Loop

//loop that performs the encryption
for (int count = 0; count < asciiArray.length; count++) {

    asciiArray[count] = (asciiArray[count]+ shiftNum) % 26;

} // end of for loop

//loop that converts the int array back into a character array
for (int count = 0; count < asciiArray.length; count++) {

    charArray[count] = asciiArray[count]; //error is right here =(

}




}//end of main function




}// end of Encrypt class

It is mentioning a “possible loss of precision” in the last for loop. Is there something else I’m supposed to do? Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:13:30+00:00Added an answer on June 13, 2026 at 8:13 am

    For A a; B b;, the assignment a = (A) b loses precision when ((B) ((A) b)) != b. In other words, casting to the destination type and back gives a different value. For example (float) ((int) 1.5f) != 1.5f so casting a float to an int loses precision because the .5 is lost.

    chars are 16-bit unsigned integers in Java, while ints are 32-bit signed 2-s complement. You can’t fit all 32-bit values into 16-bits, so the compiler warns about loss of precision due to the 16 bits that would be lost by an implicit cast that just shoehorns the 16 least-significant bits from the int into the char losing the 16 most-significant bits.

    Consider

    int i = 0x10000;
    char c = (char) i;  // equivalent to c = (char) (i & 0xffff)
    System.out.println(c);
    

    you have an integer that can only fit in 17-bits, and so c is (char) 0.

    To fix, add an explicit cast to char if you believe that this will not happen because of the logic of your program: asciiArray[count] → ((char) asciiArray[count]).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I finished my little project that does some heavy lifting. i realized in this
I've created this little quiz for a school project using Java and MySQL. Now
I've been trying to get this little project I'm doing finished, but for some
Im stuck with this little project in C# but basically my problem is this:
I'm making this little project with fwrite (long story, and I can't use DB's).
I realize this sounds a little crazy, but I'm working on a project for
found this little code snippet that seems to do what i want, but im
Goodmorning everybody, I'm having a little problem with a project for school. We are
Possible Duplicate: Ruby.Metaprogramming. class_eval I have this little project, the goal is to create
I'm developing a fun little project using Foursquare API. In this project, I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.