home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

java code question...

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 2160
Discussions: 200,972, Posts: 2,379,766, Members: 246,336
Old July 13th, 2009, 03:43 PM   Digg it!   #1 (permalink)
Junior Member
 
Join Date: Jul 2009
Posts: 2
java code question...

Another question about java coding. I am getting a run time error. The program is to generate random numbers in two different arrays list each unsorted then sorted then merge the two sorted. I am getting the runtime error in the mergedArray method. Can anyone explain to me why this might be? Thank You

Code:

import java.util.*;
import java.io.*;
public class PGM2
{
public static void main(String[] args) throws IOException
{
String fname;
Scanner xyz = new Scanner(System.in);
System.out.println("Output filename? ");
fname = xyz.nextLine();
PrintWriter myOutputFile = new PrintWriter(fname);

int nums1 []; int nums2 []; int nums3 []; int sz1; int sz2;

System.out.print("What's the size of the first array? ");
sz1 = xyz.nextInt();
nums1 = new int[sz1];
fillArray(nums1);
System.out.println();
System.out.println("\n\nUnsorted Array #1: ");
writeNums(nums1, myOutputFile);
IntBubbleSorter.bubbleSort(nums1);
System.out.println();
System.out.println("\n\nSorted Array #1: ");
System.out.println();
writeNums(nums1, myOutputFile);
System.out.println();
System.out.println("What's the size of the second array? ");
sz2 = xyz.nextInt();
nums2 = new int[sz2];
fillArray(nums2);
System.out.println();
System.out.println("\n\nUnsorted Array #2: ");
writeNums(nums2, myOutputFile);

IntBubbleSorter.bubbleSort(nums2);

System.out.println();
System.out.println("\n\nSorted Array #2: ");
writeNums(nums2, myOutputFile);

nums3 = new int[sz1+sz2];

mergeArrays(nums1, nums2, nums3);

System.out.println("\n\nMerged Array: ");
System.out.println();
writeNums(nums3, myOutputFile);

myOutputFile.close();
}
public static void fillArray(int x[])
{
Random rd = new Random(System.currentTimeMillis());
for(int i = 0; i < x.length; i++)
x[i] = rd.nextInt(10000);
}

public static void writeNums(int x[], PrintWriter y)
{
for (int i = 0; i < x.length; i++)
{ System.out.printf("%8d", x[i]);
if (i%5 == 4)
System.out.println(); }
}

public static void mergeArrays(int x[], int y[], int z[])
{
int i=0; int j=0; int k=0; int index;
while(k < z.length)
if(x[i]==y[j])
{ z[k] = x[i]; z[k+1] = x[i]; i++; j++; k+=2; }
else
if(x[i] > y[j])
{ z[k] = y[j]; j++; k++; }
else
if(x[i] < y[j])
{ z[k] = x[i]; i++; k++; }

}





public static void bubbleSort(int[] array)
{
int maxElement;
int index;
int temp;


for (maxElement = array.length - 1; maxElement >= 0; maxElement--)
{
// The inner loop steps through the array, comparing
// each element with its neighbor. All of the elements
// from index 0 thrugh maxElement are involved in the
// comparison. If two elements are out of order, they
// are swapped.
for (index = 0; index <= maxElement - 1; index++)
{
// Compare an element with its neighbor.
if (array[index] > array[index + 1])
{
// Swap the two elements.
temp = array[index];
array[index] = array[index + 1];
array[index + 1] = temp;
}
}
}
}
}
lostgoat is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads
Thread Thread Starter Forum Replies Last Post
Asking for help with java code..... lostgoat Webmastering and Programming 2 July 14th, 2009 03:14 PM
Java Code mstabiggzlilmama Applications and Operating Systems 0 November 19th, 2008 03:38 PM
Can someone help me with this Java Code? quantumlight Webmastering and Programming 4 November 9th, 2008 04:02 PM
HELP WITH SIMPLE JAVA CODE---PLEASE HELP orcdba Applications and Operating Systems 2 November 8th, 2008 05:11 AM
Need help with some Java Code :P Soheils91 General Tech Discussion 2 October 20th, 2004 08:57 PM


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (3054)
The disrespect of Obama by Russian .. (48)
Delete an OS (16)
Breaking: San Diego ACORN Document .. (10)
Nvidia GTX 260 problem (8)
Laptop with wireless problem. (12)
Wireless Televisions. (12)
CPU fan stops spinning randomly (11)
windows vista security holes (17)
Regular Build (11)
windows 7 problem (7)
Internet Lost (5)
Point and Shoot Camera Suggestions. (6)
Print spooler problem (16)
Recent Discussions
Delete an OS (16)
Multiple Restarts Required at Boot (4)
cell phone won't work (0)
Nvidia GTX 260 problem (8)
Is the PSU I received dead? (15)
Can't open Word (12)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (37)
Steam ID's, Gamertags etc... (4)
Games, Cables, PCI cards, and more fo.. (6)
Dept. of HS: NSA 'Helped' Develop Vis.. (17)
Linksys WMP54GS wireless card problem.. (5)
windows vista security holes (17)
Help getting around port 80 for camer.. (5)
Skillsoft Network+ Study Software Que.. (10)
Browsers wont load websites (3)
help me pls laptop just stopped worki.. (0)
Open With ..... Win7 (3)
Laptop with wireless problem. (12)
Internet Lost (5)
virus blocking exe. files (1)
Point and Shoot Camera Suggestions. (6)
CPU fan stops spinning randomly (11)
Modern Warfare 2: Who Bought It? (65)
Print spooler problem (16)
Kingston Bluetooth Dongle Driver (1)


All times are GMT -4. The time now is 10:15 PM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28