How do I convert character to a string - java

When I run my code, the error I get says "incompatible types: char cannot be converted to a string"
public class Credit {
public static void main(String[] args) {
long numberLong = Comp122.getLong("Number: ");
String number = numberLong + "";
System.out.println(Integer.parseInt(number.charAt(0)) * 2 + Integer.parseInt(number.charAt(1)) * 2);
System.out.println("VISA");
}
}

char is actually int that holds ASCII number of the given character. So to transform e.g. character 5 to int value, you have to find difference between (char)5 and (char)0.
public class Credit {
public static void main(String[] args) {
long numberLong = 123456789L;
String numberStr = String.valueOf(numberLong);
System.out.println(toInt(numberStr, 0) * 2 + toInt(numberStr, 1) * 2);
System.out.println("VISA");
}
private static int toInt(String numberStr, int index) {
return numberStr.charAt(index) - '0';
}
}

To answer your question title:
Character.toString(char)
This converts a char to a String.
To answer what you are trying to do:
Character.digit(char,int)
Converts a character to the value of the digit it represents in the specified base. In your case you are using base 10.

Related

I wrote this code of counting zero with integer value as 00486 set in a variable but its showing integer value large can anyone find the mistake?

public class linklist {
public static void main(String[] args) {
int a = 00486;
int x=zero(a);
System.out.println(x);
}
public static int zero(int n)
{
if(n<=10)
{
return 0;
}
if(n%10==0) {
return 1 + zero(n / 10);
}
else
return zero(n/10);
}
}
in line 3 i set 00486 as value but its showing integer too large error.As per my knowledge In Java, the integer value permissible is much bigger.
When you add 0 in front of the number, it is treated in the octal format. In octal format, the allowed digits vary from 0 to 7 only .
Therefore, you need to change the number from 00486 to 00476. But be beware, this number will be converted to base 10 format. To verify that, I have written a print statement that shows that the number will be stored indeed in base 10 format.
public class linklist {
public static void main(String[] args) {
int a = 0476;
System.out.println(8*8*4 + 8*7 + 6);
System.out.println(a);
int x=zero(a);
System.out.println(x);
}
public static int zero(double n)
{
if(n<=10)
{
return 0;
}
if(n%10==0) {
return 1 + zero(n / 10);
}
else
return zero(n/10);
}
}

Convert hexadecimal string to double in Java

I want to convert this hex string 48985DDFAF27A0 to double. The result should be 49.1903648.
I tried
Double.longBitsToDouble
Parsing the hex string with parseInt(x, 16) and then using intBitsToFloat
Nothing of the above approaches seems to work.
Need suggestions please
You have to parse the string of hexadecimal number to long. After doing that, you convert to double by using Double.longBitsToDouble. Here is an example of your hex number:
public class Converter {
public static void main(String[] args) {
String hex = "48985DDFAF27A0";
long longHex = parseUnsignedHex(hex);
double d = Double.longBitsToDouble(longHex);
System.out.println(d);
}
public static long parseUnsignedHex(String text) {
if (text.length() == 16) {
return (parseUnsignedHex(text.substring(0, 1)) << 60)
| parseUnsignedHex(text.substring(1));
}
return Long.parseLong(text, 16);
}
}

Conversion of decimal into binary using recursion

I want to convert the decimal number into a binary number using recursion in java. I tried a lot but unable to do it. Here is my code:
public class DecimalToBinary {
public static void main(String[] args) {
System.out.println(conversion(2));
}
public static int conversion(int n) {
return reconversion(n);
}
public static int reconversion(int n) {
if(n <= 0)
return 0;
else {
return (int) (n/2 + conversion(n/2));
}
}
}
Integer values are already in binary. The fact that they appear as digits 0 thru 9 when you print them is because they are converted to a string of decimal digits. So you need to return a String of binary digits like so.
public static String conversion(int n) {
String b = "";
if (n > 1) {
// continue shifting until n == 1
b = conversion(n >> 1);
}
// now concatenate the return values based on the logical AND
b += (n & 1);
return b;
}

"string cannot be converted to integer"

In my QuestionnaireUI.java class
private void btnProceedActionPerformed(java.awt.event.ActionEvent evt) {
tblResults.setRowSelectionAllowed(true);
int temp = tblResults.getSelectedRow();
Global.manu = tblResults.getValueAt(temp, 1).toString();
Global.mod = tblResults.getValueAt(temp, 2).toString();
Global.price = "R" + (Integer)tblResults.getValueAt(temp,3).toString(); //line of code that gives me the error message
this.dispose();
new PaymentUI().setVisible(true);
}
In my Global.java class that I use for all my global variables
public class Global {
public static int rowSelect;
public static String manu;
public static String mod;
public static int price;
public static int financeprice;
public static int rate = 9;
}
you are Adding R (a String) to (Integer)tblResults.getValueAt(temp,3).toString()
There are a few issues here:
tblResults.getValueAt(temp,3).toString() returns a String, which is not an integer so can not be cast to an integer.
Adding a String to anything will always give you a String, so "R" + something will always return a String. which you are then trying to assign to an Integer.
As the + operator with String as one of the arguments converts the other argument to String:
Global.price = "R" + tblResults.getValueAt(temp,3);
Unless price declared as int in which case:
Global.price = (Integer)tblResults.getValueAt(temp,3);

Parsing string to accept other inputs

General info.
A mixed number is one of 3 forms:
1).An integer such as 12.
2).Fraction = int/int such as ¾
3.Mix of forms 1 and 2: 1 ¾ In this case one of more blanks act as a separator between integer and the fraction.
Question: 1.Treat the 1st and the 2nd forms as the special input and enhance the method parse to include these two cases.
* **I have already done the 3rd form of a mixed number and i am confused on how to parse the string to include an integer and a fraction alongside the mixed number in Mix.java . Output should be in gcd form. The following three programs run together.Some help me please thank you.
Here is my Mixed number code:
import java.util.Scanner;
class Mix extends Fraction{
public Mix(int n, int m) {super(n,m); }
public String displayMix() {
String str="";
if (first < second) str=first+"/"+second;
else str= first/second +" "+ first%second+"/"+second;
return str;
}//display
public Mix(String str) {
int[] iA= parse (str);
int top=iA[0]*iA[2]+iA[1];
int bot= iA[2];
int gcd = gcd(top,bot);
first=top/gcd;
second =bot/gcd;
}//Mix
public static Mix add (Mix s, Mix s2){
int gtop=s.first * s2.second
+ s2.first * s.second;
int gbottom= s.second * s2.second;
return (new Mix(gtop,gbottom));
}//add
public static String get (){
Scanner scan = new Scanner (System.in);
String userInput = scan.nextLine();
userInput =userInput.trim();
return (userInput);
} //get
public static int[] parse (String userInput){
int pos = userInput.indexOf(" ");
String sNum=userInput.substring(0,pos);
int iNum = Integer.parseInt(sNum);//first integer
String sNum2=userInput.substring(pos+1);
pos= sNum2.indexOf("/");
String sTop=sNum2.substring(0,pos);
int iTop = Integer.parseInt(sTop);//second integer
String sBot=sNum2.substring(pos+1);
int iBot = Integer.parseInt(sBot);//third integer
int[] sA = {iNum,iTop,iBot};
return (sA);
} //parse
public static void main(String[] args) {
System.out.print("Please enter mixed-format number :");
String userInput = Mix.get();
System.out.println("Input is: "+userInput);
Mix s = new Mix(userInput);
s.displayMix();
System.out.print("Please enter mixed-format number :");
userInput = Mix.get();
System.out.println("Input is: "+userInput);
Mix s2 = new Mix(userInput);
s2.displayMix();
Mix h= Mix.add(s,s2);
System.out.print(h.displayMix());
}//main
}//class
Here is the Fraction code:
public class Fraction extends Pair {
//attributes: NONE
public Fraction() { first=0; second=1;}
public Fraction(int n, int m) {
super(n,m);
int g=gcd(n,m);
first = first/g;
second=second/g;
}//Fraction
public String display2() {
String str = first+"/"+second;
return str;
}//display
public static Fraction add (Fraction f1, Fraction f2){
int gtop=f1.first * f2.second
+ f2.first * f1.second;
int gbottom= f1.second * f2.second;
return (new Fraction(gtop,gbottom));
}
public static int gcd (int n, int m){
while ( n!=m) {
if (n>m) n=n-m;
else m=m-n;
}//while
return (n);
}//gcd
//test the class
public static void main(String[] args) {
//pseudo-code is here
Fraction f= new Fraction();
f.display();
System.out.print(f.display2());
}//main
} //class
Finally here is pair:
import java.util.Arrays;
public class Pair {
int first;
int second;
public Pair (){first=0; second=0;}
public Pair(int n, int m) {
first=n;
second=m;
}//Pair
public int[] display() {
//pseudo_code is here
int[] c = {first, second};
return c;
}//display
public static void main(String[] args) {
//pseudo-code is here
Pair f= new Pair();
f.display();
System.out.println(Arrays.toString(f.display()));
}//main
} //class
You would have to make your own methods to do that using methods like str.split("/"), I don't know of any other way.

Categories