How do I convert number to a letter in Java? [duplicate] - java

This question already has answers here:
Switch statement with string wrong output
(2 answers)
Closed 2 years ago.
I want to make a program to convert number to letter, from 0-9 to ABCDEFGHIK.
For example:
with n = 10 the output would be BA as 0 is A is 0 and B is 1.
Here is my code:
String convertNumber(long n) {
String result="";
String strN = Long.toString(n);
for (int i=0; i < strN.length();i++){
char ch = strN.charAt(i);
switch(ch){
case '0':
result = "A";
case '1':
result = "B";
case '2':
result = "C";
case '3':
result = "D";
case '4':
result = "E";
case '5':
result = "F";
case '6':
result = "G";
case '7':
result = "H";
case '8':
result = "I";
case '9':
result = "K";
}
}
return result;
}
However, the results returns only K. Where did I do wrong? Thank you!

There are three mistakes in your program:
Not using break with the case and therefore every case will fall to the last case.
Using = instead of +=
Using the loop in reverse order than the required order. It should be for (int i = strN.length() - 1; i >= 0; i--) instead of for (int i=0; i < strN.length();i++)
Given below is your corrected program:
public class Main {
public static void main(String[] args) {
System.out.println(convertNumber(10));
}
static String convertNumber(long n) {
String result = "";
String strN = Long.toString(n);
for (int i = strN.length() - 1; i >= 0; i--) {
char ch = strN.charAt(i);
switch (ch) {
case '0':
result += "A";
break;
case '1':
result += "B";
break;
case '2':
result += "C";
break;
case '3':
result += "D";
break;
case '4':
result += "E";
break;
case '5':
result += "F";
break;
case '6':
result += "G";
break;
case '7':
result += "H";
break;
case '8':
result = "I";
break;
case '9':
result = "K";
}
}
return result;
}
}
Output:
AB

You can use this one:
static String convertNumber(int n) {
int reminder;
char[] arr = "ABCDEFGHIK".toCharArray();
int len = arr.length;
StringBuilder builder = new StringBuilder();
while (n != 0) {
reminder = (int) n % 10;
n /= 10;
builder.append(arr[(reminder % len)]);
}
return builder.toString();
}
, main
static public void main(String[] args) {
System.out.println(convertNumber(65));
System.out.println(convertNumber(78));
System.out.println(convertNumber(99));
System.out.println(convertNumber(901));
}
, output
FG
IH
KK
BAK

You forgot the break. add break; in every case, like this:
case '0':
result = "A";
break;
case '1':
result = "B";
break;

Related

how to make a jbutton thats check if the number hexidecimal, binary,octal, decimal?

Im very new to java, newbee in short. I want to make a program that checks if the number is hexidecimal, binary, octal, decimal. And if its Hexdecimal/binary/octal convert it to decimal but if it is a decimal not convert it. in my case i made a jbutton that if its clicked it will first check the number then convert it to decimal if it is not a decimal. Now the problem is i don't know what to add to my code, at present i can only check if it is a hexidecimal and then convert it to decimal if it meets the requirements and if not it will just stay. here is my code.
private void del1ActionPerformed(java.awt.event.ActionEvent
evt) {
String hex=prime1.getText();
long dec=0;
int r=0,c=0,b,con,er;
con=(int)Math.round(inb);
String co2, tra=prime1.getText();
boolean valid;
if(valid=true){for (int i = 0; i < hex.length(); i++ ) {
int digit = hexValue( hex.charAt(i) );
if (digit == -1) {
return;
}dec = 16*dec + digit;
prime1.setText(String.valueOf(dec));
}
static int hexValue(char ch) {
switch (ch) {
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
case 'a': // Note: Handle both upper and lower case letters.
case 'A':
return 10;
case 'b':
case 'B':
return 11;
case 'c':
case 'C':
return 12;
case 'd':
case 'D':
return 13;
case 'e':
case 'E':
return 14;
case 'f':
case 'F':
return 15;
default:
return -1;
}
} // end hexValue
here prime1 is a jtextfield.
If I got you correctly, this should be what you are looking for:
int getBaseAndConvertToDecimal(String input) {
int output = -1;
for(int i=0; i<input.length(); i++) {
if(Character.isAlphabetic(input.charAt(i)) && Character.toUpperCase(input.charAt(i))<='F') {
output = 16;
}
else {
if(Character.isDigit(input.charAt(i)) && Integer.parseInt(String.valueOf(input.charAt(i)))>=8) {
output = 10;
}
else {
if(Character.isDigit(input.charAt(i)) && Integer.parseInt(String.valueOf(input.charAt(i)))>=2) {
output = 8;
}
else {
if(input.charAt(i)=='0' || input.charAt(i)=='1') {
output = 2;
}
else {
output = -1;
}
}
}
}
}
if(output != -1) {
//int this case, the output object represents the base of the input string
//Integer.parseInt(inputToConvert, destinationBase);
return Integer.parseInt(input, output);
}
return output;
}

How can I convert a hexadecimal number into binary without using parse or automatic conversion

Below I have a method named 'hextoBinary' that returns a hexadecimal to binary conversion through type void.
In order for me to continue with my program I need a conversion from hex to binary method that returns and int so I can convert that binary int into a decimal with my 'hextoDecimal' method.
Can anybody help me or guide me on what approach to take, i've been stuck on this for a while now. i am limited to doing this manually instead of using parse or java automatic conversions.
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
public class Main
{
static void hexToBinary(char hexdec[])
{
for (char c: hexdec)
{
switch (c)
{
case '0':
System.out.print("0000");
break;
case '1':
System.out.print("0001");
break;
case '2':
System.out.print("0010");
break;
case '3':
System.out.print("0011");
break;
case '4':
System.out.print("0100");
break;
case '5':
System.out.print("0101");
break;
case '6':
System.out.print("0110");
break;
case '7':
System.out.print("0111");
break;
case '8':
System.out.print("1000");
break;
case '9':
System.out.print("1001");
break;
case 'A':
System.out.print("1010");
break;
case 'B':
System.out.print("1011");
break;
case 'C':
System.out.print("1100");
break;
case 'D':
System.out.print("1101");
break;
case 'E':
System.out.print("1110");
break;
case 'F':
System.out.print("1111");
break;
default:
System.out.print("\nInvalid hexadecimal digit " + hexdec[c]);
}
}
}
public static int hextoDecimal(int n)
{
int decimal = 0, p = 0;
while(n != 0)
{
decimal += ((n % 10) * Math.pow(2,p));
n = n / 10;
p++;
}
return decimal;
}
public static void main(String[] args) throws IOException
{
Scanner sc = new Scanner(new File("RAMerrors8x4c"));
ArrayList<String> hexValues = new ArrayList<>();
while(sc.hasNext())
{
hexValues.add(sc.nextLine());
}
hexToBinary(hexValues.get(0).toCharArray());
}
}
This code is based on some that came from here but that link no longer seems to be active. Anyway, from a hex string you can get an int like this:
int hexToDecimal(String s){
int result = 0;
int digit = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c >= '0' && c <= '9')
digit = c - '0';
else
if (c >= 'A' && c <= 'F')
digit = 10 + c - 'A';
else
inputError(s);
result = 16 * result + digit;
}
return result
}
I modified your code a little.
a. In your code only the first hex was printed.
Change:
call hexToBinary for every hex String.
b. the binary value was discarded after printing, so it couldn't be reused.
Change:
Changed returntype of hexToBinary from void to String and returned the binary value calculated.
To be able to return a String I add the peaces(nibbles) of the hex/binary to a String in every switch(case) clause.(a Stringbuilder might be better than a String - you can additionally improve that)
in the main: additionally collect all the returned binary values in a arraylist called "binaryValues" in order to have them for the next step.
With the above (little) changes I now have all the binary values that had already been calculated.
So I am able to simply use them in a binaryToDecimal method which just sums up the binary values weighted by their position.
Why not do it again? Because youd need to convert the A-F to numbers what your hexToBinary already did. So storing the values saves you doing that step again. I have a feeling that is what your teacher had in mind when he/she combined the tasks like this.
The resulting code is:
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
public class Main
{
static String hexToBinary(char hexdec[]) {
String hex = "";
for (char c : hexdec) {
switch (c) {
case '0':
System.out.print("0000");
hex += "0000";
break;
case '1':
System.out.print("0001");
hex += "0001";
break;
case '2':
System.out.print("0010");
hex += "0010";
break;
case '3':
System.out.print("0011");
hex += "0011";
break;
case '4':
System.out.print("0100");
hex += "0100";
break;
case '5':
System.out.print("0101");
hex += "0101";
break;
case '6':
System.out.print("0110");
hex += "0110";
break;
case '7':
System.out.print("0111");
hex += "0111";
break;
case '8':
System.out.print("1000");
hex += "1000";
break;
case '9':
System.out.print("1001");
hex += "1001";
break;
case 'A':
System.out.print("1010");
hex += "1110";
break;
case 'B':
System.out.print("1011");
hex += "1111";
break;
case 'C':
System.out.print("1100");
hex += "1100";
break;
case 'D':
System.out.print("1101");
hex += "1110";
break;
case 'E':
System.out.print("1110");
hex += "1110";
break;
case 'F':
hex += "1111";
System.out.print("1111");
break;
default:
System.out.print("\nInvalid hexadecimal digit " + hexdec[c]);
}
}
System.out.println();
return hex;
}
public static int binaryToDecimal(String binary) {
int decimal = 0;
for (int i = 1; i < binary.length()-1; i++) {
decimal += Math.pow(2, i-1) * (binary.charAt(binary.length()-i) - '0');
}
return decimal;
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(new File("RAMerrors8x4c"));
ArrayList<String> hexValues = new ArrayList<>();
ArrayList<String> binaryValues = new ArrayList<>();
while (sc.hasNext()) {
hexValues.add(sc.nextLine());
}
for (String hex : hexValues) {
String binary = hexToBinary(hex.toCharArray());
binaryValues.add(binary);
System.out.println(binary);
}
for (String binary : binaryValues) {
int decimal = binaryToDecimal(binary);
System.out.println(decimal);
}
}
}
}
Besides using a Stringbuilder another idea could be to do all the printing of the binary values in the main. The hexToBinary returns the String - so you can print it in the loop - if you want.

Ways to reverse print a string?

I'm trying to code a program that would convert an int to binaries. So far I have it print out the remainders, but it has to be printed in reverse for it to be a proper binary. I am not allowed to use any methods.
Here's my code.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter A Postive Number:");
int i = sc.nextInt();
int j = i; //backup
int k = 0; //remainder
while (j != 0) {
k = j % 2;
j /= 2;
String reversedStr = "";
switch (k) {
case 0:
reversedStr += "0";
break;
case 1:
reversedStr += "1";
break;
case 2:
reversedStr += "2";
break;
case 3:
reversedStr += "3";
break;
case 4:
reversedStr += "4";
break;
case 5:
reversedStr += "5";
break;
case 6:
reversedStr += "6";
break;
case 7:
reversedStr += "7";
break;
case 8:
reversedStr += "8";
break;
case 9:
reversedStr += "9";
break;
case 10:
reversedStr += "A";
break;
case 11:
reversedStr += "B";
break;
case 12:
reversedStr += "C";
break;
case 13:
reversedStr += "D";
break;
case 14:
reversedStr += "E";
break;
case 15:
reversedStr += "F";
break;
}
for (int l = reversedStr.length() - 1; l >= 0; l--) {
reversedStr.charAt(i);
System.out.print(reversedStr.charAt(l));
}
}
System.out.println("done");
}
}
this code gives me
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 178
at java.lang.String.charAt(String.java:658)
at radixHandmade.Handmade.main(Handmade.java:73)
The for loop at the bottom is the reversing part, but im not sure how to use charAt to complete the code. To be honest im very confused.
Any help would be greatly appreciated.
This statement causes the problem:
reversedStr.charAt(i);
Aside from causing an exception (because you're trying to get the (originally entered value)-th character in the string, which is by outside the bounds of the string, unless that value happens to be 0), it has no other side effect.
Just remove this line.
import java.util.Scanner;
public class Reverse {
public static void main(String[] args){
userInput();
}
public static void userInput(){
Scanner console = new Scanner(System.in);
String reverseString = console.nextLine(); //This is the word to be written backwards.
int s = reverseString.length()-1; //length of the word.
for (int i = s ; i>=0; i--){
System.out.print(reverseString.charAt(i));
}
}
}
Hope this helps.
Good luck.
removing the line reversedStr.charAt(i) will resolve the issue, but the program will still not convert to binary correctly. Your string still needs to be reversed. Might I suggest using the StringBuilder class:
import java.util.Scanner;
import java.lang.StringBuilder;
public class App {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StringBuilder out = new StringBuilder();
System.out.println("Enter A Postive Number:");
int i = sc.nextInt();
int r = 0;
int b = 2; // now you can just change this for what base (binary, hex, ...)
while (i != 0) {
r = i % b;
i /= b;
switch (r) {
case 0:
out.append("0");
break;
case 1:
out.append("1");
break;
}
}
System.out.println(out.reverse());
}
}
Your code would work fine if you move the reversing for loop out of the while loop:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String revStr = "";
System.out.println("Enter A Postive Number:");
int i = sc.nextInt();
int r = 0;
int b = 2; // now you can just change this for what base (binary, hex, ...)
while (i != 0) {
r = i % b;
i /= b;
switch (r) {
case 0:
revStr += "0";
break;
case 1:
revStr += "1";
break;
// ... for hex
}
}
for (int j = revStr.length() - 1; j >= 0; j--) {
System.out.print(revStr.charAt(j));
}
System.out.print("\n");
}
But what I really don't understand is why you even have to reverse the string if you can just do:
reversedStr = "0" + reversedStr; // of course it will no longer be reversed
// ... for all the cases
Even better if you stick with just binary, you could skip the whole switch and do
public static void main(String[] args) {
String str = "";
System.out.println("Enter A Postive Number:");
for(int i = new Scanner(System.in).nextInt(); i != 0; i /= 2)
str = String.valueOf(i % 2) + str;
System.out.println(str);
}

Java: Lighter code for encryption

I'm trying to get a program to encrypt a message using Cesar's Cypher, which involves replacing every character of a string with the letter, whose code is the code of the replaced letter plus 3. For e.g., if the letter is A, then it has to be replaced with D, because the code of D is the code of A plus 3. The letters are case-sensitive. The code I thought of uses a really heavy switch construct, I was wondering, if you could help me to make it more straight forward.
Here's the code I use for the encryption method:
public class Util
{
public static String Encript(String stringa)
{
char[] stringaArray=stringa.toCharArray();
for (int i =0; i<stringaArray.length; i++)
{
switch (stringaArray[i])
{
case 'a':
stringaArray[i]=('D');
break;
case 'b':
stringaArray[i]='E';
break;
case 'c':
stringaArray[i]='F';
case 'd':
stringaArray[i]='G';
break;
case 'e':
stringaArray[i]='H';
break;
case 'f':
stringaArray[i]='I';
break;
case 'g':
stringaArray[i]='J';
break;
case 'h':
stringaArray[i]='K';
break;
case 'i':
stringaArray[i]='L';
break;
case 'j':
stringaArray[i]='M';
break;
case 'k':
stringaArray[i]='N';
break;
case 'l':
stringaArray[i]='O';
break;
case 'm':
stringaArray[i]='P';
break;
case 'n':
stringaArray[i]='Q';
break;
case 'o':
stringaArray[i]='R';
break;
case 'p':
stringaArray[i]='S';
break;
case 'q':
stringaArray[i]='T';
break;
case 'r':
stringaArray[i]='U';
break;
case 's':
stringaArray[i]='V';
break;
case 't':
stringaArray[i]='W';
break;
case 'u':
stringaArray[i]='X';
break;
case 'v':
stringaArray[i]='Y';
break;
case 'w':
stringaArray[i]='Z';
break;
case 'x':
stringaArray[i]='A';
break;
case 'y':
stringaArray[i]='B';
break;
case 'z':
stringaArray[i]='C';
break;
}
}
String encripted= new String(stringaArray);
return encripted;
}
}
Then I use this method in the graphical interface class so that it acts when a button is pressed like this:
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
String stringa=messageTxt.getText();
encriptedTxt.setText(Util.Encript(stringa, encriptedTxt));
}
Here is an example of test-case:
Test case:
aBxyE //Input
dEabH //Output
Thank you in advance!
Khoor/#Zruog
You could iterate the characters in the String, and I would also pass in the key offset. Create a StringBuilder and append each character after performing integer addition and casting. Something like,
public static String encrypt(String msg, int key) {
StringBuilder sb = new StringBuilder();
for (char ch : msg.toCharArray()) {
char c = (char) (ch + key);
sb.append(c);
}
return sb.toString();
}
Decryption is trivial with a Caesar cipher; you can call encrypt with the negative value of the key
public static String decrypt(String msg, int key) {
return encrypt(msg, -key);
}
And I tested my example with
public static void main(String[] args) {
String msg = encrypt("Hello, World", 3);
System.out.println(msg);
System.out.println(decrypt(msg, 3));
}
Finally, as others have noted, the Caesar cipher is terribly insecure (because letter frequency is not perturbed, it's trivial in a modern sense).
This code will sawp a char with another one three chars apart, as defined in ALPHABET :
public class Util
{
private final static String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
public static String encript(String stringa)
{
char[] stringaArray = stringa.toCharArray();
for (int i =0; i<stringaArray.length; i++) {
char c = stringaArray[i];
int index = ALPHABET.indexOf(c);
if(index <0)
{
continue ; //if c does not appear in ALPHABET
}
// for example c is *, leave it unchanged
if((index +3) >= ALPHABET.length() ) {
index = index - ALPHABET.length();
}
stringaArray[i] = ALPHABET.charAt(index+3);
}
String encripted= new String(stringaArray);
return encripted;
}
}
If it is not clear, do not hesitate to ask.
Here is my Short and Efficient code for your program:
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//N is length of string
int N = Integer.parseInt(br.readLine());
//Reading string Input
String str = br.readLine();
//K is the key to rotate characters in the string
//In your case K = 3
int K = Integer.parseInt(br.readLine());
K %= 26; //Incase K is greater than 25
//Main [tag:algorithm]
for(int i = 0; i < N; i++){
char c = str.charAt(i);
if(c >= 65 && c <= 90){
c += K;
if(c > 90){
c = (char)(c - 90 + 64);
}
}
if(c >= 97 && c <= 122){
c += K;
if(c > 122){
c = (char)(c - 122 + 96);
}
}
System.out.print(c);
}
}
}
I am doing k = k % 26 because if the k = 26 then it will print the same letter, if k = 27 the character will rotate only 1 time and so on.

could use some help on this decode(char c) method

The program is to write a calss PhoneNumber.java
I understand that I am supposed to test if the string is a digit or a letter and then if it is a letter its supposed to be decoded by decode(char c);
However, I dont think char c should be in between the ( ) If any one has suggestions thatd be great thanks!! The toString is left unreturned intentionally because i have not gotten that far in the program yet. Also, have to keep it in the case 'A' format Thanks
public class PhoneNumber {
private int areacode;
private int number;
private int ext;
PhoneNumber() {
areacode = 0;
number = 0;
ext = 0;
}
PhoneNumber(int newnumber) {
areacode = 216;
number = newnumber;
ext = 0;
}
PhoneNumber(int newarea, int newnumber, int newext) {
areacode = newarea;
number = newnumber;
ext = newext;
}
PhoneNumber(String newnumber) {
String areacode = str[0];
String number = str[1];
String[] str = newnumber.split("-");
String[] number = newnumber;
boolean b1, b2;
int i = 0;
int place = 0;
for (int x: newnumber){
newnumber.charAt[i] = place;
b1 = Character.isDigit(place);
if (b1 == true){
number = place;
i++;
} else {
b2 = Character.isLetter(place);
} if (b2 == true) {
number = decode(place);
i++;
} else {
System.out.print("invalid phone number!");
}
}
System.out.print(areacode.concat(number));
return newnumber;
}
private String decode(place) {
switch (c) {
case 'A': case 'B': case 'C': return "2";
case 'D': case 'E': case 'F': return "3";
case 'G': case 'H': case 'I': return "4";
case 'J': case 'K': case 'L': return "5";
case 'M': case 'N': case 'O': return "6";
case 'P': case 'Q': case 'R': case 'S': return "7";
case 'T': case 'U': case 'V': return "8";
case 'W': case 'X': case 'Y': case 'z': return "9";
default: return "";
}
}
public boolean equals(PhoneNumber pn) {
}
public String toString() {
}
}
G:\CIS260\Assignments>javac PhoneNumber.java
PhoneNumber.java:53: error: <identifier> expected
private String decode(place) {
^
1 error
In the constructor, you need to declare the array before you put things in it. You also can't say String[] number = newnumber because number is a String[] and newnumber is a String. equals() and toString() need to return something. And, to answer your question, just say
private String decode(char c){

Categories

Resources