-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain2.java
More file actions
150 lines (130 loc) · 3.82 KB
/
Main2.java
File metadata and controls
150 lines (130 loc) · 3.82 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* Assignment 6 Merge Arrays
*
* @RobertBerg
* @11/15/17
*/
import java.util.Scanner;
public class Main2
{
public static void main(String[] args)
{
Scanner kin = new Scanner(System.in);
String[] nameArray1 = new String[10000];
String[] nameArray2 = new String[10000];
String[] merged = new String[20000];
System.out.println("Enter the values for the first array, up to 10000 values, enter 'End' to quit");
assignArray(nameArray1, kin);
System.out.println("\nEnter the values for the second array, up to 10000 values, enter 'End' to quit");
assignArray(nameArray2, kin);
System.out.println("\nFirst Array");
printArray(nameArray1);
System.out.println("\nSecond Array");
printArray(nameArray2);
System.out.println();
if(isInOrder(nameArray1) && isInOrder(nameArray2))
{
merged = mergeArrays(nameArray1, nameArray2);
System.out.println("Merged Array");
printArray(merged);
}
else
{
System.out.println("Error: Arrays not in correct order");
}
}
/**
* Assign String values to an array
*
* @param pArray The array that values will be assigned to
* @param pkin The scanner that will read from the console
*/
public static void assignArray(String[] pArray, Scanner pkin){
int i = 0;
String result = pkin.nextLine();
result = result.toLowerCase();
while(!result.equals("end"))
{
pArray[i] = result.substring(0,1).toUpperCase() + result.substring(1);
i++;
result = pkin.nextLine();
result = result.toLowerCase();
}
}
/**
* Merge two arrays together in alphabetical order
*
* @param pArray1 The first array to merge
* @param pArray2 The second array to merge
*
* @return The merged array
*/
public static String[] mergeArrays(String[] pArray1, String[] pArray2)
{
String[] result = new String[20000];
int i = 0, j = 0, k = 0;
while(pArray1[i] != null || pArray2[j] != null)
{
if(pArray1[i] == null)
{
result[k] = pArray2[j];
j++;
}
else if (pArray2[j] == null)
{
result[k] = pArray1[i];
i++;
}
else
{
if(pArray1[i].compareTo(pArray2[j]) < 0)
{
result[k] = pArray1[i];
i++;
}
else
{
result[k] = pArray2[j];
j++;
}
}
k++;
}
return result;
}
/**
* Checks to see if an array is in alphabetic order
*
* @param pArray The array to check
*
* @return true if they are in order, false if they are not
*/
public static boolean isInOrder(String[] pArray)
{
boolean inOrder = true;
int i = 0;
String previous = "";
while(pArray[i] != null)
{
if(previous.compareTo(pArray[i]) > 0)
inOrder = false;
previous = pArray[i];
i++;
}
return inOrder;
}
/**
* Prints each element of the array of Strings
*
* @param pArray The array that will be printed to the console
*/
public static void printArray(String[] pArray){
int i = 0;
while(pArray[i] != null)
{
System.out.print(pArray[i] + " ");
i++;
}
System.out.println();
}
}