just wanting to know how do i set an input range for the scanner object, which then transfers the values and inputs them into a 2d array.
I was able to set a range for random number generator but i do not know how to apply the same concept to my scanner object
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random random = new Random();
int rows = 3;
int columns = 5;
System.out.println("Enter array elements : ");
int arry1[][] = new int[rows][columns];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns-2; j++)
{
arry1[i][j] = sc.nextInt();
}
}
arry1[0][3] = random.nextInt(50-10+1)+10;
arry1[1][3] = random.nextInt(50-10+1)+10;
arry1[2][3] = random.nextInt(50-10+1)+10;
int sum1 = 0;
int sum2 = 0;
int sum3 = 0;
sum1 = arry1[0][0]+arry1[0][1]+arry1[0][2]+arry1[0][3];
sum2 = arry1[1][0]+arry1[1][1]+arry1[1][2]+arry1[1][3];
sum3 = arry1[2][0]+arry1[2][1]+arry1[2][2]+arry1[2][3];
arry1[0][4] = sum1;
arry1[1][4] = sum2;
arry1[2][4] = sum3;
System.out.print("DISPLAYING ARRAY:");
System.out.println();
for (int[] x1 : arry1) {
for (int y1 : x1) {
System.out.print(y1 + " ");
}
System.out.println();
}
}
}
Add checking if the value is within range.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random random = new Random();
int rows = 3;
int columns = 5;
System.out.println("Enter array elements : ");
int arry1[][] = new int[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns - 2; j++) {
int input;
do {
input = sc.nextInt();
} while (input < 0 || input > 3);
arry1[i][j] = input;
}
}
arry1[0][3] = random.nextInt(50 - 10 + 1) + 10;
arry1[1][3] = random.nextInt(50 - 10 + 1) + 10;
arry1[2][3] = random.nextInt(50 - 10 + 1) + 10;
int sum1 = 0;
int sum2 = 0;
int sum3 = 0;
sum1 = arry1[0][0] + arry1[0][1] + arry1[0][2] + arry1[0][3];
sum2 = arry1[1][0] + arry1[1][1] + arry1[1][2] + arry1[1][3];
sum3 = arry1[2][0] + arry1[2][1] + arry1[2][2] + arry1[2][3];
arry1[0][4] = sum1;
arry1[1][4] = sum2;
arry1[2][4] = sum3;
System.out.print("DISPLAYING ARRAY:");
System.out.println();
for (int[] x1 : arry1) {
for (int y1 : x1) {
System.out.print(y1 + " ");
}
System.out.println();
}
}
Related
Writing a bubble sort algorithm in java for school and randomly getting an extra line printed to the screen, and I can't figure out why it is doing this. Here is my code.Code result here
import java.util.Scanner;
public class Assignment7 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter the # of numbers to be sorted: ");
int number = input.nextInt();
System.out.print("Enter 1 for ints or 2 for doubles: ");
int intdou = input.nextInt();
if (intdou == 1) {
int x = 0;
int[] num = new int[number];
for (int q = 0; q < number; q++) {
System.out.print("Enter number: ");
num[q] = input.nextInt();
}
for (int w = 0; w < number; w++) {
for (int e = 1; e < (number - w); e++) {
if (num[e - 1] > num[e]) {
x = num[e - 1];
num[e - 1] = num[e];
num[e] = x;
for (int r = 0; r < number; r++) {
System.out.print(num[r] + ", ");
}
}
System.out.print("\n");
}
}
} else if (intdou == 2) {
double x = 0;
double[] num = new double[number];
for (int q = 0; q < number; q++) {
System.out.print("Enter number: ");
num[q] = input.nextInt();
}
for (int w = 0; w < number; w++) {
for (int e = 1; e < (number - w); e++) {
if (num[e - 1] > num[e]) {
x = num[e - 1];
num[e - 1] = num[e];
num[e] = x;
for (int r = 0; r < number; r++) {
System.out.print(num[r] + ", ");
}
}
System.out.print("\n");
}
}
}
}
}
Do you mean the extra blank line? Just move "System.out.print("\n");" inside the if block.
In the blow code the inner 3rd while loop not working please tell me why ?
Here I tried with for loop by replacing the 3rd inner while loop it is working correctly but why not working with while loop .....? can you give me genuine reason...?
import java.util.Scanner;
public class MergeArray {
void arrayInitialization(Scanner arg) {
//entering test cases
System.out.println("enter test cases");
int t = arg.nextInt();
int k, l, i;
k = 0;
l = 0;
i = 0;
//outer while loop
while (t-- > 0) {
//initializing a1[]'s size
System.out.println("enter a1[]'s size");
int as1 = arg.nextInt();
int a1[] = new int[as1];
//inner while loop-1
while (as1-- > 0) {
System.out.println("enter a1[]'s elements");
a1[i] = arg.nextInt();
System.out.print(a1[i]);
i++;
}
i = 0;
//initializing a2[]'s size
System.out.println("enter a2[]'s size");
int as2 = arg.nextInt();
int a2[] = new int[as2];
//inner while loop-2
while (as2-- > 0) {
System.out.println("enter a2[]'s elements");
a2[i] = arg.nextInt();
System.out.print(a2[i]);
i++;
}
System.out.println();
int a3[] = new int[a1.length + a2.length];
int size = as1 + as2;
//inner while loop-3
while (size-- > 0) {
if (k < a1.length)
a3[l] = a1[k];
if (k < a2.length)
a3[l + 1] = a2[k];
k++;
l += 2;
}
for (int j = 0; j < (a1.length + a2.length); j++) {
System.out.print(a3[j]);
}
}
}
public static void main(String[] args) {
MergeArray ma = new MergeArray();
Scanner sc = new Scanner(System. in );
ma.arrayInitialization(sc);
}
}
I tried so much but not found solution. Here I am using while loop because I know that while loop will work fast instead of for loop.
It does not work because you are decrementing the sizes of as1 and as2. Which will be
int size = as1 + as2; // size = 0 + 0;
Instead you can make use of the array length e.g.
int size = as1.length + as2.length;
Murat K's Answer is right, but try to init the arrays like this:
//init a1
System.out.println("enter a1[]'s size");
int a1[] = new int[arg.nextInt()];
//fill a1
for (int i = 0; i < a1.length; i++) {
System.out.println("enter element " + i + " of a1[]");
a1[i] = arg.nextInt();
}
//init a2
System.out.println("enter a2[]'s size");
int a2[] = new int[arg.nextInt()];
//fill a2
for (int i = 0; i < a2.length; i++) {
System.out.println("enter element " + i + " of a2[]");
a2[i] = arg.nextInt();
}
//init a3
int a3[] = new int[a1.length + a2.length];
//merge a1 and a2 into a3
for (int i = 0; i < a1.length; i++) {
a3[i] = a1[i];
}
for (int i = 0; i < a2.length; i++) {
a3[a1.length + i] = a2[i];
}
//print
for (int i : a3) {
System.out.print(i);
}
I need to write a program that produces a single random permutation of the numbers 1 - 10. If possible, no methods or extra include libraries. Simple as can be. Here is what I have so far.
import java.util.Scanner;
public class SwitchingNum {
public static void main(String[] args) {
int num = 10;
Scanner in = new Scanner(System.in);
int[] tempArray = new int[10];
int currentSize = tempArray.length;
System.out.print("First Array: ");
for(int i = 0; i < num; i++){
tempArray[i] = i + 1;
System.out.print(tempArray[i] + " ");
}
int[] permArray = new int[tempArray.length];
System.out.println();
System.out.print("Second Array: ");
for (int i = 0; i < permArray.length; i ++){
permArray[i] = tempArray[(int) (Math.random() * currentSize -1)];
for (int j = i; j < currentSize -1; j++){
tempArray[i] = tempArray[i+1];
}
currentSize--;
System.out.print(permArray[i] + " ");
}
}
}
An easy way to shuffle an array is the Fisher–Yates shuffle. Just copy the original array into your permArray and then do:
Random rand = new Random();
for(int i = 0; i < permArray.length - 1; i++) {
int swapPosition = rand.nextInt(permArray.length - i) + i;
int tmp = permArray[i];
permArray[i] = permArray[swapPosition]
permArray[swapPosition] = tmp;
}
Don't forget to import java.util.Random. For more info, you can take a look at this question.
I can't fix my code. It always says "cannot find symbol" error. I tried to search for some solution but still doesn't work.
import java.util.*;
public class NPP{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int numOfJobs;
float waitingTimeAve = 0, turnaroundTimeAve = 0;
System.out.println("Enter number of jobs : ");
numOfJobs = scan.nextInt();
Job[] job = new Job[numOfJobs]; // Line 15
for(int i = 0; i < numOfJobs; i++ ){
job[i] = new Job(); // Line 18
System.out.println("For job " + (i+1));
System.out.print("Enter arrival time: ");
job[i].setArrivalTime(scan.nextInt());
System.out.print("Enter burst time: ");
job[i].setBurstTime(scan.nextInt());
System.out.print("Enter priority: ");
job[i].setPriority(scan.nextInt());
System.out.println();
}
System.out.println("Type go.");
scan.next();
int jobNum = 0, jobAt = 1, jobBt = 2, jobPrio = 3;
int currentTime;
int [][]timeline = new int[numOfJobs][4];
import java.util.*;
public class NPP{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int numOfJobs;
float waitingTimeAve = 0, turnaroundTimeAve = 0;
System.out.println("Enter number of jobs : ");
numOfJobs = scan.nextInt();
Job[] job = new Job[numOfJobs]; //LINE15
for(int i = 0; i < numOfJobs; i++ ){
job[i] = new Job(); //LINE18
System.out.println("For job " + (i+1));
System.out.print("Enter arrival time: ");
job[i].setArrivalTime(scan.nextInt());
System.out.print("Enter burst time: ");
job[i].setBurstTime(scan.nextInt());
System.out.print("Enter priority: ");
job[i].setPriority(scan.nextInt());
System.out.println();
}
System.out.println("Type go.");
scan.next();
int jobNum = 0, jobAt = 1, jobBt = 2, jobPrio = 3;
int currentTime;
int [][]timeline = new int[numOfJobs][4];
for(int i = 0; i < numOfJobs; i ++){
timeline[i][jobNum] = i;
timeline[i][jobAt] = job[i].getArrivalTime();
timeline[i][jobBt] = job[i].getBurstTime();
timeline[i][jobPrio] = job[i].getPriority();
}
Arrays.sort(timeline, new Comparator<int[]>(){
public int compare(int[] o1, int[] o2) {
return Integer.compare(o1[jobAt], o2[jobAt]);
}
});
currentTime = timeline[0][jobAt];
int [][]ganttchart = new int [100][2];
int g = 0;
for(int i = 0; i < numOfJobs; i++){
int n = 0;
int temp = i;
if(timeline[i][jobAt] > currentTime){
int idle = timeline[i][jobAt] - currentTime;
currentTime += idle;
}
while(timeline[i][jobAt] <= currentTime){
n++;
i++;
if(i == numOfJobs){
break;
}
}
i = temp;
if(n > 1){
for(int j = i; j < n+i; j++){
for(int k = j; k < n+i; k++){
if(timeline[j][jobPrio] > timeline[k][jobPrio]){
int temp1 = timeline[j][jobNum];
int temp2 = timeline[j][jobAt];
int temp3 = timeline[j][jobBt];
int temp4 = timeline[j][jobPrio];
timeline[j][jobNum] = timeline[k][jobNum];
timeline[j][jobAt] = timeline[k][jobAt];
timeline[j][jobBt] = timeline[k][jobBt];
timeline[j][jobPrio] = timeline[k][jobPrio];
timeline[k][jobNum] = temp1;
timeline[k][jobAt] = temp2;
timeline[k][jobBt] = temp3;
timeline[k][jobPrio] = temp4;
}
}
}
}
int temp4 = timeline[i][jobNum];
currentTime += job[temp4].getBurstTime();
job[temp4].setEndTime(currentTime);
ganttchart[g][0] = timeline[i][jobNum];
ganttchart[g][1] = currentTime;
g++;
}
for(int i = 0; i < numOfJobs; i++){
int wt, tt;
tt = job[i].getEndTime() - job[i].getArrivalTime();
job[i].setTurnaroundTime(tt);
turnaroundTimeAve +=tt;
wt = job[i].getTurnaroundTime() - job[i].getBurstTime();
job[i].setWaitingTime(wt);
waitingTimeAve+=wt;
}
turnaroundTimeAve = turnaroundTimeAve/numOfJobs;
waitingTimeAve = waitingTimeAve/numOfJobs;
for(int i = 0; i < numOfJobs; i++){
System.out.println("Job " + (i+1));
System.out.println("End time is " + job[i].getEndTime());
System.out.println("Turnaround time is " + job[i].getTurnaroundTime());
System.out.println("Waiting time is " + job[i].getWatingTime());
System.out.println();
}
System.out.println("Turnaround time average is " + turnaroundTimeAve);
System.out.println("Waiting time average is " + waitingTimeAve);
System.out.println("Gantt Chart: ");
for( int i = 0; i < g; i++){
System.out.println("Job " + (ganttchart[i][0]+1) + " end time of " + ganttchart[i][1]);
}
}
}
For some reason I can't get the count for comparisons and swap in the InsertionSort part, it just outputs zero. And when I isolate the code for it, it outputs a number of swaps and comparisons (though I don't know if it's wrong or not, probably wrong considering the number is the same for both) and the array is not sorted at all. I'm really confused as to why this isn't working, any help is greatly appreciated!
Update: The instance of bubble was being passed onto both selection and insertion, now that that's fixed turns out T also have a problem with the selection part. Any suggestions on how to fix them?
Update 2: Fixed the selection part! Still confused about insertion.
import java.util.Scanner;
public class Sorting {
public static void main(String[] args) {
int n, c;
Scanner scan = new Scanner(System.in);
System.out.print("Number of elements: ");
n = scan.nextInt();
int[] bubbleSortArray = new int[n];
int[] selectionSortArray = new int[n];
int[] insertionSortArray = new int[n];
System.out.print("Enter " + n + " elements: ");
for (c = 0; c < n; c++) {
int i = scan.nextInt();
bubbleSortArray[c] = i;
selectionSortArray[c] = i;
insertionSortArray[c] = i;
}
BubbleSort(bubbleSortArray);
SelectionSort(selectionSortArray);
InsertionSort(insertionSortArray);
}
static void BubbleSort(int[] array) {
int n = array.length;
int cm = 0;
int sw = 0;
for (int c = 0; c < (n - 1); c++) {
for (int d = 0; d < n - c - 1; d++) {
cm++;
if (array[d] > array[d + 1]) {
int swap = array[d];
array[d] = array[d + 1];
array[d + 1] = swap;
sw++;
}
}
}
System.out.print("Bubble sort: ");
for (int c = 0; c < n; c++) {
System.out.print(array[c] + " ");
}
System.out.println("- " + cm + " comparisons, " + sw + " swaps");
}
static void SelectionSort(int[] array) {
int n = array.length;
int cm = 0;
int sw = 0;
for (int c = 0; c < n - 1; c++) {
int index = c;
for (int d = c + 1; d < n; d++){
cm++;
if (array[d] < array[index])
index = d;
}
int temp = array[index];
sw++;
array[index] = array[c];
array[c] = temp;
}
System.out.print("Selection sort: ");
for (int c = 0; c < n; c++) {
System.out.print(array[c] + " ");
}
System.out.println("- " + cm + " comparisons, " + sw + " swaps");
}
static void InsertionSort(int[] array) {
int n = array.length;
int cm = 0;
int sw = 0;
for (int c = 1; c < n; c++){
int temp = array[c];
for (int d = c - 1; d > 0 && temp < array[d]; d--) {
array[d+1] = array[d];
array[d+1] = temp;
cm++;
sw++;
}
}
System.out.print("Insertion sort: ");
for (int c = 0; c < n; c++) {
System.out.print(array[c] + " ");
}
System.out.println("- " + cm + " comparisons, " + sw + " swaps");
}
}
After you are done with BubbleSort array is sorted, and you are passing that sorted instance to SelectionSort and InsertionSort.
If you want to get the results for each kind of sorts you can do :
int n, c;
Scanner scan = new Scanner(System.in);
System.out.print("Number of elements: ");
n = scan.nextInt();
int[] bubbleSortArray = new int[n];
int[] selectionSortArray = new int[n];
int[] insertionSortArray = new int[n];
System.out.print("Enter " + n + " elements: ");
for (c = 0; c < n; c++) {
int i = scan.nextInt();
bubbleSortArray[c] = i;
selectionSortArray[c] = i;
insertionSortArray[c] = i;
}
BubbleSort(bubbleSortArray);
SelectionSort(selectionSortArray);
InsertionSort(insertionSortArray);