Android intent not working correctly - java

I developed an app.. it is a numerology app.. where user's firstname, second name and third name is collected and calculated... values from 1 -9 are assigned for each values and when calculating these value for all letters has to be added together and make into a one digit 1 - 9. after that this value is given to textview in another activity and the result is displayed.... my problem is i did the programming .. but when adding .. correct value is not displaying.. i used switch case for giving values for each letter... i gave 0 as default value.. when result is displayed the value is displaying as 0 . if i change it to 1 then 1 is added to the value and that value is isplaying.. please check my code and if ther any mistake pls point it out 4 me... thanks...
MainActivity.java
public void gReport(View V)
{
long sum1 = 0;
long sum2 = 0;
long sum3 = 0;
long sum7 = 0;
long sum8 = 0;
long sum9 = 0;
long sum10 = 0;
EditText et1 = (EditText) findViewById (R.id.editText1);
EditText et2 = (EditText) findViewById (R.id.editText2);
EditText et3 = (EditText) findViewById (R.id.editText3);
EditText et7 = (EditText) findViewById (R.id.editText7);
EditText et8 = (EditText) findViewById (R.id.editText8);
EditText et9 = (EditText) findViewById (R.id.editText9);
sum1 = getSum1(et1.getText().toString());
sum2 = getSum2(et2.getText().toString());
sum3 = getSum3(et3.getText().toString());
/*sum7 = getSum7(et7.getText().toString());
sum8 = getSum8(et8.getText().toString());
sum9 = getSum9(et9.getText().toString());*/
sum10 = getSum10 (et1.getText().toString() + et2.getText().toString() + et3.getText().toString());
Intent i = new Intent(this, FirstResult.class);
i.putExtra("name10", sum10 + "");
startActivity(i);
}
private long getSum10(String text)
{
long sum10 = 0;
char[] name10 = new char[text.length()];
name10 = text.toCharArray();
for(int i=0; i<text.length(); i++)
{
sum10 += value10( name10[i] );
}
while (sum10>9)
{
sum10 = findDigitSum10(sum10);
}
return sum10;
}
private long value10(char a)
{
switch(a)
{
case 'A':
return 1;
case 'B':
return 2;
case 'C':
return 3;
case 'D':
return 4;
case 'E':
return 5;
case 'F':
return 6;
case 'G':
return 7;
case 'H':
return 8;
case 'I':
return 9;
case 'J':
return 1;
case 'K':
return 2;
case 'L':
return 3;
case 'M':
return 4;
case 'N':
return 5;
case 'O':
return 6;
case 'P':
return 7;
case 'Q':
return 8;
case 'R':
return 9;
case 'S':
return 1;
case 'T':
return 2;
case 'U':
return 3;
case 'V':
return 4;
case 'W':
return 5;
case 'X':
return 6;
case 'Y':
return 7;
case 'Z':
return 8;
default:
return 0;
}
}
private long findDigitSum10(long n)
{
int sum10=0;
while (n != 0)
{
sum10 += n % 10;
n = n / 10;
}
return sum10;
}
}
ResultActivity.java
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.firstresult_xm);
TextView txt1 = (TextView) findViewById (R.id.textView2);
txt1.setText(getIntent().getStringExtra("name10"));
}

you have no key sum10 inside your intent, but only name10. You should see the String int first TextView and nothing in the second one

May be encoding in eclipse is different that Android? Try to change it to UTF-8 in propreties of project.
I tried such concole code. Your function of value10 works correctly.
public class HelloWorld {
public static void main(String[] args) {
HelloWorld hw = new HelloWorld();
long a = hw.value10('F');
System.out.println("val: " + a);
}
private long value10(char a)
{
// TODO Auto-generated method stub
switch(a)
{
case 'A':
return 1;
case 'B':
return 2;
case 'C':
return 3;
case 'D':
return 4;
case 'E':
return 5;
case 'F':
return 6;
case 'G':
return 7;
case 'H':
return 8;
case 'I':
return 9;
case 'J':
return 1;
case 'K':
return 2;
case 'L':
return 3;
case 'M':
return 4;
case 'N':
return 5;
case 'O':
return 6;
case 'P':
return 7;
case 'Q':
return 8;
case 'R':
return 9;
case 'S':
return 1;
case 'T':
return 2;
case 'U':
return 3;
case 'V':
return 4;
case 'W':
return 5;
case 'X':
return 6;
case 'Y':
return 7;
case 'Z':
return 8;
default:
return 0;
}
}
}

Related

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

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;

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 to convert If statement to switch statement?

how can I convert this if statement to switch statement by considering that I am using two variables here and I tried to solve it but not working
int child;
char gender;
int temp;
child=console.nextInt();
gender=console.next().charat(0);
if(gender=='m' && children>=4)
temp =1;
else if(gender=='m' && children<4)
temp =2;
else if(gender=='f' && children<4)
temp =3;
else
temp=4;
}
this is my code
int children;
double temp;
char gender;
children=console.nextInt();
switch(children , gender )
{
case < 4, 'm':
temp=1;
break;
default : salary=600;
}
You should start by having a look at The switch Statement
The switch statement is basically evaluating a single condition per case. You can use drop through conditions, but that's a lot of additional code.
For example, something like...
switch (gender) {
case 'm':
temp = 0;
if (children >= 4) {
temp += 1;
} else {
temp += 2;
}
break;
case 'f':
temp = 2;
if (children >= 4) {
temp += 2;
} else {
temp += 1;
}
break;
}
would generate the same results as your if statements
If you preferred to use pure switch statements, you could do something like...
switch (gender) {
case 'm':
temp = 0;
switch (children) {
case 0:
case 1:
case 2:
case 3:
temp += 2;
break;
default:
temp += 1;
}
break;
case 'f':
temp = 2;
switch (children) {
case 0:
case 1:
case 2:
case 3:
temp += 1;
break;
default:
temp += 2;
}
break;
}
Java's switch statement doesn't support ranges :(
switch (gender) {
case 'm':
temp = (children >= 4)? 1:2;
break;
case 'f':
temp = (children >= 4)? 4:3;
break;
default:
temp = 4;
break;
}

Java - Sharing int values between cases in a switch statment

I'm new to java and not sure how do I share values between cases in a switch statement? When I try to use a variable which i created in the previous case it tells me "variable might not have been initialized"
Code:
case 6:
String stringCopy = stringInput;
String lowerCase = stringCopy.toLowerCase();
int vowelCount = 0;
int stringLength = lowerCase.length();
for (int i = 0; i <= stringLength - 1; ++i){
switch(stringInput.charAt(i)) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowelCount++;
}
System.out.println(vowelCount);
break;
}
case 7:
int noofConstants = 0;
noofConstants = (stringLength - vowelCount);
Declare and initialize value before the switch statement.
int value = 0;
switch (key) {
case 3:
value = 1 + 1;
break;
case 4:
value = 1;
break;
you cannot access a variable that you initialize in a separate code block which is case .
declare it outside/before the code block
int stringLength = 0;
switch(){
case 6:
stringLength = 1;
break;
case 7:
stringLength = 2;
break;
}

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