Hi im trying to count in a if/else but it wont work.
Every time you try to find the number 32 it should count +1. This is not working for me..
So... i want it to count all the try's ive done to find number 32 so it shows me how much times i tried it.
Can anyone help me out?
String antwoord = null;
int getal = 32;
int count = 0;
if((Integer.parseInt(txt_input.getText().toString()) ) == getal)
{
antwoord = "Goed! in: " + count + " keer";
}
else if((Integer.parseInt(txt_input.getText().toString()) ) <= getal)
{
antwoord = "Hoger... ";
count++;
}
else if((Integer.parseInt(txt_input.getText().toString()) ) >= getal)
{
antwoord = "Lager... ";
count++;
}
count++;
lbl_hoger_lager.setText(antwoord);
You are mixing your logic in the if(condition).
It should be
if(number is equal){
// some operation
}
else if(number is greater){
// some operation
}
else if(number is lesser than X ){
// some operation
}
Hope this helps.
I think you wanted to do this :
String antwoord = null;
int getal = 32;
int count = 0;
if ((Integer.parseInt(txt_input.getText().toString())) == getal) {
count++;
antwoord = "Goed! in: " + count + " keer";
} else if ((Integer.parseInt(txt_input.getText().toString())) < getal) {
antwoord = "Hoger... ";
// count++;
}
else if ((Integer.parseInt(txt_input.getText().toString())) > getal) {
antwoord = "Lager... ";
// count++;
}
lbl_hoger_lager.setText(antwoord);
Some tipps:
You should avoid long strings of common code. For example (Integer.parseInt(txt_input.getText().toString()) ) appears three times. It's a long, complicated expression. How about evaluating this only once and storing the result in a local variable?
int userInput = (Integer.parseInt(txt_input.getText().toString()) );
(and the .toString() is probably not necessary, too)
If you want to count always, count outside of the if.
count is a local variable. It will be 0 every time the code is executed. If you want to preserve the value from previous attempts, you must use a field.
I just now tested with your code and it looks ok to me , obviously with some changes :
public class A
{
public static void main(String [] args)
{
String antwoord = null;
int getal = 32;
int count = 0;
String k ="32";
if((Integer.parseInt(k) ) == getal)
{
antwoord = "Goed! in: " + (count+1) + " keer";
}
else if((Integer.parseInt(k) ) <= getal)
{
antwoord = "Hoger... ";
count++;
}
else if((Integer.parseInt(k) ) >= getal)
{
antwoord = "Lager... ";
count++;
}
// count++;
System.out.println(antwoord);
//lbl_hoger_lager.setText(antwoord);
}
}
Output is :
Goed! in: 1 keer
The way you have it set up, count will increment if the input is greater than or less than 32.
This should work for you:
String antwoord = null;
int getal = 32;
int count = 0;
if((Integer.parseInt(txt_input.getText().toString()) ) == getal)
{
antwoord = "Goed! in: " + count + " keer";
}
else if((Integer.parseInt(txt_input.getText().toString()) ) < getal)
{
antwoord = "Hoger... ";
count++;
}
else if((Integer.parseInt(txt_input.getText().toString()) ) > getal)
{
antwoord = "Lager... ";
count++;
}
lbl_hoger_lager.setText(antwoord);
Related
Given a string, I want to compress the string based on each character's number of consecutive occurrences next to it. For example, let's say we have a string like "abaasass". 'a' occurs one time, 'b' occurs one time, 'a' occurs two times consecutively, 's' occurs one time, 'a' occurs one time, and 's' occurs two times consecutively. The method should then return a string like "aba2sas2".
This is what I have so far:
public static String compressedString(String message) {
StringBuilder compressedString = new StringBuilder();
int total = 0;
for (int i = 0; i < message.length() - 1; i++){
if (message.charAt(i) == message.charAt(i+1)){
total += 2;
compressedString.append(message.charAt(i)).append(total);
}
else {
compressedString.append(message.charAt(i));
}
total = 0;
}
return compressedString.toString();
}
It instead returns: "aba2asas2" which is somewhat close, anyone sees the issue?
public static String compressedString(String message) {
StringBuilder compressedString = new StringBuilder();
int total = 1;
for (int i = 0; i < message.length() - 1; i++){
if (message.charAt(i) == message.charAt(i+1)){
total++;
}
else if(total==1){
compressedString.append(message.charAt(i));
}
else
{
compressedString.append(message.charAt(i)).append(total);
total = 1;
}
}
if(message.charAt(message.length()-2) != message.charAt(message.length()-1)
compressedString.append(message.charAt(message.length()-1));
else
compressedString.append(message.charAt(message.length()-1)).append(total);
return compressedString.toString();
}
public static String compressedString(String message)
{
String result = "" ;
for ( int i = 0, t = message.length() - 1 ; i < t ; )
{
String letter = String.valueOf( message.charAt(i) ) ;
int currentChain = consec( i, message ) ;
result += ( currentChain > 1 ? ( letter + currentChain ) : letter ) ;
i += currentChain ;
}
return result ;
}
private static int consec( int startIndex, String text )
{
int chain = 1 ;
for( int i = startIndex ; i < text.length() - 1 ; ++i )
{
if( text.charAt(i) == text.charAt(i+1) )
chain++ ;
else
break ;
}
return chain ;
}
This is your solution for your question
static void compressedString(String str) {
int n = str.length();
for (int i = 0; i < n; i++) {
// Count occurrences of current character
int count = 1;
while (i < n - 1 && str.charAt(i) == str.charAt(i + 1)) {
count++;
i++;
}
if (count == 1) {
System.out.print(str.charAt(i));
} else {
System.out.print(str.charAt(i));
System.out.print(count);
}
}
}
public static void main(String[] args) {
String str = "abaasass";
compressedString(str);
}
I am trying to write program to find upside down of all double digit number. Actually I have written this code and it works pretty well too. Any other alternatives to find upside down of an integer. Say 18=81 and 96=69....
List < Integer > al = new ArrayList < Integer > ();
al.add(10);al.add(11);al.add(16);al.add(18);al.add(19);al.add(60);al.add(61);
al.add(66);al.add(69);al.add(80);al.add(81);al.add(86);al.add(88);al.add(91);
al.add(96);al.add(98);al.add(99);
Scanner s = new Scanner(System.in);
Integer n = s.nextInt();
if (((String)"" + n).length() == 2) {
if (al.contains(n))
System.out.println("yes");
else
System.out.println("no");
}
This is a simple, general algorithm analysing an integer w.r.t. digits with vertical symmetry. (It depends on a certain style of writing digits; note that "continental" '1' is not symmetrical.)
private static int[] other = new int[]{0, 1, -1, -1, -1, -1, 9, -1, 8, 6};
public static int invert( int n ){
int units = n % 10;
n /= 10;
int inv = other[units];
if( n == 0 ) return inv;
return inv < 0 ? -1 : invert( n )*10 + inv;
}
If a negative value is returned, the number is not symmetrical.
public static void main(String[] args) throws Exception {
for( int i = 100; i <= 199; ++i ){
int invi = invert( i );
if( invi > 0 ){
System.out.println( i + ": " + invi );
}
}
}
This is a better way of doing it.
It not only prints yes or no, but also the up side down number
public static void main(String[] args) {
List<String> al = new ArrayList<String>();
al.add("0");
al.add("1");
al.add("6");
al.add("8");
al.add("9");
Scanner s = new Scanner(System.in);
Integer n = s.nextInt();
String num = n + "";
if (num.length() == 2) {
if(al.contains(num.charAt(0) + "") && al.contains(num.charAt(1) + "")) {
System.out.println("Yes");
String upSideDownNumber = "";
if(num.charAt(1) == 6) {
upSideDownNumber += 9;
} else if(num.charAt(1) == 9) {
upSideDownNumber += 6;
} else {
upSideDownNumber += num.charAt(1);
}
if(num.charAt(0) == 6) {
upSideDownNumber += 9;
} else if(num.charAt(0) == 9) {
upSideDownNumber += 6;
} else {
upSideDownNumber += num.charAt(0);
}
System.out.println("The up side down number is " + upSideDownNumber);
} else {
System.out.println("No");
}
} else {
System.out.println("No");
}
}
Since only 0,1,6,8,9 look like digits when inverted. You can so below:
public static void main(String[] args) throws IOException {
Set<Integer> upsideDownProperDigits = new TreeSet<>(Arrays.asList(0,1,8,9,6));
Scanner s = new Scanner(System.in);
Integer n = s.nextInt();
boolean found = true;
while( n != 0){
int digit = n % 10;
if(!upsideDownProperDigits.contains(digit)){
found = false;
break;
}
n = n/10;
}
if(found){
System.out.println("yes");
}
else{
System.out.println("no");
}
}
Based from the accepted answer from this post, I have this code:
if (authors.length >= 1) {
System.out.print(authors[0]);
}
for (int i = 1; i < authors.length; i++) {
System.out.print("; " + authors[i]);
}
So the output of this is author1; author2; author3
How can I change this into author1; author2 & author3? If there are only 2 authors, the output should be author1 & author2. Thanks in advance.
You'd just need to add in one conditional to your loop to handle the last case:
for (int i = 1; i < authors.length; i++) {
if(i == authors.length - 1)
System.out.print("& " + authors[i]);
else
System.out.print("; " + authors[i]);
}
One way to do it would be changing the structure of the code to use a loop and a boolean flag instead of a conditional, like this:
boolean isFirst = true;
for (int i = 0 ; i != authors.length ; i++) {
if (!isFirst) {
System.out.print(i == authors.length-1 ? "& " : "; ");
} else {
isFirst = false;
}
System.out.print(authors[i]);
}
Demo.
You can do it recursively to separate cases clearly. Seems like other answers lack that.
This is the proxy function:
public static String doIt(String[] authors){
if (authors == null || authors.length == 0){
return "";
}
if (authors.length == 1){
return authors[0];
}
return authors[0] + doHelper(authors, 1);
}
And the helper function:
public static String doItHelper(String[] authors, int index){
if (index == authors.length - 1){
return " & " + authors[index];
}
return "; " + authors[index] + doItHelper(authors, index + 1);
}
As mentioned in comments (Thanks #JNYRanger) this is not optimal when performance is an issue.
Can't test it now, so I hope the idea is clear.
Try it this way:
String[] authors = { "1", "2", "3", "4", "5" };
StringBuffer sb = new StringBuffer();
for (int i = 0; i < authors.length; i++) {
sb.append(authors[i]);
if (i + 2 < authors.length) {
sb.append(";");
} else if (i + 2 == authors.length) {
sb.append("&");
}
}
System.out.print(sb.toString());
for (int i = 0; i < authors.length; i += 1) {
if (i > 0) {
System.out.print(i < authors.length - 1 ? "; " : " & ");
}
System.out.print(authors[i]);
}
String[] authors = {"a", "b", "c", "d"};
for (int i = 0; i < authors.length; i++) {
System.out.print((i != 0 ? (i == authors.length - 1 ? " & " : "; ") : "") + authors[i]);
}
I am trying to print a polynomial from a given number.
I did the example below, but for something like 100 it will print 1x^2+, when I want just x^2. What I'm looking for is how can I make it to not print + and at the same time get rid of coefficients that are 1.
Edit: I did it, it prints perfectly. Feel free to use it.
private static String S_frumos(int poli) {
String s = "";
for (int i = 0; i < String.valueOf(poli).length(); i++) {
int nr = Character.getNumericValue(S_GetCoefs(poli, i));
if (nr != 0) {
if (i == String.valueOf(poli).length() - 1) {
s = s + nr;
} else if (i == String.valueOf(poli).length() - 2) {
if ((S_zero(poli, i + 1) == 1)) {
if (nr != 1) {
s = s + nr + "x";
} else {
s = s + "x";
}
} else {
if (nr != 1) {
s = s + nr + "x" + "+";
} else {
s = s + "x" + "+";
}
}
} else if ((S_zero(poli, i + 1) == 1)) {
if (nr != 1) { s = s + nr + "x^" + (String.valueOf(poli).length() - i - 1);}
else { s = s + "x^" + (String.valueOf(poli).length() - i - 1);}
} else {
if (nr != 1){ s = s + nr + "x^" + (String.valueOf(poli).length() - i - 1) + "+";}
else { s = s + "x^" + (String.valueOf(poli).length() - i - 1) + "+";}
}
}
}
return s;
}
private static int S_GetCoefs(int poli, int x) {
return String.valueOf(java.lang.Math.abs(poli)).charAt(x);
}
To store something of an unknown length... then you can still use an int/double array, just gets slightly more complicated.
public static void main(String[] args)
{
// Say the size is given in a command line argument.
int coefficientNumber = Integer.parseInt(args[0]);
int[] poly = new int[coefficientNumber];
for (int i = 0; i < poly.length; i++)
{
poly[i] = 0;
}
// Set the highest coeffient to 1 (if there is 3 coefficients, this is coefficient
// of x^2, if 4 coefficients, this is coefficient of x^3
poly[0] = 1;
printPoly(poly);
}
// To print a polynomial of unknown length.
// If the coefficient is 0, don't print it.
private static void printPoly(int[] poly)
{
String output = "";
for (int index = 0; index < poly.length; index++)
{
if (poly[index] != 0)
{
// If this is the first coefficient with a value
if (output.length() == 0)
output = poly[index] + "x^" + (poly.length - (index + 1));
// Else if there is already some coefficient with values printed.
else
output += " + " + "x^" + (poly.length - (index + 1));
} // if
} // for
System.out.println(output);
} // printPoly
First of all, storing a polynomial in one variable isn't a great idea as if you have coefficients of more than 9 you'll get confused.
A better method imo (without making a polynomial class) is to store the polynomial in an int/double array.
public static void main(String[] args)
{
// To store the polynomial x^2, you could do the following:
int[] poly = new int[3];
poly[0] = 1;
poly[1] = 0;
poly[2] = 0;
printPoly(poly);
}
// To print it:
private static void printPoly(int[] poly)
{
String output = "";
if (poly[0] != 0)
output += poly[0] + "x^2"
if (poly[1] != 0)
{
if (output.size() > 0)
output += " + " + poly[1] + "^x";
else
output += poly[1] + "x";
}
if (poly[2] != 0)
{
if (output.size() > 0)
output += " + " + poly[2];
else
output += poly[2];
}
}
Herro, Um I'm not sure if this is how you use this site but uh lets get to it... So I need help on this project and I have to do this -
Input [][] Output
A - F -> Multiply by 3, Divide by 4
G - J -> Divide by 3 + 25
K - N -> Find Greatest Factor * 2
O - Q -> Find largest prime inclusive * 3
R - W -> Find Smallest Factor Except 1
X - Z -> Sum Numbers
So I was wondering if my first couple is correct, and need help on the empty space. So the Letter represents the number of the as in "Z" is the last letter so its 26, and "A" is the first so its 1. So if an responses... Thanks ! package Fun;
import java.util.Scanner;
public class Fun {
public static void main(String[] args) {
// TODO Auto-generated method stub
run();
}
public static void run()
{
input();
evaluateAlphabet();
evaluate();
}
public static void input()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Letter, please");
x = sc.next();
}
public static String x = " ";
public static int temp = 0;
public static int answer = 0;
public static void evaluateAlphabet()
{
if(x.equals("A"))
{
temp = 1;
}
else if (x.equals("B"))
{
temp = 2;
}
else if (x.equals("C"))
{
temp = 3;
}
else if (x.equals("D"))
{
temp = 4;
}
else if (x.equals("E"))
{
temp = 5;
}
else if (x.equals("F"))
{
temp = 6;
}
else if (x.equals("G"))
{
temp = 7;
}
else if (x.equals("H"))
{
temp = 8;
}
else if (x.equals("I"))
{
temp = 9;
}
else if (x.equals("J"))
{
temp = 10;
}
else if (x.equals("K"))
{
temp = 11;
}
else if (x.equals("L"))
{
temp = 12;
}
else if (x.equals("M"))
{
temp = 13;
}
else if (x.equals("N"))
{
temp = 14;
}
else if (x.equals("O"))
{
temp = 15;
}
else if (x.equals("P"))
{
temp = 16;
}
else if (x.equals("Q"))
{
temp = 17;
}
else if (x.equals("R"))
{
temp = 18;
}
else if (x.equals("S"))
{
temp = 19;
}
else if (x.equals("T"))
{
temp = 20;
}
else if (x.equals("U"))
{
temp = 21;
}
else if (x.equals("V"))
{
temp = 22;
}
else if (x.equals("W"))
{
temp = 23;
}
else if (x.equals("X"))
{
temp = 24;
}
else if (x.equals("Y"))
{
temp = 25;
}
else if (x.equals("Z"))
{
temp = 26;
}
else if (x.equals("Qwerty"))
{
temp = 27;
}
}
public static void evaluate()
{
if(temp>=1 && temp<= 6)
{
answer = (temp * 3)/4;
System.out.println("Answer is " + answer);
}
else if(temp >= 7 && temp<= 10)
{
answer = (temp/3) + 25;
System.out.println("Answer is " + answer);
}
else if(temp >= 11 && temp<= 14)
{
}
else if(temp>=15 && temp<= 17)
{
for(int i = temp; i>0; i--)
{
for(int j = 2; j <=i/2 + 1; j++)
{
if(i%j==0)
{
break;
}
if(j==i/2 + 1)
{
answer = i * 3;
}
}
}
System.out.println("Answer is " + answer);
}
else if(temp>=18 && temp<= 23)
{
answer = temp;
}
else if(temp>= 24 && temp<=26)
answer = (answer * 12)%26;
System.out.println("Answer is " + answer);
}
}
-Corruption
Here is a better approach for char to int conversion:
Assuming the string has at least 1 character(check for its length), you can get the temp by doing:
temp = x.getCharAt(0) - 'A' + 1;
or, safety first:
temp = 0;
if (x.matches("^[A-Z]{1}$") {
temp = x.getCharAt(0) - 'A' + 1;
}
What's happening here? Every character has an ASCII code, an integer. So, when you have 2 chars and you try to get the distance between them, the result is an int. 'A' - 'A' = 0(that's why i added a + 1), 'B' - 'A' = 1 and so on.
For the if condition, I am using a RegExp. ^ means start of the input, [A-Z]{1} means one of A-Z, $ means the end of the input. So, if it's an A-Z, temp will get a value, anything else won't make it in the if and your temp will remain 0 so you can easily test if you've got an A-Z or not.
That's all for code review, I won't give you solutions, you must work harder, use Google. You won't enjoy and learn if I give you everything ready for a copy paste.