Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Problem with arrays.

1. ## Problem with arrays.

Im working on a program where the user enters a line of numbers and then can either sort them in order or can display the minimum number.

Both those seem to work, but for some reason I keep getting three -1 values in my array. So the minimum value is always -1 and when I sort it comes back -1-1-1[numbers i enter].

I cant seem to figure out why my code is doing that.

Code:
```  private int[] buildArray(String line) {
int[]arr = new int[line.length()];
arr[0] = 0;
for (int i = 1; i < line.length(); i++){
arr[i] = Character.digit(line.charAt(i),10);

}
return arr;
}

private int findMin(int numbers[]) {
//PRE numbers.length >= 1

int min = numbers [0];

for (int i = 1; i < numbers.length; i++){

if (numbers [i] < min) {
min = numbers [i];
}
}
return min;
}

private String makeString(int numbers[]) {
//PRE numbers.length >= 1
int i = 0;
String s1 = new String ("");
while (i < numbers.length){
s1 = numbers[i] + s1;
i++;

}
return s1;

}

private void sort(int numbers[]) {
for (int i = 1; i< numbers.length; i++){
int j = i-1;
int temp = numbers[i];
while (j >= 0 && temp > (numbers[j])){
numbers[j+1] = numbers[j];
j--;
}
numbers[j+1] = temp;
}
}```
Input: 3,1,2,88

output:
Min Value = -1
Line 1 Sorted = -1-1-101288

Any type of help would be very appreciated, Im very new to this still.

2. This is the javaSCRIPT forum.

Your program is written in JAVA.

About the only thing the two languages have in common are the first 4 letters of their names.

Try the Java forum.

And might I suggest that when you post in that forum that you post your entire code?

You are showing all the "worker bee" functions, but not the code that is driving them.

*********

EDIT: Actually, the answer is dead obvious!

You say your input string is 3,1,2,88

The -1's are the *COMMAS*

Your buildArray function is very badly broken. It is treating each *SINGLE CHARACTER* as a number.

If you had use input 1000000,8,9 you would have seen your sorted ouput as -1-1000000189 as your poor Java program does its best to treat a comma as a digit and then works on each character separately.

Throw out your buildArray and start over with it.

3. O sorry for the confusion, Im only a few weeks into this. But actually that helped a lot thanks. Ill try to stay in Java forums from now on.

4. As a hint: Look intp the split method.

http://docs.oracle.com/javase/6/docs...lang.String%29

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•