How do I add space or comma in this output? - java

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();
}
}

Related

Having trouble searching array

So I have everything working fine up until the point in which I need to search. I'm really new to this so my code is probably awful I'm sorry in advance. Anyway, its a user input array, and the user should be able to search for a number in an array. Im getting the error for a duplicate variable on line 50 (int i, get 1).
import java.util.Scanner;
class SearchingSorting {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
System.out.println ("How many numbers would you like to input?");
int num = input.nextInt();
double[] array = new double[num];
for (int i = 0; i < num; i++) {
System.out.println ("Input number " + (1 + i) + ":");
array[i] = input.nextDouble();
}
for (double temp1 : array){
System.out.print (temp1 + "\t");
}
input.close();
int pass;
int i;
int hold;
for(pass = 1; pass < array.length; pass++)
{
for(i = 0; i < array.length - 1; i++)
{
if(array[i] > array[i+1])
{
hold = (int) array[i];
array[i] = array[i+1];
array[i+1] = hold;
}
}
System.out.println("\nSorted number is: ");
for(i = 0; i < array.length; i++)
System.out.print(" " + array[i]);
}
int i, get1;
Scanner keyboard = new Scanner(System.in);
int[] numbers = new int[10];
for(i = 0; i < numbers.length; i++)
{
numbers[i] = i * 10;
}
System.out.print("Enter search number: ");
get1 = keyboard.nextInt();
SearchMethod(numbers, get1);
}
public static void SearchMethod(int[] num, int get2)
{
int i ;
boolean j = false;
for(i = 0; i < num.length; i++)
{
if(num[i] == get2)
{
j = true;
break;
}
}
if(j == true)
System.out.println(get2 + " is found at num[" + i + "]");
else
System.out.println(get2 + " is not found in an array");
}
}
You are trying to declare a new variable with the same name ("i") in the same scope.
Rename your variable i on line 50.

ArrayIndexOutOfBoundsException - ArrayLength

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();
}

Descending order with string in constant position

I need to take input from the user and if the user enters string the program should keep integer in descending order and the position of the string should be in constant position. I am not being able to keep only integer in descending order.
public class sort {
public static void main(String[] args) {
int num;
int i, j, temp;
String nu;
int y;
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of character to be sort:");
String num = input.nextLine();
int length = num.length();
String result = "";
for (i = 0; i < length; i++) {
Character character = num.charAt(i);
if (Character.isDigit(character)) {
result += character;
}
}
int array[] = new int[num];
System.out.println("Enter " + num + " character ");
for (i = 0; i < num; i++)
array[i] = input.nextInt();
for (i = 0; i < (num - 1); i++) {
for (j = 0; j < num - i - 1; j++) {
if (array[j] < array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
System.out.println("Sorted list of integers:");
for (i = 0; i < num; i++)
System.out.println(array[i]);
}
}
I've made some modifications to your code that might help. For one, Integer.ParseInt works better than nextInt() because nextInt() leaves a carriage return on nextLine(). Also I feel a while loop would work better for your bubble sort algorithm.
import java.util.*;
public class sort {
public static void main(String[] args) {
int num;
int i, temp;
String nu;
int y;
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of character to be sort:");
num = Integer.ParseInt(input.nextLine());
int[] arr = new int[num];
System.out.println("Enter " + num + " character ");
for (i = 0; i < num; i++)
arr[i] = Integer.ParseInt(input.nextLine());
int swaps;
while (swaps == 0) {
swaps = 0;
for (i = 0; i < arr.Length; i++) {
if (arr[j] < arr[i + 1]) {
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
swaps++;
}
}
}
System.out.println("Sorted list of integers:");
for (i = 0; i < num; i++)
System.out.println(array[i]);
}
}
I changed a few things. First of all, you declared num twice, and had a String = input.nextInt() which doesn't match. The try...catch checks if it's an int input, otherwise it converts it from a char to an int. Your algorithm to put them in descending order worked fine, but populating the array wasn't working.
import java.util.InputMismatchException;
import java.util.Scanner;
public class sort {
public static void main(String []args) {
int i, j, temp;
int y;
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of character to be sort:");
String num = input.nextLine();
int length = Integer.parseInt(num);
String result = "";
int array[] = new int[length];
System.out.println("Enter " + num + " character ");
for (i = 0; i < length; i++) {
try{
array[i]=input.nextInt();
}catch(InputMismatchException e){
Character c = input.next().charAt(0);
array[i]=(int)(c.charValue());
}
}
for (i = 0; i < ( length- 1 ); i++) {
for (j = 0; j < length - i - 1; j++) {
if (array[j] < array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
System.out.println("Sorted list of integers:");
for (i = 0; i < length; i++)
System.out.println(array[i]);
}
}

Array Index out of bounds exception, when I run this, I am not able to figure out why. I am not able to view the sorted array [duplicate]

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 to make Bubble Sort in Java to output the sorted numbers?

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;
}
}
}

Categories

Resources