Please click here to see my task's screenshot
Hello, could you please help me? What is wrong with my code? I understand that there is something wrong in my Array's length or my If statement, but i couldnot able to find out it.
Thank you very much!
import java.util.Scanner;
class TripleSwapping {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Please enter the count of your elements: ");
int count = in.nextInt();
System.out.println("Your array before sorting: ");
int A[] = new int [count];
for (int i = 0; i < 10; i++) {
A[i]=in.nextInt();
}
boolean changed =false;
do {
for (int i = 0; i < A.length-1; i++) {
if (!(A[i+1]>A[i]) && !(A[i+1] > A[i+2])) {
int temp;
temp = A[i];
A[i]= A[i+1];
A[i+1] =temp;
}
}
for (int i = 0; i < A.length-1; i++) {
if (A[i] >A[i+1]) {
int temp;
temp = A[i];
A[i]= A[i+1];
A[i+1] =temp;
changed=true;
}
}
}while(changed);
System.out.println("Your array after swapping");
for (int i = 0; i < A.length; i++) {
System.out.println(A[i]);
}
}
}
You are getting ArrayIndexOutOfBoundsException because if you enter to be less than 10 it will throw an exception.
for (int i = 0; i < 10; i++) {
A[i]=in.nextInt();
}
Used instead
for (int i = 0; i < count; i++) {
A[i]=in.nextInt();
}
Related
This is a program to sort the user input in ascending and descending order, while I am giving input using spaces I want the output to be displayed in same manner either with spaces or comma.
import java.util.*;
class sort4a {
Scanner input = new Scanner(System.in);
int num, i;
int arr[];
int temp = 0;
public void getdata() {
System.out.print("\nEnter the size of array: ");
num = input.nextInt();
arr = new int[num];
System.out.print("\nEnter the number: ");
for (i = 0; i < num; i++) {
arr[i] = input.nextInt();
}
}
void putdata() {
System.out.print("\n\nGiven numbers are: ");
for (i = 0; i < num; i++) {
System.out.print(arr[i]);
}
}
void asce() {
for (i = 0; i < num; i++) {
for (int j = i + 1; j < num; j++) {
if (arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.print("\n\nAscending order of number are: ");
for (int i = 0; i < num; i++) {
System.out.print(arr[i]);
}
}
void desc() {
for (i = 0; i < num; i++) {
for (int j = i + 1; j < num; j++) {
if (arr[i] < arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.print("\n\nDescending order of number are: ");
for (int i = 0; i < num; i++) {
System.out.print(arr[i]);
}
}
public static void main(String args[]) {
sort4a ob = new sort4a();
ob.getdata();
ob.putdata();
ob.asce();
ob.desc();
}
}
My output is this I want spacing in this output like I am giving space in input
Please suggest correction if my code is wrong.
Pretty easy to do. Just add a + ", " to every System.out.print() where you output the array. Also please start using more indentations. It makes it much easier to read for everybody.
import java.util.*;
class sort4a {
Scanner input = new Scanner(System.in);
int num, i;
int arr[];
int temp = 0;
public void getdata() {
System.out.print("\nEnter the size of array: ");
num = input.nextInt();
arr = new int[num];
System.out.print("\nEnter the number: ");
for (i = 0; i < num; i++) {
arr[i] = input.nextInt();
}
}
void putdata() {
System.out.print("\n\nGiven numbers are: ");
for (i = 0; i < num; i++) {
System.out.print(arr[i] + ", ");
}
}
void asce() {
for (i = 0; i < num; i++) {
for (int j = i + 1; j < num; j++) {
if (arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.print("\n\nAscending order of number are: ");
for (int i = 0; i < num; i++) {
System.out.print(arr[i] + ", ");
}
}
void desc() {
for (i = 0; i < num; i++) {
for (int j = i + 1; j < num; j++) {
if (arr[i] < arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.print("\n\nDescending order of number are: ");
for (int i = 0; i < num; i++) {
System.out.print(arr[i] + ", ");
}
}
public static void main(String args[]) {
sort4a ob = new sort4a();
ob.getdata();
ob.putdata();
ob.asce();
ob.desc();
}
}
I'm trying to reverse the array; i input length of array than it's all values. After that i'm trying to reverse it doing like that: a[i] = a[al - i] in the "for construction" - for(int i = o; i < al; i++);
What am i doing wrong there? The full code:
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s1 = new Scanner(System.in);
int al = s1.nextInt();
int a[] = new int[al];
for (int i = 0; i < al; i++) {
a[i] = s1.nextInt();
}
for(int i = 0; i < al; i++) {
a[i] = a[al - i];
}
for(int i = 0; i < al; i++) {
System.out.println(a[i]);
}
}
}
Problem Description of Your program:
for(int i = 0; i < al; i++) {
//let al=5, so index of a will be 0..4
//so for first iteration i=0
//so a[5-0] i.e: a[5] where 5 is index out of bound
a[i] = a[al - i];
}
Solution of the problem, but this logic will not reverse your array data:
for(int i = 0,j=al-1; i < al; i++,j--) {
a[i] = a[j];
}
For reversing your array data, you have to be used like this:
for (int i = 0, j = al - 1; i < j; i++, j--) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
So, Whole solution,
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s1 = new Scanner(System.in);
int al = s1.nextInt();
int a[] = new int[al];
for (int i = 0; i < al; i++) {
a[i] = s1.nextInt();
}
for (int i = 0, j = al - 1; i < j; i++, j--) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
for (int i = 0; i < al; i++) {
System.out.println(a[i]);
}
}
}
This question already has answers here:
What is a stack trace, and how can I use it to debug my application errors?
(7 answers)
Closed 7 years ago.
import java.util.Scanner;
public class Sort {
public void Countsort(int a[], int b[], int k) throws ArrayIndexOutOfBoundsException {
int[] c = new int[k + 1];
for (int i = 0; i < k; i++) {
c[i] = 0;
}
for (int i = 0; i <= a.length; i++) {
c[a[i]] = c[a[i]] + 1;
}
for (int i = 1; i <= k; i++) {
c[i] = c[i] + c[i - 1];
}
for (int i = a.length; i <= 1; i--) {
b[c[a[i]]] = a[i];
c[a[i]] = c[a[i]] - 1;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num;
int[] temp = new int[10000];
int i = 0;
while (sc.hasNextInt()) {
num = sc.nextInt();
temp[i] = num;
i++;
if (num == -1) {
break;
}
}
int A[] = new int[i];
// just a check
for (i = 0; i < temp.length; i++) {
System.out.println("temp values:" + temp[i]);
}
// just a check ends
for (int j = 0; j < A.length; j++) {
A[j] = temp[j];
System.out.println("tem copied vals:" + A[j]);
}
// a check for gthat a has temp values..
int[] B = new int[A.length];
new Sort().Countsort(A, B, 100);
for (i = 0; i < B.length; i++) {
System.out.println("Run count #" + i + " : " + B[i]);
}
}
}
First of all your while loop should be
while (sc.hasNextInt()) {
num = sc.nextInt();
if (num == -1) {
break;
}
// so that you can stop -1 to be stored
temp[i] = num;
i++;
}
Next thing is your loop
for (int i = 0; i < a.length; i++) { // always less than length of the array
c[a[i]] = c[a[i]] + 1;
}
How do I fix this error and what does it mean?
java.lang.ArrayIndexOutOfBoundsException: 5
at Sort.sort(Sort.java:29)
at Sort.<init>(Sort.java:13)
at SortedArray.<init>(SortedArray.java:23)
Here is the code:
import java.util.Scanner;
import java.util.Random;
public class SortedArray
{
Scanner input = new Scanner(System.in);
int [] Array;
Sort sortedArray;
int sizeOfArray;
public SortedArray()
{
System.out.print("Enter the number of values to put in the array: ");
sizeOfArray = input.nextInt();
Array = new int [sizeOfArray];
System.out.println("");
for(int i = 0; i < sizeOfArray; i++)
{
Random r = new Random();
Array[i] = r.nextInt(100) + 1;
System.out.println(Array[i]);
}
sortedArray = new Sort(Array, sizeOfArray);
sortedArray.display();
}
}
public class Sort
{
int[] array;
int sizeOfArray;
public Sort(int[] oldArray, int sizeOfOldArray)
{
sizeOfArray = sizeOfOldArray;
array = new int [sizeOfArray];
for( int i = 0; i < sizeOfArray; i++)
{
array[i] = oldArray[i];
}
sort();
}
public void display()
{
for ( int i = 0; i < sizeOfArray; i++){
System.out.println(array[i]);
}
}
private void sort()
{
for (int i = 0; i < sizeOfArray; i++)
{
for (int j = 0; j < sizeOfArray; i++)
{
if (array[j] < array[i])
{
swap(i,j);
}
}
}
}
private void swap(int x, int y)
{
int temp;
temp = array[x];
array[x] = array[y];
array[y] = temp;
}
}
I get the error when I run the program and enter the value. The program is supposed to sort the numbers from greatest to least. I'm not sure what is wrong.
First, what it means: you have an array and are trying to acces an index that is outside its range (below 0 or bigger or equal than the length of the array).
The probable cause is:
for (int j = 0; j < sizeOfArray; i++)
Notice that you check that j does not get too big but you are increasing i.
The Problem is that the inner loop also incrementsi which isn't correct in this situation!
private void sort()
{
for (int i = 0; i < sizeOfArray; i++)
{
for (int j = 0; j < sizeOfArray; j++)
{
if (array[j] < array[i])
{
swap(i,j);
}
}
}
}
Your problem is on this line
for (int j = 0; j < sizeOfArray; i++)
it should be
for (int j = 0; j < sizeOfArray; j++)
This is my code for the Bubble Sort. I cannot get the actual sorted values to output. The program reads the inputted numbers, but does not print it sorted.
I'm not sure what I have to do to make them sort.
Any advice or suggestions would be helpful.
package sortingalgorithm2;
import java.util.Scanner;
public class Main {
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
Scanner read = new Scanner (System.in);
int[] num = new int[15];
int size = 15;
System.out.println("Enter 15 numbers: ");
for (int i=0; i <= size-1; i++)
{
num[i] = read.nextInt();
}
for (int i=0; i <= size-1; i++)
{
if (num[i] >=1 && num[i] <= 1000)
{
System.out.println("The numbers you entered are: ");
System.out.println(+num[0]);
System.out.println(+num[1]);
System.out.println(+num[2]);
System.out.println(+num[3]);
System.out.println(+num[4]);
System.out.println(+num[5]);
System.out.println(+num[6]);
System.out.println(+num[7]);
System.out.println(+num[8]);
System.out.println(+num[9]);
System.out.println(+num[10]);
System.out.println(+num[11]);
System.out.println(+num[12]);
System.out.println(+num[13]);
System.out.println(+num[14]);
}
else
{
System.out.println("Data input is invalid. Enter a number between "
+
"1 and 1000.");
break;
}
}
BubbleSort (num);
for (int i=0; i < num.length; i++)
{
System.out.println("The sorted numbers are: ");
System.out.print(num[i]+ " ");
}
}
private static void BubbleSort(int[] num)
{
for (int i=0; i <= num.length; i++)
for (int x=1; x <= num.length; x++)
if (num[x] > num[x+1])
{
int temp = num[x];
num[x] = num[x+1];
num[x+1] = temp;
}
}
}
Try this Bubble sort :
private static void BubbleSort(int[] num) {
for (int i = 0; i < num.length; i++) {
for (int x = 1; x < num.length - i; x++) {
if (num[x - 1] > num[x]) {
int temp = num[x - 1];
num[x - 1] = num[x];
num[x] = temp;
}
}
}
}
You are printing the actual numbers in the order the user entered. Try this instead:
int[] sortedNumbers = new int[15];
sortedNumbers = BubbleSort (num);
for (int i=0; i < sortedNumbers.length; i++)
{
System.out.println("The sorted numbers are: ");
System.out.print(sortedNumbers[i]+ " ");
}
public static int[] BubbleSort(int [] num)
{
int temp;
for (int i=1; i<num.length; i++)
{
for(int j=0; j<num.length-i; j++)
{
if (num[j] > num [j+1])
{
temp = num [j];
num [j] = num [j+1];
num [j+1] = temp;
}
}
}
return num;
}
Try this :
for (int i = 0; i < num.length; i++) {
for (int j = i + 1; j < num.length; j++) {
if (num[i] > num[j]) {
num[i] = num[i] + num[j] - (num[j] = num[i]);
}
}
}
you are passing the array variable num (which is not static) to BubbleSort()(which does not returns a value and shadows the global num variable with its own) and trying to use the same num variable to access your sorted array from your main method which is not right.
The genuine fix to this is to declare your variable num as static just before the main method( in the class declaration). So I have made the changes in the program and here is the solution.
import java.util.Scanner;
public class sol {
static int num [] =new int [15]; //declaring num as static in the class definition.
public static void main(String[] args)
{
Scanner read = new Scanner (System.in);
int size = 15;
System.out.println("Enter 15 numbers: ");
for (int i=0; i <= size-1; i++)
{
num[i] = read.nextInt();
}
read.close();
/*for (int i=0; i <= size-1; i++)
{
if (num[i] >=1 && num[i] <= 1000)
{
System.out.println("The numbers you entered are: ");
System.out.println(+num[0]);
System.out.println(+num[1]);
System.out.println(+num[2]);
System.out.println(+num[3]);
System.out.println(+num[4]);
System.out.println(+num[5]);
System.out.println(+num[6]);
System.out.println(+num[7]);
System.out.println(+num[8]);
System.out.println(+num[9]);
System.out.println(+num[10]);
System.out.println(+num[11]);
System.out.println(+num[12]);
System.out.println(+num[13]);
System.out.println(+num[14]);
}
else
{
System.out.println("Data input is invalid. Enter a number between "
+
"1 and 1000.");
break;
}
}*/ //I have disabled this just to check with the sort method.
BubbleSort ();//no need to pass the array as it is static and declared as a //class variable hence can be used to by all the methods of that class
System.out.println("The sorted numbers are: ");
for (int i=0; i < num.length; i++)
{
System.out.print(num[i]+ " ");
}
}
private static void BubbleSort()
{
for (int i=0; i < num.length; i++)// required changes in the looping
for (int x=0; x < num.length-i-1; x++)
if (num[x] > num[x+1])
{
int temp = num[x];
num[x] = num[x+1];
num[x+1] = temp;
}
}
}