Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-07-2011, 08:51 PM   PM User | #1
Kuj4k
New to the CF scene

 
Join Date: Dec 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Kuj4k is an unknown quantity at this point
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.
Kuj4k is offline   Reply With Quote
Old 12-07-2011, 09:11 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

Last edited by Old Pedant; 12-07-2011 at 09:15 PM..
Old Pedant is offline   Reply With Quote
Old 12-07-2011, 09:32 PM   PM User | #3
Kuj4k
New to the CF scene

 
Join Date: Dec 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Kuj4k is an unknown quantity at this point
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.
Kuj4k is offline   Reply With Quote
Old 12-07-2011, 09:50 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
As a hint: Look intp the split method.

http://docs.oracle.com/javase/6/docs...lang.String%29
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Tags
array, find, min

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:05 PM.


Advertisement
Log in to turn off these ads.