Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have to put into an integer for matches in the numbers of an array and another array, which do not have the same position in the array.
For example: I have these two Arrays of numbers:
4578
7539
It means that it have 1 number in the same position (5), and the number 7 is in the first array but not in the same position, so this case must increment 1 in my integer.
If it is in the same position like the number 5, I did this:
int introducido = Integer.parseInt(numero.getText());
for (int i = 0; i < String.valueOf(introducido).length(); i++) {
int entero = Integer.parseInt("" + numero.getText().charAt(i));
String temp = Integer.toString(numAleatorio);
int intarrNumeros = Integer.parseInt("" + temp.charAt(i));
if (intarrNumeros == entero) {
fijas++;
}
But I don't know how to do if is not in the same position.
UPD
Working for non-unique symbols in input strings
Try this code
pattern = "4578 ";
String toFind = "7539";
int samePosition = 0;
int notSamePosition = 0;
for (int i = 0; i < toFind.length(); ++i) {
char digit = toFind.charAt(i);
if (pattern.contains(String.valueOf(digit))) {
if (pattern.charAt(i) == digit) {
++samePosition;
} else {
++notSamePosition;
}
}
}
You can simply change the argument in the if statement to not equals.
if (intarrNumeros != entero)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
My code is:
int diff = 0;
for (int i = 0; i<listOne.size(); i++)
{
for (int j = 0; j<listTwo.size(); j++)
{
if (listOne.get(i)-listTwo.get(j)>diff)
diff = listOne.get(i)-listTwo.get(j);
if (listTwo.get(j)-listOne.get(i)>diff)
diff = listTwo.get(j)-listOne.get(i);
}
}
return diff;
The task is to find the greatest difference between any two numbers in two inputted lists (the difference must be between a number from list one and a number from list two).
I cannot tell what is wrong with my code.
You may be missing Math.abs executed on the diff. Difference is an absolute value, so difference between 5 to 7 and 7 to 5 is same - 2.
int diff = 0;
for (int i = 0; i<listOne.size(); i++) {
for (int j = 0; j<listTwo.size(); j++) {
int elementDiff = Math.abs(listOne.get(i)-listTwo.get(j));
if (elementDiff>diff) {
diff = elementDiff;
}
}
}
return diff;
So both below lines will produce same results:
int elementDiff = Math.abs(listOne.get(i)-listTwo.get(j));
int elementDiff = Math.abs(listTwo.get(i)-listOne.get(j));
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am looking to write a function that prints out a character a certain amount of times.
I am thinking to take in the first two characters from a string and setting up a for loop that runs the number of the second character to print out the first character.
I have this so far but seem to be getting nowhere
public static String output(String a, String b,String c,String d) {
int a= Integer.parseInt(a.substring(0, 2));
for(int i=0;i<a;i++) {
}
If you divide the length of your String by two, you can get at the base number of letter/number pairs that you have. Then you can use a for loop which increments by two each time, and pulls out the next set of two chars and processes them. Something like this:
public static String output(String a) {
String output = "";
int length = a.length();
for (int i = 0; i < length; i+=2) {
String tempString = a.substring(i,i+2);
int x = Integer.parseInt(tempString.substring(1));
for (int t = 0; t < x; t++) {
output += tempString.substring(0,1);
}
}
return output;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am using java
For example, the following situation:
First, the function is used as a print all possible strings.
char[] alphabetSet = "012abc".toCharArray();
int length =5;
Output:
0,1,2,a,b,c,01,02,0a,0b,0c,10,11,12,1a ..................... ccccc. stop in length = 5
Then, I want to add a loop stopper to fetch the specified string.
char[] alphabetSet = "012abc".toCharArray();
int length =5;
int loopStopper = 3;
Output:
a
Thank you
Use backtracking.
void print_all(char []ch,int maxLen){
for(int i=1;i<=maxLen;i++)
backTrack(ch,i,0,new char[i]);
}
void backTrack(char[] ch,int len,int k,char[] ans){
if(k==len){
System.out.print(new String(ans,0,len)+",");
return;
}
for(int i=0;i<ch.length;i++){
ans[k]=ch[i];
backTrack(ch,len,k+1,ans);
}
}
try this:
String alphabet = "012abc";// for example as your code "012abc"
char[] alphabetSet = alphabet.toCharArray();
int length = 5;
for (int i = 0; i < alphabetSet.length; i++) {
System.out.print(alphabetSet[i] + ",");
}
for (int j = 0; j <= length; j++) {
for (int i = 0; i < alphabetSet.length; i++) {
System.out.printf("%d%c,",j,alphabetSet[i]);
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
so my real question is, how can i make this code identify all the "look alike" numbers while theire running from 1 to 99, for example :11,22,33,44,...
and while the program identify them it sends a message.
package doodle;
int num2=11;
for (int i=1; i<100; i++) {
System.out.println(i);
int num1=i;
if(num1==num2) {
System.out.println("WOW");
}
}
Thanks
I would do using a String
for (int i = 11; i < 100; i++) {
StringBuffer orig = new StringBuffer();
String left = orig.append(i).toString();
if (orig.reverse().toString().equals(left)) {
System.out.println(left);
}
}
or if you really wanted to use an int with flaky logic
int start = 11;
for (int i = 11; i < 100; i++) {
if (i == start) {
System.out.println(start);
start += 11;
}
}
Edit
As #mark has rightly pointed out, these solution only work whilst the range is up to 100
int num2=11;
for (int i=1; i<100; i++) {
if(i%num2==0) { //<---- look alike
System.out.println("WOW");
}
I would do it using String conversion and codePoint comparison
for (Integer number = 0; number < 1000; number++) {
System.out.println(number);
String stringnumber = String.valueOf(number);
if (stringnumber.length() > 1 && stringnumber.codePoints().allMatch((digit) -> digit == stringnumber.codePointAt(0))) {
System.out.println("WOW");
}
}
length check (length() > 0) is needed to exclude all numbers with only one digit, otherwise, the program would print "WOW" for all numbers from 0 - 9 too.
All numbers from 0 to Integer.MAX_VALUE can be handled.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Hi I think there may be a problem with my 3 way sort algorithm in the following Java program, also any suggestion on optimizing or just a simpler it would be greatly appreciated. The objective of the sort is to have the minus numbers first then zeros and then positive numbers
class ThreeWaySort
{
public static void main(String[] args)
{
int location = 0;
int[] sArray = new int[50];
for (int a = 25; a<= -24; a--)
{
sArray[location] = a;
location++;
}
int i = 0; int j = 0; int k = 50;
while (j!=k)
{
if (sArray[j]==0)
{
j++;
}
else if (sArray[j]<0)
{
int t = sArray[i]; sArray[i] = sArray[j]; sArray[j] = t; // case (ii)
i++; j++;
}
else
{
k--;
int t= sArray[j]; sArray[j] = sArray[k]; sArray[k] = t;
}
}
for (int a = 0; a <= 49; a++)
{
if(sArray[a] >-1)
{
System.out.println();
System.out.println();
System.out.println();
}
if(sArray[a] > 0)
{
System.out.println();
System.out.println();
System.out.println();
}
System.out.print(sArray[a] + " ");
}
}
}
When i run the program as is it costantly print out a zero followed by three line instead of what I'm expecting to be, Numbers below zero in a line, followed by 3 blank lines then any zeros in the array, 3 blank lines, positive numbers in the array.
The loop that populates your array is incorrect:
for (int a = 25; a<= -24; a--)
The variable a starts at 25, which is not less than or equal to -24, so the loop never executes. You should use >=.