Printing String[] produces random null values, unsure why it's happening - java

I tried finding this problem on this site, and the closest similar thread was this:
Can't figure out why im getting null values in my array print statement
Basically, I have an instance variable array of Strings taking words from a dictionary.txt file whenever its method is called (I have a separate main method). I have it set up to create a new array with double the capacity whenever it reaches its limit:
public String[] lengthN (int n) {
/*String[] output = new String[1000];*/
int i = 0;
while (input.hasNext()) {
String word = input.next();
if (i == output.length) {
increaseSize();
}
if (word.length() != n) {
}
if (word.length() == n) {
output[i] = word;
/*System.out.println(output[i]);
System.out.println(i);*/
i++;
}
}
for (int j = 0; j < output.length; j++) {
System.out.println(output[j]); //for testing purposes,returns random null values
}
return output;
}
public void increaseSize() {
String[] temp = new String[output.length + 1000];
for (int i = 0; i < output.length; i++) {
temp[i] = output[i];
output = temp;
}
}
Thankfully, it actually runs and prints out this list. However, in addition to these words, it appears as though large chunks of my list are replaced with null values.
The output itself is too long to post here (with >10000 elements or so), but essentially it is along the lines of
aardvark
null
null
null
null
//lots of null values dispersed throughout
vindicate
vineyards
vintagers
null
null
null
null
If anyone could help point me to a way to fix this problem, I'd be incredibly appreciative! (Apologies if I didn't explain well, first time posting here).

Here:
public void increaseSize() {
String[] temp = new String[output.length + 1000];
for (int i = 0; i < output.length; i++) {
temp[i] = output[i];
output = temp; // <-- here
}
}
you are setting the output array variable to the temp array inside your loop. On subsequent iterations through the loop, output and temp are referencing the same array, so temp[i] = output[i] does nothing. Presumably you meant something like this:
public void increaseSize() {
String[] temp = new String[output.length + 1000];
for (int i = 0; i < output.length; i++) {
temp[i] = output[i];
}
// Once the loop is finished, and the whole contents have been copied, use the `temp` array as the new `output`.
output = temp;
}
That should work pretty much the same as just having:
output = Arrays.copyOf(output, output.length+1000);

Related

Turning 1d array into 2d

I am working on an assignment for my java class and part of the assignment requires reading in a .csv file that is 20x20 and inserting each string into an array.
I am trying to convert my 1d array from the initial reading in of the file into a 2d array, but I seem to be doing something wrong in my output of the data.
I made an add method, and when running the program and calling the method I only get one column of strings and listed in reverse order, but if I do a System.out.println() I don't the output I desire. I am still fairly new to this so I'm sure I just don't see the simple error, but to me, it looks correct.
the reading in of the file
try {
Scanner fileScanner = new Scanner(toOpen);
while (fileScanner.hasNext()) {
fromFile = fileScanner.nextLine();
String temp[] = fromFile.split(" ");
theList.add(temp[0]);
System.out.println(fromFile);
String[][] arr = new String[20][20];
int count = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = temp[i];
System.out.print(arr);
}
}
System.out.println();
}
fileScanner.close();
my add method
public void add(String tableValue) { // Adds a new node
Node newNode = new Node(tableValue);
if (isEmpty()) {
setRoot(newNode);
} else {
newNode.setNext(getRoot());
setRoot(newNode);
}
}
and my method that prints the result
public String makeString() { // A Class that makes a string
String theString = new String();
if (isEmpty()) {
theString = "List is empty";
} else {
Node printer = getRoot();
while (printer != null) {
theString += printer.getTableValue() + " ";
printer = printer.getNext();
}
}
return theString;
}
I guess your problem is here:
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = temp[i];
This assigns the same value (temp[i]) to all slots in arr[i]. Again guessing, I think you need something like:
int tmpIndex = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = temp[tmpIndex];
tmpIndex++;
In other words: you have 400 different values in temp. But your code is only assigning the first 20 values 20 times again and again.
Beyond that: System.out.print(arr); isn't doing what you expect it to do - to learn how to print arrays correctly, see this.
As we don't know the number of lines in a file, we can start by storing each line into an ArrayList (of String[]) and then convert it into a 2D array, e.g.:
List<String[]> lines = new ArrayList<>();
while (fileScanner.hasNext()) {
String line = fileScanner.nextLine();
String temp[] = line.split(" ");
lines.add(temp);
}
Now, convert it into an array:
String[][] array = new String[lines.size()][];
for(int i=0 ; i<lines.size() ; i++){
array[i] = lines.get(i);
}
I hevent seen where you have really used your add and makeString methods, and what is the role of the theList variable.
Also, could you please send your file Content.
any way:
If this is your calling to the add method: theList.add(temp[0]); that means that you are inside an Extended class structure that you have not shown it. but Any way you have not used it to fill the 2d Array in the for loop
the Code here is also error: you insert the same element temp[i] in every line !!!!
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = temp[i];
System.out.print(arr);
}
}
You can use a dynamic structure like ArrayList to fill the Elements...

Not able to add to ArrayList properly

I have 2 methods in my program, one to add ***** above and below the smallest int in the array and one to add %%%%% above and below the largest. The method for the largest is essentially the same as the other but for some reason isn't adding what is needed.
Here is the smallest element method:
public static ArrayList smallestElement() {
int smallest = array[0];
for (int i = 0; i < array.length; i++)
if (array[i] < smallest)
smallest = array[i];
String smallestString = String.valueOf(smallest);
ArrayList<String> list = new ArrayList<String>();
for(int i = 0; i < array.length; i++) {
if (smallestString.equals(String.valueOf(array[i]))) {
list.add("*****");
list.add(Integer.toString(array[i]));
list.add("*****");
} else {
list.add(Integer.toString(array[i]));
}
}
return list;
}
Here is the method for the largest element:
public static ArrayList largestElement() {
int largest = array[0];
for (int i = 0; i < array.length; i++)
if (array[i] > largest)
largest = array[i];
String largestString = String.valueOf(largest);
for(int i = 0; i < array.length; i++) {
if (largestString.equals(String.valueOf(array[i]))) {
smallestElement().add("%%%%%");
smallestElement().add(Integer.toString(array[i]));
smallestElement().add("%%%%%");
} else {
smallestElement().add(Integer.toString(array[i]));
}
}
System.out.println(smallestElement());
return smallestElement();
}
}
If anyone knows why this isn't performing correctly, I would really appreciate the help
You are creating a new object every time you are executing the smallestElement function. Instead do something like,
ArrayList<String> list = smallestElement();
Then use this list object every time you are calling smallestElement() method
You have already created the list 3 times over by this line
smallestElement().add("%%%%%");
smallestElement().add(Integer.toString(array[i]));
smallestElement().add("%%%%%");
Create just 1 list and use it instead of calling the smallestelementelement() function multiple times
You are overcomplicating things here. There is no need to turn that minimum array value into a string right there (and to then do String comparisons later on). Btw: those string comparisons are also your problem: your code will definitely not work when your minimal value shows up several times in your array - because your code will put in those patterns for each match!
Instead, you could do something like:
int indexToUse = 0;
for (int i = 0; i < array.length; i++) { // please always use braces!
if (array[i] < array[indexToUse]) {
indexToUse = i;
}
}
List<String> valuesWithMarkerStrings = new ArrayList<>();
for (int i = 0; i < array.length; i++) {
if (i == indexToUse -1 || i == indexToUse+1) {
valuesWithMarkerStrings.add("******");
} else {
valuesWithMarkerStrings.add(Integer.toString(array[i]);
}
}
(where my solution assumes that you want to have *** ... instead of array[i] for such rows ... )

Code improvement, array of Strings

I am completely new to programming. Can you give me some tips on how to improve my code?
The problem was:
Given an array of strings, return a new array without the strings that are equal to the target string. One approach is to count the occurrences of the target string, make a new array of the correct length, and then copy over the correct strings.
And my code:
public String[] wordsWithout(String[] words, String target) {
int numberOfTargets = 0;
for (int i = 0; i < words.length; i++){
if ( words[i].equals(target) ) numberOfTargets++;
}
String[] result = new String[words.length - numberOfTargets];
for (int i = 0; i < words.length - numberOfTargets; i++){ // 1
result[i] = "0"; // 1
} // 1
for (int i = 0; i < words.length; i++){
if ( !words[i].equals(target) ){
int j = 0; // 2
while ( !result[j].equals("0") ){ // 2
j++; // 2
} // 2
result[j] = words[i];
}
}
return result;
}
Example of how code works:
wordsWithout(["aa", "ab", "ac", "aa"], "aa") → ["ab", "ac"]
I know that new array of ints is filled by zeros dy default. What about new array of Strings? I had to artificially fill it by zeros in part marked as //1, so that I could "scroll" to the right element, when I have to add elements to my new array in part marked as //2.
My code seems to be kind of awkward. Are there any standard methods or general ways to improve my code?
You don't need to set each element to "0".
Just do this:
public static String[] wordsWithout(String[] words, String target) {
int numberOfTargets = 0;
for (int i = 0; i < words.length; i++){
if ( words[i].equals(target) ) numberOfTargets++;
}
String[] result = new String[words.length - numberOfTargets];
int j =0; // for indices of result
for (int i = 0; i < words.length; i++){
if (!words[i].equals(target) ){
result[j++] = words[i];
}
}
return result;
}
Looks like your code could be simplified a lot by just using an ArrayList.
public String[] wordsWithout(String[] words, String target)
{
ArrayList<String> list = new ArrayList<String>();
for(int i = 0; i < words.length; ++i)
{
if(!words[i].equals(target))
{
list.add(words[i]);
}
}
return list.toArray(new String[0]);
}
Basically instead of calculating the size of the target array and initialising it, you use a list (which is variable in size), put in all the elements you need, and then create a new array from it.
Unrelated to that, please don't invent your own values ("0") to describe a null value - there's a dedicated keyword, null, for that.
Use
for (String s : words) {
if (s.equals(target))
numberOfTargets++;
}
This might be a bit simpler. Using the split string method allows you to create an array with each value separated by white space.
public String[] wordsWithout(String[] words, String target) {
String newStr = "";
for (int i = 0; i < words.length; i++){
if (words[i].equals(target))
continue;
newStr = newStr + words[i] +" ";
}
return newStr.split(" ");
}

ArrayIndexOutOfBoundsException for my PrintList function and I have no idea why

I am writing a really simple program which automatically extends the array when the user reaches the limit of the current array.
The problem is that I am getting a java.lang.ArrayIndexOutOfBoundsException when I run my PrintList method and I really don't know why. It's working perfectly if I use a random number, which is bigger than the array (e.g. 500), but if I use
for (int i = 0; i < stringArray.length; i++)
or
for (int i = 0; i <= stringArray.length; i++)
I get a nasty exception. How do I deal with this and why am I getting it in the first place?
Thanks a lot for your help!
Here's the source code of my program:
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int index = 0;
String[] randomString = new String[1];
while (index <= randomString.length) {
out.println("Enter your string");
String input = keyboard.next();
randomString[index] = input;
PrintArray(randomString);
index++;
if (index >= randomString.length) {
ExtendArray(randomString);
continue;
}
}
}
public static void ExtendArray(String[] stringArray) {
String[] secondArray = new String[stringArray.length * 2];
// Copy first array into second array
for (int i = 0; i < stringArray.length; i++) {
stringArray[i] = secondArray[i];
stringArray = secondArray;
}
}
public static void PrintArray(String[] stringArray) {
for (int i = 0; i < stringArray.length; i++) {
out.println(" " + stringArray[i]);
}
}
Java does not work in the methods you are trying to employ. Everything in Java is passed by value, unless it is a data point in an object. What you are trying to employ is a pass by reference, which is not possible in Java.
What you are trying to do is an already existing data structure called a Vector: http://docs.oracle.com/javase/7/docs/api/java/util/Vector.html
I would suggest doing this: (not sure if it will work properly, as my current PC doesn't have dev tools):
public static String[] ExtendArray(String[] stringArray) {
String[] secondArray = new String[stringArray.length * 2];
// Copy first array into second array
for (int i = 0; i < stringArray.length; i++) {
secondArray[i] = stringArray[i];
}
return secondArray;
}
then calling it like so in main:
randomString = ExtendArray(randomString);
Relating to vectors, this is how it works in a Vector class:
public void incrementCount(int count){
int increment = (count * 2);
Object newElementData [] = new Object[increment];
for(int i = 0; i < count; i++)
{
newElementData[i] = elementData[i];
}
elementData = new Object[increment];
elementData = newElementData;
}
In this case, elementData is the original array, newElementData is a temp array that acts to up the bounds.
You cant get error on your PrintArray method, you get the error on the line before!
randomString[index] = input;
Because if you do this
index <= randomString.length
The last iteration is out of bounds, String of length 10 has values on 0-9. You have to change the while cycle to
index < randomString.length
Also your ExtendArray method is NOT functional!
You are supposed to swap out the randomString array for a new array with double length. You create a new array and copy the contents of the old one to the new one, but don't do anything with the new array.
I suppose you want the ExtendArray method to return the new array, and set the randomString variable to be the new array.
You need to return your second array from ExtendArray function:
public static String[] ExtendArray(String[] stringArray) {
String[] secondArray = new String[stringArray.length * 2];
// Copy first array into second array
for (int i = 0; i <= stringArray.length; i++) {
stringArray[i] = secondArray[i];
}
return secondArray;
}
and in your main:
randomString = ExtendArray(randomString);
also your while condition should be:
while (index < randomString.length)

Removing duplicate strings from an array?

How can I remove duplicate strings from a string array without using a HashSet?
I try to use loops, but the words not delete.
StringBuffer outString = new StringBuffer("Our, aim, and, isn't, easy, you, you're, actual, and, are, aren't, and, improve, achieving, and, Obviously, and, illumination, are");
wordList = outString.toString().split(", ");
for (i = 0; i < wordList.length; i++) {
for (j = 0; j < wordList.length; j++) {
if((wordList[i]!=wordList[j])&&(j>i)){
t=true;
}
}
if(t==true){
k++;
}
}
String[] wordList1 = new String[k];
wordList = outString.toString().split(", ");
for (i = 0; i < wordList.length; i++) {
(j = 0; j < wordList.length; j++) {
if((wordList[i]!=wordList[j])&&(j>i)){
t=true;
}
}
if(t==true){
wordList1[i]=wordList[i];
}
}
1)
I think you need to use the equals operator. Try
if (!wordList[i].equals(wordList[j])){
instead of !=.
2) Also Kevin is right. You need to set t back to false.
3) Side note as pointed out by others already: To be more efficient you should start the inner loop with
for (j = i+1; j < wordList.length; j++) {
4) Another side note: Your result array is still too long. If you don't want to use a List<String> and it is ok to loose the original array you could go with a solution as suggested by Zim-Zam O'Pootertoot and set the original duplicates to null, add a counter
to count how many null values you assigned, initialize the new array with the correct size and loop a final time over the first array and copy only the non-null values into your new array.
Try this code to remove dup words:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < wordList.length; i++) {
boolean found = false;
for (int j = i+1; j < wordList.length; j++) {
if (wordList[j].equals(wordList[i])) {
found = true;
break;
}
}
// System.out.printf("Checking: [%s]%n", wordList[i]);
if (!found) {
if (sb.length() > 0)
sb.append(' ');
sb.append(wordList[i]);
}
}
System.out.printf("Unique: [%s]%n", sb);
If you are allowed to use Lists, you can define a generic method that does this fairly easily:
public <T> T[] removeDuplicates(final T[] array) {
List<T> noDuplicates = new ArrayList<T>();
for (T arrayElem : array) {
if (!noDuplicates.contains(arrayElem)) {
noDuplicates.add(arrayElem);
}
}
return (T[]) noDuplicates.toArray();
}
You probably want to set t back to false after pulling the value you want:
if(t)
{
wordList1[i]=wordList[i];
t = false;
}
Also this:
if((wordList[i]!=wordList[j])&&(j>i))
Will always return true since strings are immutable (unless you compared a string to an exact reference of itself which you disallow with j>i). You need to change it to say this:
if (!(wordList[i].equals(wordList[j]))&&(j>i))
Using .equals will compared that they contain the same string, not that they point to the exact reference of a string.
Not sure if that's the only problems or not, a bit unclear from what's given.
In your inner loop, initialize j = i + 1
if(wordlist[i] != null && wordlist[i].equals(worldlist[j])) { wordlist[j] = null; }
...and then compact the array when you're finished to remove all null values
How about using a List:
wordList = outString.toString().split(", ");
List<String> finalList = new ArrayList<String>();
for(String val : wordList) {
if(!finalList.contains(val)) {
finalList.add(val);
}
}
A Set would be more efficient, however. If you can't use a List or a Set, and you are forced to remove the duplicates, then you will have to loop through the array each time, which will perform horribly.
Iterate through the array, and store in an auxiliary int[] or List<Integer> the indexes of duplicates that you find with your two for's.
Create a new Array, with size equal to the original one minus the size of the repeated Strings.
Iterate through your original array, if the index isn't on your auxiliary list, set it to your new Array.
The best and most effective method is to suppose arr is the array that contains strings and can have duplicate values:
Arrays.sort(arr);
int l = 0;
for (int a = 0; a < arr.length; a++) {
if (a == arr.length - 1)
l++;// its a unique value
else if (!(a[a + 1].equals(arr[a])))
l++;// its also a unique
}
String newArray[] = new String[l];
l = 0;
for (int a = 0; a < arr.length; a++) {
if (a == arr.length - 1)
newArray[l] = arr[a];
else if (!(a[a + 1].equals(arr[a]))) {
newArray[l] = arr[a];
l++;
}
}
Try this...
public class RemoveDupsStringArray {
public static void main(String[] args) {
String[] withDuplicates = new String[] {"one","one","two","three","one","three","three"};
String[] withoutDuplicates = new String[] {"one","two","three"};
removeDuplicates(withDuplicates);
removeDuplicates(withoutDuplicates);
}
private static void removeDuplicates(String[] array) {
int[] occurence = new int[array.length];
for (int i = 0; i < array.length; i++) {
for(int j=i+1;j<array.length;j++){
if(array[i]==array[j]){
occurence[j]=j;
}
}
}
int resultLength=0;
for(int i=0;i<occurence.length;i++){
if(occurence[i]==0){
resultLength++;
}
}
String[] result=new String[resultLength];
int index=0;int j=0;
for(int i=0;i<occurence.length;i++){
index = occurence[i];
if(index==0){
result[j]= array[i];
j++;
}
}
for(String eachString : result){
System.out.println(eachString);
}
}
}

Categories