Home arrow static arrow Java Programming [Archive] - Problem with code-Please Help!!!
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Problem with code-Please Help!!!
This topic has 4 replies on 1 page.

Posts:6
Registered: 6/26/04
Problem with code-Please Help!!!  
Jun 26, 2004 10:47 PM



 
Hey guys i have some problems with the following code:

1. I have to do a stutter and reverse of an array [ 2 4 ] and it should return [ 4 4 2 2 ]. I wrote the code and i get [ 2 2 4 4]. How can this be fixed?

public class Part1
{
public static void main(String[] args)
{
int[] values = {2, 4};

stutterAndReverse(values);
}

public static int[] stutterAndReverse(int[] values)
{
//Stutter
System.out.print("[ ");
for (int i = 0; i < values.length; i++)
{
for(int j = values.length-1; j >=0; j--)
{
System.out.print(values + " ");
}
}
System.out.print("] ");
return values;

}
}

2. This one takes the array and returns the even numbers to the left and odds to the right. It works with the code i have so far but the odds on the right should be in order. For example an array of 6 gives the output [2, 4, 6, 5, 3, 1]. Mine gives [2, 4, 6, 1, 5, 3]. How can i fix this???

public class Part2
{
public static void main(String[] args)
{

int[] arr = new int[6];

for(int i = 0; i < arr.length; i++)
{
arr = i + 1 ;
}
printArray(arr);
arr = evensToLeft(arr);
//arr = oddsToRight(arr);
System.out.println("Evens to the Left");
printArray(arr);

}

public static void printArray(int[] arr)
{
System.out.print("[");
for(int i = 0; i < arr.length; i++)
{
System.out.print(arr);
if(i < arr.length-1)
System.out.print(", ");
}
System.out.println("]");
}

public static int[] makeArray(int n)
{
int[] arr = new int[6];
return arr;
}

public static int[] evensToLeft(int[] arr)
{
int split = 0;
for(int i = 0; i < arr.length; i++)
{
if(arr%2==0) //even
{
swap(arr,i,split);
split++;
}
}
return arr;
}

public static void swap(int[] arr, int n1, int n2)
{
int temp = arr[n1];
arr[n1] = arr[n2];
arr[n2] = temp;
}
}

Thank you for your time i really appreciate it.

 

Posts:450
Registered: 6/5/03
Re: Problem with code-Please Help!!!  
Jun 27, 2004 3:48 AM (reply 1 of 4)



 
if you want to sort array, then you may use java.util.Arrays.sort()
if you had array of Objects, then you could define a custom Comparator that would make sure that your array would be sorted in your needed custom way whitout any interferrece from you... but you have just those primitieves...

unfortunatelly i didn't find "reverese()" method from Arrays class, i'm sure that tere is somewhere that method, and you could use that as well, but then you'd maybe have to convert your int[] to something else for a while...
but then again, reversing a array isn't that hard after all...
 

Posts:6
Registered: 6/26/04
Re: Problem with code-Please Help!!!  
Jun 27, 2004 11:19 AM (reply 2 of 4)



 
now i'm more confused than ever...:(
 

Posts:5,451
Registered: 17/08/03
Re: Problem with code-Please Help!!!  
Jun 27, 2004 5:03 PM (reply 3 of 4)



 
now i'm more confused than ever...:(

This'll probably make it worse :)
class Testing{  public Testing()  {    String[] values1 = {"2","4"};    System.out.println(java.util.Arrays.asList(values1));    System.out.println(java.util.Arrays.asList(stutterAndReverse(values1)));    String[] values2 = {"1","2","3","4","5","6","7","9","11"};    System.out.println(java.util.Arrays.asList(values2));    System.out.println(java.util.Arrays.asList(evensOdds(values2)));  }  public String[] stutterAndReverse(String[] arr)  {    String[] temp = new String[arr.length * 2];    for(int x = 0; x < temp.length; x++)    {      temp[temp.length - 1 -x] = arr[x/2];    }    return temp;  }  public String[] evensOdds(String[] arr)  {    StringBuffer evens = new StringBuffer();    StringBuffer odds = new StringBuffer();    for(int x = 0; x < arr.length; x++)    {      if(Integer.parseInt(arr[x]) % 2 == 0) evens.append(","+arr[x]);      else odds.insert(0,","+arr[x]);    }    return (evens.toString().substring(1)+odds.toString()).split(",");  }  public static void main(String[] args){new Testing();}}
 

Posts:6
Registered: 6/26/04
Re: Problem with code-Please Help!!!  
Jun 27, 2004 7:32 PM (reply 4 of 4)



 
Thanx a lot man you really helped me i was thinking these stuff completely different but this works great. The only problem is for the evensOdds of the array if you put 0, the zeros are all in the beginning instead of the end. I guess if it's zero then it should return an array of size 0. Other than that great job and thanks again for your help.
 
This topic has 4 replies on 1 page.