Post: Please help!! (java)
02-19-2014, 05:44 PM #1
|C++|
< ^ > < ^ >
(adsbygoogle = window.adsbygoogle || []).push({}); Iv'e had it up to here with trying to make this program work :fyou:

Iv'e refrained from looking online seeking help etc but I just cant deal anymore...

My assignment is to merge two arrays using int arrays that the user fills and we have to assume that there will be a maximum of 10000 inputs from the user, and the user inputs a negative number to stop. Then sort the array from least to greatest and print it out. Initially i thought that this would be quite easy but when i finished, i began getting outputs such as:


Enter the values for the first array, up to 10000 values, enter a negative number to quit: 1
3
5
-1
Enter the values for the second array, up to 10000 values, enter a negative number to quit
2
4
6
-1
First Array:
1
3
5
Second Array:
2
4
6
Merged Array:
6 1 2 3 4 5
>

as you can see, the six is out of place and i have no idea how to fix it. Here is the source code, i have included copious comments because I really want you guys to help me out to the best of your abilities. IF it's possible to use the same exact technique without implement new techniques and methods into the code please do so. I know there are methods in java that can do all of this in one line but it's for an assignment at a more basic level.

    import java.util.Scanner;

public class Merge
{
public static void main(String [] args)
{
Scanner scan = new Scanner(System.in);

int [] first = new int[10000]; //first array, assume 10k inputs max
int [] second = new int[10000]; //first array, assume 10k inputs max

boolean legal = true; //WILL IMPLIMENT LATER


int end = 0; // set how many elements to put in my "both" array
int end2 = 0;// set how many elements to put in my "both" array


System.out.print("Enter the values for the first array, up to 10000 values, enter a negative number to quit");
//get values
for(int i = 0; i<first.length; i++)
{
first[i] = scan.nextInt(); //fill first with user input

if(first[i] <0) //if negative number, stop loop
{
end = i; //get position of end of user input
break;
}
}

System.out.println("Enter the values for the second array, up to 10000 values, enter a negative number to quit");

for(int i = 0; i<second.length; i++) //exact same as the first get values loop
{
second[i] = scan.nextInt();

if(second[i] <0)
{
end2 = i;
break;
}
}

System.out.print("First Array:\n");

for(int i = 0; i<first.length; i++) //print first array
{
if(i == end) //this prevents from printing thousands of zeros, only prints values that user inputed
break;
System.out.println(first[i] + " ");
}


System.out.print("Second Array:\n");

for(int i = 0; i<second.length; i++) //same as printing first array
{
if(i == end2)
break;
System.out.println(second[i] + " ");
}

int [] both = new int[(end)+(end2)]; //instanciate an int array to hold only inputted values from first[] and second[]
int [] bothF = new int[(end)+(end2)]; //this is for my simple sorter algotithm loop

for(int i = 0; i<both.length; i++) //fill both with the first array that was filled
{
both[i] = first[i];
}

int temp = end; // see below
for(int i = 0;i<both.length; i++) //fill array with the second array that was filled(starting from the end of the first array so that the first set is not overwritten
{
if(temp<both.length){ //this prevents an out of bounds
both[temp] = second[i];

temp++;}
}

//simple sorting algorithm
for(int d = both.length -1;d>=0;d--)
{
for(int i = 0; i<both.length; i++)
{
if(both[d]<both[i])
{
bothF[d] = both[d];
both[d] = both[i];
both[i] = bothF[d];

}

}
}



System.out.println("Merged Array:"); //print the results
for(int i = 0; i<both.length; i++)
{
System.out.print(both[i] + " ");

}

//System.out.println("ERROR: Array not in correct order");


}
}
(adsbygoogle = window.adsbygoogle || []).push({});
02-23-2014, 03:29 AM #2
Script
Banned
Originally posted by C
Iv'e had it up to here with trying to make this program work :fyou:

Iv'e refrained from looking online seeking help etc but I just cant deal anymore...

My assignment is to merge two arrays using int arrays that the user fills and we have to assume that there will be a maximum of 10000 inputs from the user, and the user inputs a negative number to stop. Then sort the array from least to greatest and print it out. Initially i thought that this would be quite easy but when i finished, i began getting outputs such as:


Enter the values for the first array, up to 10000 values, enter a negative number to quit: 1
3
5
-1
Enter the values for the second array, up to 10000 values, enter a negative number to quit
2
4
6
-1
First Array:
1
3
5
Second Array:
2
4
6
Merged Array:
6 1 2 3 4 5
>

as you can see, the six is out of place and i have no idea how to fix it. Here is the source code, i have included copious comments because I really want you guys to help me out to the best of your abilities. IF it's possible to use the same exact technique without implement new techniques and methods into the code please do so. I know there are methods in java that can do all of this in one line but it's for an assignment at a more basic level.

    import java.util.Scanner;

public class Merge
{
public static void main(String [] args)
{
Scanner scan = new Scanner(System.in);

int [] first = new int[10000]; //first array, assume 10k inputs max
int [] second = new int[10000]; //first array, assume 10k inputs max

boolean legal = true; //WILL IMPLIMENT LATER


int end = 0; // set how many elements to put in my "both" array
int end2 = 0;// set how many elements to put in my "both" array


System.out.print("Enter the values for the first array, up to 10000 values, enter a negative number to quit");
//get values
for(int i = 0; i<first.length; i++)
{
first[i] = scan.nextInt(); //fill first with user input

if(first[i] <0) //if negative number, stop loop
{
end = i; //get position of end of user input
break;
}
}

System.out.println("Enter the values for the second array, up to 10000 values, enter a negative number to quit");

for(int i = 0; i<second.length; i++) //exact same as the first get values loop
{
second[i] = scan.nextInt();

if(second[i] <0)
{
end2 = i;
break;
}
}

System.out.print("First Array:\n");

for(int i = 0; i<first.length; i++) //print first array
{
if(i == end) //this prevents from printing thousands of zeros, only prints values that user inputed
break;
System.out.println(first[i] + " ");
}


System.out.print("Second Array:\n");

for(int i = 0; i<second.length; i++) //same as printing first array
{
if(i == end2)
break;
System.out.println(second[i] + " ");
}

int [] both = new int[(end)+(end2)]; //instanciate an int array to hold only inputted values from first[] and second[]
int [] bothF = new int[(end)+(end2)]; //this is for my simple sorter algotithm loop

for(int i = 0; i<both.length; i++) //fill both with the first array that was filled
{
both[i] = first[i];
}

int temp = end; // see below
for(int i = 0;i<both.length; i++) //fill array with the second array that was filled(starting from the end of the first array so that the first set is not overwritten
{
if(temp<both.length){ //this prevents an out of bounds
both[temp] = second[i];

temp++;}
}

//simple sorting algorithm
for(int d = both.length -1;d>=0;d--)
{
for(int i = 0; i<both.length; i++)
{
if(both[d]<both[i])
{
bothF[d] = both[d];
both[d] = both[i];
both[i] = bothF[d];

}

}
}



System.out.println("Merged Array:"); //print the results
for(int i = 0; i<both.length; i++)
{
System.out.print(both[i] + " ");

}

//System.out.println("ERROR: Array not in correct order");


}
}


If you know C++ hence your name, you should know Java. C++ is harder than Java, and they both have similar syntax.

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo