value of array changes for no reason JAVA - java

The contents of arr[] change somehow so when I try to print the contents of the returned array from the method it says null.
This method takes a text file path from the user and puts every individual word into an array and compares it with a dictionary array (book) and the ones that aren't in the dictionary are supposed to be returned in dif[].
int d = arr.length-compare(book, arr);
int l = 0;
int bi = 0;
String[] dif = new String[d];
if (d >= 1) {
for (int i = 0; i < arr.length; i++) {
for (int a = 0; a < book.length; a++) {
if (book[a].equalsIgnoreCase(arr[i])) {
//IF I TRY TO PRINT ARR[I] HERE IT HAS A VALUE
bi = 1;
}
}
if (bi != 1) {
dif[l] = arr[i];
//HERE ARR[I] IS NULL
System.out.println(arr[i]);
l = l + 1;
}
}
return dif;
}
else {
return null;}
}

for (int i = 0; i < arr.length; i++) {
for (int a = 0; a < book.length; a++) {
if (book[a].equalsIgnoreCase(arr[i])) {
//IF I TRY TO PRINT ARR[I] HERE IT HAS A VALUE
bi = 1;
}
}
if (bi != 1) {
dif[l] = arr[i];
//HERE ARR[I] IS NULL
System.out.println(arr[i]);
l = l + 1;
}
}
You never reset the value of bi, so as soon as there is one entry in arr that is equal to one entry in book all subsequent values in arr will be outputted which might include null.

Related

How can I count subsequently equal values in an int array

I would like to count subsequently equal values in an int array and return an array with the count and another array with the value order.
E.g. i would like to transform:
int arr = {0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,2,2,2,2,0,0,0,1,1,1,2,2,2,2,0,0,0,0};
To:
int arr = {11,5,4,3,3,4,4}; // The count
int idx = {0,1,2,0,1,2,0}; // The order
Can you help me achieve this?
This should do the job:
int[] arr = {0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,2,2,2,2,0,0,0,1,1,1,2,2,2,2,0,0,0,0};
//First determine how many subsequently equal values there are
int countEquals = 1;
for (int i = 1; i < arr.length; i++) {
if (arr[i] != arr[i-1]) {
countEquals++;
}
}
//Then calculate the order and count
int[] count = new int[countEquals];
int[] order = new int[countEquals];
int index = 0;
Arrays.fill(count, 1);
for (int i = 1; i < arr.length; i++) {
if (arr[i] == arr[i-1]) {
count[index]++;
} else {
order[index] = arr[i-1];
index++;
}
}

Using Bubble Sort to Alphabetically Sort Array of Names in Java

I've been trying to tackle this bug for a while, but I can't get around to it. The purpose of the program below is to use bubble sort to alphabetically order an array of names. For example, if the names are ["Bob Joe", "Bob Frank", and "Bob Johnson"], the correctly sorted array would be ["Bob Frank", "Bob Joe", "Bob Johnson"].
The main challenge I am having is comparing any 2 strings past name.charAt(0). If I only compare the characters of any 2 strings at 1 specific index point, my code works. However, if I try to make the comparison move past index 0 if index 0 of both strings are equal to each other, my program no longer works.
The code is outlined below
public static void sortAlpha (String names[])
{
for (int i = 0 ; i < names.length - 1 ; i++)
{
for (int a = 0 ; a < names.length - 1 - i ; a++)
{
int length1 = names [a].length ();
int length2 = names [a + 1].length ();
int min = 1;
if (length1 > length2)
{
min = length2;
}
else
{
min = length1;
}
for (int b = 0 ; b < min ; b++)
{
if ((int) names [a].toLowerCase ().charAt (b) > (int) names [a + 1].toLowerCase ().charAt (b))
{
String tempName = names [a];
// sort:
names [a] = names [a + 1];
names [a + 1] = tempName;
break;
}
}
}
}
}
If I simply default the min value to 1, the code runs and does its intended job. However, if the min value stays dynamic, the program does not work. I'm trying to discern why this is so and what the fix is. Any help would be appreciated!
Check this out.
public static void sortAlpha(String names[]) {
for (int i = 0; i < names.length - 1; i++) {
for (int a = 0; a < names.length - 1 - i; a++) {
int lengthLeft = names[a].length();
int lengthRight = names[a + 1].length();
int minLength = lengthLeft > lengthRight ? lengthRight : lengthLeft;
for (int b = 0; b < minLength; b++) {
int letterLeft = (int) names[a].toLowerCase().charAt(b);
int letterRight = (int) names[a + 1].toLowerCase().charAt(b);
if (letterLeft > letterRight) {
String tempName = names[a];
// sort:
names[a] = names[a + 1];
names[a + 1] = tempName;
break;
} else if (letterLeft == letterRight) {
// if the letters are the same go for the next letters
continue;
} else {
// if it's already in the right position - stop.
break;
}
}
}
}
}
Use this
for (int i = 0; i < count; i++)
{
for (int j = i + 1; j < count; j++) {
if (str[i].compareTo(str[j])>0)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
You can simply use compareTo() and a temp variable to compare and store
Scanner sc = new Scanner(System.in);
String n[]= new String[5];
System.out.println("Enter the String");
for(int k = 0;k<5;k++) {
n[k] = sc.nextLine();
}
String temp;
System.out.println("sorted order:");
for (int j = 0; j < n.length; j++) {
for (int i = j + 1; i < n.length; i++) {
if (n[i].compareTo(n[j]) < 0) {
temp = n[j];
n[j] = n[i];
n[i] = temp;
}
}
System.out.println(n[j]);

access array elements in java outside the loop

I am developing a simple program to print seat numbers, where the row are numbered from 1-5 and columns from a-e. the code i am using is as follows
public class JavaApplication5 {
public static void main(String[] args) {
int j =1,k;
int i;
char c;
String[] arr = new String[25];
for( i = 0;i < arr.length;i++)
{
while(j <= 5)
{
for(k = 97;k < 102; k++)
{
c = ((char)k);
arr[i] = j + "" + c;
System.out.println(arr[i]);
}
j++;
}
}
}
}
this displays desired result. but when I print an element outside the for loop I get the result as null like below
public static void main(String[] args) {
int j =1,k;
int i;
char c;
String[] arr = new String[25];
for( i = 0;i < arr.length;i++)
{
while(j <= 5)
{
for(k = 97;k < 102; k++)
{
c = ((char)k);
arr[i] = j + "" + c;
}
j++;
}
}
System.out.println(arr[6]);
}
how to solve this?
this will leave all elements as null
String[] arr = new String[25];
this will iterate until j == 5 so only until arr[5]
while(j <= 5) {
j++;
}
Therefore arr[6] is still null
Change
arr[j] = j + "" + c;
instead of
arr[i] = j + "" + c;
Now it works.
public static void main(String[] args) {
int j = 1, k;
int i;
char c;
String[] arr = new String[25];
for (i = 0; i < arr.length; i++) {
while (j <= 5) {
for (k = 97; k < 102; k++) {
c = ((char) k);
arr[j] = j + "" + c;
}
j++;
}
}
System.out.println(arr[1]);
System.out.println(arr[2]);
System.out.println(arr[3]);
System.out.println(arr[4]);
System.out.println(arr[5]);
System.out.println(arr[6]); // null because your check j <= 5 in while loop
}
You can access the array elements normally outside of the loop. In your example, arr[6] is just null. The fault is not in the way you access it. (Although i cant see the bug yet ;))
The problem in your code is that you simply write 5 time on index 1, then 5 time on index 2 and so on.
So you never wrote on index 6.
Your code should be changed to code below:
String[] arr = new String[25];
int i = 0;
int j = 1;
while (j <= 5) {
for (k = 97; k < 102; k++) {
c = ((char) k);
arr[i++] = j + "" + c;
}
j++;
}
System.out.println(arr[6]);
Because your loops run 5*5 times, then your i index will never pass arr array length.
But you better control it like this to prevent your code from being error prone:
if(i < arr.length) {
arr[i++] = j + "" + c;
} else {
break;
}

null pointer exception sorting merged array list

I have been trying to figure out how to properly print the return of my method.
When the program prints the return of my method, I am giving a nullPointerException error on line 45(the line where i am trying to print the method).
*I did try to make the return to the method static so it is accessible.
How do I initialize the "answer" variable so that i can print it outside of my method?
Thank you in advance
import javax.swing.JOptionPane;
public class ListSortMerge {
static int[]answer;
public static void main(String[] args) {
int v1 = 0, v2 = 0;
for(int c = 0; c <= 1; c++) {
String values = JOptionPane.showInputDialog("How many values would you like to store in list "+(c+1)+"?");
if (c==0) {
v1 = Integer.parseInt(values);
}
else{
v2 = Integer.parseInt(values);
}
}
int[] numbers1 = new int[v1];
int[] numbers2 = new int[v2];
merge(numbers1,numbers2);
int i;
System.out.println("\nList 1 before the sort");
System.out.println("--------------------");
for(i = 0; i < (v1); i++) {
System.out.println(numbers1[i]);
}
System.out.println("\nList 2 before the sort");
System.out.println("--------------------");
for(i = 0; i < (v2); i++) {
System.out.println(numbers2[i]);
}
System.out.println("\nList after the sort");
System.out.println("--------------------");
for(i = 0; i < (v1+v2); i++) {
System.out.println(answer[i]);
}
}
public static int[] merge(int[] a, int[] b) {
int[] answer = new int[a.length + b.length];
for(int c = 0; c < (a.length); c++)
{
String aVal1 = JOptionPane.showInputDialog("Input list 1 value " +(c+1));
a[c] = Integer.parseInt(aVal1);
}
for ( int c = 0; c < (b.length); c++){
String aVal2 = JOptionPane.showInputDialog("Input list 2 value " +(c + 1));
b[c] = Integer.parseInt(aVal2);
}
int i = 0, j = 0, k = 0;
while (i < a.length && j < b.length)
{
if (a[i] < b[j])
answer[k++] = a[i++];
else
answer[k++] = b[j++];
}
while (i < a.length)
answer[k++] = a[i++];
while (j < b.length)
answer[k++] = b[j++];
return answer;
}
}
You have two different answer variables: one is a local variable in the merge function and another is a static field in the class. You never initialize the second one.

how to delete null values/elements from an array

need help again. sorry. how can i remove the null values from my array? here is what i got so far.
int unikCount = 0;
String c = " ";
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
if (tempAry[i].equals(tempAry[j])) {
unikCount++;
}
if (unikCount > 1) {
tempAry[j] = c;
unikCount = 1;
}
}
unikCount = 0;
}
for (i = 0; i < a.length; i++) {
if (tempAry[i] != c) {
unikCount++;
}
}
System.out.println(unikCount);
for (int i = 0; i < a.length; i++) {
for (int j = 1; j < a.length; j++) {
if (tempAry[i].equals(tempAry[j])) {
if (tempAry[i] == c) {
count++;
if (tempAry[j] != c) {
count++;
tempAry[j] = tempAry[i];
}
}
}
}
}
count = 0;
for (int i = 0; i < a.length; i++) {
System.out.println(tempAry[i]);
}
*the remove part is after the "System.out.println(unikCount)". thanks for the upcoming help. by the way, cant use hash and arraylist.
You can check for null like this:
if ( null == someObject )
{
// do things
}
There is no way to remove an element from an array and have it shrink automatically. You'll have to use a temporary array to hold values, create an array of a new size and transfer all those items.
A more effective way would be to use a List.
Look at your logic (with proper indentation):
if(tempAry[i].equals(tempAry[j])) {
if(tempAry[i] == c) {
count++;
if(tempAry[j] != c) {
count++;
tempAry[j] = tempAry[i];
}
}
}
It does not make any sense. Why would you check for tempAry[j] != c inside if(tempAry[i] == c) ???
Did you mean to use if...else instead?
int j = 0;
Object[] temp = new Object[a.length];
for (int i = 0; i < a.length; i++) {
if (a[i] != null) {
temp[j++] = a[i];
}
}
Object[] newA = new Object[j];
System.arraycopy(temp, 0, newA, 0, j);
If the array is an array of, eg, String, you'd of course change "Object" to "String". And if by "null" one means an empty String, then the if test would be changed appropriately.

Categories