As the title suggestions, Im trying to convert a string into a unique long by converting each char to ascii.
public class MyClass {
public long AsciiFromString(String inString)
String tempString = "";
for (int i = 0; i < inString.length(); i++) {
char c = inString.charAt(i);
String charAsASCIIString = Integer.toString((int) c);
tempString = tempString + charAsASCIIString;
}
return Long.parseLong(tempString);
}
This method throws a number format error when I pass in a string
This works just fine. But if the computed string is > Long.MAX_VALUE you will get a number format exception. You also need a catch block with your try block.
long v = AsciiFromString("thisisa");
System.out.println(v);
prints
116104105115105115
Your method
public static long AsciiFromString(String inString) {
String tempString = "";
for (int i = 0; i < inString.length(); i++) {
char c = inString.charAt(i);
String charAsASCIIString = Integer.toString((int) c);
tempString = tempString + charAsASCIIString;
}
return Long.parseLong(tempString);
}
Related
I'm fairly new to programming. I'm trying to repeat the word in a given string the amount of times by a given number in the same string. I have decided to loop through the string and add each char to a new string to print out but I'm getting an out of index error.
final String string = "Hello";
final int num = 3;
int number = string.length() * num;
String str = "";
for (int i = 0; i < number; i++) {
str += string.charAt(i);
}
System.out.println(str);
Zero-based index
You are getting the error because the value of i is going beyond the last index available in Hello. The last index in Hello is "Hello".length() - 1 whereas the value of i is going beyond this value because of your loop terminating condition:
i < string.length() * num;
By the way, if you want to repeat Hello 3 times, you should do it as
for(int i = 0; i < num; i ++){
System.out.print(string);
}
Demo:
public class Main {
public static void main(String[] args) {
final String string = "Hello";
final int num = 3;
for (int i = 0; i < num; i++) {
System.out.print(string);
}
}
}
String#repeat
With Java 11+, you can do it without using a loop by using String#repeat:
System.out.println(string.repeat(num));
Demo:
public class Main {
public static void main(String[] args) {
final String string = "Hello";
final int num = 3;
System.out.println(string.repeat(num));
}
}
Just keep it simple
String string = "Hello";
int num = 3;
for (int i = 0; i < num; i++) {
System.out.println(string);
}
If you want to have your result in a new String you can just do this :
String string = "Hello";
int num = 3;
String res = "";
for (int i = 0; i < num; i++) {
res += string;
}
System.out.println(res);
Just adding this in here since you stated you're learning.
Java has a StringBuilder class, super easy to use
And according this this induvial class using StringBuilder is incredibly more efficient than concatenate.
StringBuilder vs String concatenation in toString() in Java
I need to combine an array of strings as below ( so as each character in the result string is a bitwise & of the characters in the input string)
String a = "10110001"
String b = "01101101"
String c = "10101011"
String result = "00100001"
Solution I came up with:
long resultLong = 0;
for( String a : inputs )
{
resultLong = resultLong & Long.parseLong( a ,2);
}
String result = Long.toBinaryString( resultLong );
The number of characters in the input string could be very long, and the above solution wouldn't work (NumberFormatException) . I couldn't get my head around how to implement this, what would be the cleanest way ?
If Long is not enough for your use case then you can use BigInteger
BigInteger(String val, int radix);
Which takes a String and radix as the arguments.
BigInteger result = new BigInteger(inputs[0], 2);
for (int i = 1; i < inputs.length; i++) {
result = result.and(new BigInteger(inputs[i], 2));
}
String resultStr = result.toString(2);
Here's your algorithm. This will work for any number of Strings provided that all the Strings are of same length:
public static void main(String[] args) {
String a = "10110001";
String b = "01101101";
String c = "10101011";
String arr[] = new String[]{a, b, c};
String finalString = "";
for (int i = 0; i < arr[0].length(); i++) {
int temp = Integer.parseInt("" + arr[0].charAt(i));
for (int j = 1; j < arr.length; j++) {
temp = temp & Integer.parseInt("" + arr[j].charAt(i));
}
finalString += temp;
}
System.out.println(finalString);
}
O/P
00100001
i hope someone can help me, i have the following problem.
I have a variable that looks like this:
var a = "01VENT000KRV010WFEVVV055";
I would like to either:
have the last 3 figures of the variable (e.g. 055) as an int
or remove ALL non-figures out of the variable (e.g. 01000010055) as an int
My idea was that:
int sub = Integer.parseInt(a.substring(a.length-3));
or:
int sub = Integer.parseInt(a.replaceAll("[\\D]", ""));
That didnt work, so i would really appreciate if someone could help me here
Thanks
Note all methods can potentially return an empty string.
public static void main(String[] args) {
// TODO code application logic here
String a = "01VENT000KRV010WFEVVV055";
System.out.println(removeChars(a));
System.out.println(removeDigits(a));
System.out.println(getLastThreeChars(a));
}
//This method removes Characters from a string and returns a String of numbers
static String removeChars(String t)
{
String tempString = "";
for(int i = 0; i < t.length(); i++)
{
if(Character.isDigit(t.charAt(i)))
{
tempString += t.charAt(i);
}
}
return tempString;
}
//This method removes Digits from a string and returns only characters
static String removeDigits(String t)
{
String tempString = "";
for(int i = 0; i < t.length(); i++)
{
if(Character.isAlphabetic(t.charAt(i)))
{
tempString += t.charAt(i);
}
}
return tempString;
}
//This methods prints the last 3 char of a string
static String getLastThreeChars(String t)
{
StringBuilder tempString = new StringBuilder();
for(int i = t.length() - 1; i > t.length() - 1 - 3; i--)
{
tempString.append(t.charAt(i));
}
return tempString.reverse().toString();
}
I want to make a code which has function of changing binary to decimal.
So i made a public long tonum()
and tried to return on Main method.
but there is anything shown on screen. Where is the problem?
Plz give me some hints.
public class Bitmap {
byte[] byteArr; //byte array for saving 0 or 1
char[] charArr; //char array for casting from string to byte array
public static void main(String[] args) throws Exception {
Bitmap test1 = new Bitmap("100110");
test1.tonum();
}
public Bitmap(String val) throws Exception {
byteArr = new byte[val.length()]; //To make the array length of Bitmap should e same as that of string
charArr = val.toCharArray(); //casting from string to char
for(int i = 0; i < charArr.length; i++) {
if (charArr[i] == '0')
byteArr[i] = 0;
else if (charArr[i] == '1')
byteArr[i] = 1;
else throw new Exception("Bitmap are should be sequences of zeros and ones!");
}
}
public long tonum() {
int temp = 0;
String str = "";
String str2 = "";
for (int i = 0; i < this.byteArr.length; i++){
temp = this.byteArr[i];
str = Integer.toString(temp);
str2 = str2 + str;
}
long decimal = (long)Integer.parseInt(str2,10);
System.out.println(str2);
return decimal;
}
}
long decimal = (long)Integer.parseInt(str2,10);
That doesn't change binary to decimal. It changes decimal to binary. And you don't have decimal in the first place, you have a string or presentation of binary. Try a radix of 2. But why you have both a char array and a byte array when all you do is deconstruct and reconstruct the original String is anybody's guess.
I'm stuck at the very end of my problem getting a NumberFormatException.
I want to take a string such as Wsup, and turn it into its ASCII values with the result, '87115117112'.
I've gotten that string of numbers built successfully a couple different ways, but when I try to parseInt(string) on it, I get my exception. I've tried printing out the string as an array of characters to look for hidden white space, I've used String.trim() with no luck, I'm quite confused why it isn't recognized as a valid integer.
public static int toAscii(String s){
StringBuilder sb = new StringBuilder();
String ascString = null;
long asciiInt;
for (int i = 0; i < s.length(); i++){
sb.append((int)s.charAt(i));
char c = s.charAt(i);
}
ascString = sb.toString();
asciiInt = Integer.parseInt(ascString); // Exception here
return 0;// asciiInt; 0 in place just to run
}
Thanks for any help given.
Yor asciiInt is long type so do it in this way
asciiInt = Long.parseLong(ascString);
here is your full function
public static long toAscii(String s){
StringBuilder sb = new StringBuilder();
String ascString = null;
long asciiInt;
for (int i = 0; i < s.length(); i++){
sb.append((int)s.charAt(i));
char c = s.charAt(i);
}
ascString = sb.toString();
asciiInt = Long.parseLong(ascString);
return asciiInt;
}
You need to return as long not as integer I have implement your toAscii() with little bit
public static long toAscii(String s){
StringBuilder sb = new StringBuilder();
long asciiInt;
for (int i = 0; i < s.length(); i++){
char c = s.charAt(i);
asciiInt = (int)c;
System.out.println(c +"="+asciiInt);
sb.append(asciiInt);
}
return Long.parseLong(sb.toString());
}