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 debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have this code which suppose to do the following task:
For example , given M=3, and array built like this:
A[0]=1
A[1]=1
A[2]=3
A[3]=3
A[4]=5
A[5]=1
A[6]=3
the function may return 1 or 3
import java.util.*;
class Solution {
int solution(int M, int[] A) {
int N = A.length;
int[] count = new int[M + 1];
for (int i = 0; i <= M; i++)
count[i] = 0;
int maxOccurence = 1;
int index = -1;
for (int i = 0; i < N; i++) {
if (count[A[i]] > 0) {
int tmp = count[A[i]];
if (tmp > maxOccurence) {
maxOccurence = tmp;
index = i;
}
count[A[i]] = tmp + 1;
} else {
count[A[i]] = 1;
}
}
return A[index];
}
}
what could be the problem because it is not always working and I can see there is a bug in my program.
1 1 1 1 5 5 5 5 5 this is a case where your code may fail. Check and update the max occurance variable outside the loop too. Above case give enough justice to my point, i hope.
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 6 years ago.
Improve this question
I have this String array in which is made up of string representing a student id, first name, last name, email,and grades. My question is: Is there away I can split each entry in this array and be able to calculate the average grade of student in this sample array entry. I would appreciate if anyone can offer some solution on how to achieve that in this array.
String[] students = {"1,John,Smith, John1010#fakemail.com ,20,88,79,59",
"2,Suzan,Erickson, Sue9999#fakemail.com ,19,91,72,85",
"3,Jack,Napoli, Jack789#fakemail.com,19,85,84,87",
"4,Erin,Black, Aaron888#fakemail.com,22,91,98,82"};
Here a piece of code that does what you want:
String[] students = ...
double[] averages = new double[students.length];
for (int i = 0; i < students.length; i++) {
String[] student = students[i].split(",");
int sum = 0;
for (int j = 4; j < student.length; j++) {
sum += Integer.parseInt(student[j]);
}
averages[i] = (double) sum / (student.length - 4);
}
The array averages will contain all the averages at the same position. Note that this code doesn't handle any wrong format and that it's assuming that all the remaining values are grades.
I guess writing code for you will not be so appropriate... However, I can suggest what needs to be done:
1. Split each string by using String class split(regex). You can simply split the string with comma.
(https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String))
2. Just add the grade and divide by the size of students array.
I am sorry if you were expecting someone would provide the code...but learning by doing!!
public static void main(String[] args) {
int grades = 0;
String[] students = { "1,John,Smith,John1010#fakemail.com,20,88,79,59",
"2,Suzan,Erickson,Sue9999#fakemail.com ,19,91,72,85", "3,Jack,Napoli,Jack789#fakemail.com,19,85,84,87",
"4,Erin,Black,Aaron888#fakemail.com,22,91,98,82" };
for (int i = 0; i < students.length; i++) {
String[] student = students[i].split(",");
for (int j = 4; j < student.length; j++) {
grades += Integer.parseInt(student[j]);
}
grades=0;System.out.println(student[2] + ": " +(double) grades / (student.length - 4));
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have written this java program for sorting some numbers. But it doesn't stop executing. Can you help me with it?
package inplacesort;
import java.util.*;
public class InplaceSort
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
Vector <Integer> intList = new Vector <Integer> ();
//getting the numbers from the user
char ans = 'y';
while (ans == 'Y' || ans == 'y')
{
System.out.print("Enter a Number: ");
intList.addElement(console.nextInt());
System.out.print("Do You Want to Continue?(Y/N)");
ans = console.next().charAt(0);
}
System.out.println("Before Sorting the Numbers: " + intList);
for (int i = 1; i < intList.size(); i++)
{
int j = i - 1;
while (j > 0 && intList.elementAt(i) < intList.elementAt(j))
{
j--;
}
for (int k = intList.size() - 1; k >= j; k--)
{
intList.insertElementAt(intList.elementAt(k),k + 1);
}
intList.insertElementAt(intList.elementAt(i+1),j);
intList.removeElementAt(i+1);
}
System.out.print(intList);
}
}
Your problem is with intList.size() in the k & i loops. This is not be as what you would expect it. When I debugged your code the value of k was 425996.
Edit :
When i debugged it more I saw that because of you mutating the vector within it self it keeps increasing in size. If you let your program run for a few minutes you will get out of memory error.
Please don't mutate the object you are looping though it. Either make a copy of it and the loop though one of them and mutate another or start with a fresh one & keep adding the values to it while looping over the older one.
System.out.println("Before Sorting the Numbers: " + intList);
List<Integer> sortList = new ArrayList<Integer>();
int minVal;
int index=0;
int size = intList.size();
for (int i = 0; i < size; i++)
{ minVal=Integer.MAX_VALUE;
for (int j = 0; j < intList.size(); j++)
{
if(intList.get(j) < minVal){
index=j;
minVal=intList.get(j);
}
}
intList.remove(index);
sortList.add(minVal);
}
System.out.print("After Sorting the Numbers: "+ sortList);
The reason is because your value for j is ALWAYS 1 less than the value for i. Therefore, infinite while loop.
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 >=.