Question
Java ((without using array))
3. Write a program that reads a 7-digit number and then shuffles its digits randon Sample run: Please enter a number 123456 Wrong too small! Sample run: Please enter a number: 1234567 The shuffled number is: 4356271 Sample run: Please enter a number 1000001 The shuffled number is: 0100100
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.concurrent.ThreadLocalRandom;
import java.util.Scanner;
   public class ShuffleNumber {
        public static void main(String[] args) {
            ShuffleNumber sn = new ShuffleNumber(); // Create shuffle object
            Scanner reader = new Scanner(System.in); // Reading from System.in
            System.out.print("Please enter a number: ");
            String number = reader.nextLine(); //Read input from user
            if(number.length() < 7)
            {   //If input number length is less than 7 then return error
                System.out.println("Wrong too small !");
                return;
            }
            else if(number.length() > 7)
            {   //If input number length is greater than 7 then return error
                System.out.println("Wrong too large !");
                return;
            }
            String shuffled = sn.shuffle(number); //Call shuffle function
            System.out.println("The Shuffled number is: "+shuffled); // Display the result
        }
  
         private String shuffle(String number) {
            String shuffledNumber = number; // start with original number
            int numSize = number.length(); // Get the number length
            int shuffleCount = 7; // Shuffle randomly 7 times
            for(int i=0;i<shuffleCount;i++) {
                //swap letters in two indexes
                int pos1 = ThreadLocalRandom.current().nextInt(0, numSize); //Get first random position
                int pos2 = ThreadLocalRandom.current().nextInt(0, numSize); //Get second random position
                shuffledNumber = swapCharacters(shuffledNumber,pos1,pos2); //Get the result shuffled number by swapping characters
            }
            return shuffledNumber; //Return the shuffled number
        }
  
      private String swapCharacters(String shuffledNumber, int pos1, int pos2) {;
            //If both positions are same or position1 is more than position2 or position 2 is the last digit, then return without doing any changes
            if(pos1 > pos2 || pos1 == pos2 || pos2 >= shuffledNumber.length()-1)
                return shuffledNumber;
            String s1 = shuffledNumber.substring(0, pos1); //Else get number upto first position
            String s2 = shuffledNumber.substring(pos1+1, pos2); //Get number in between pos1 and pos2
            String s3 = shuffledNumber.substring(pos2+1); //Get number from pos2 to end
            return s1+shuffledNumber.charAt(pos2)+s2+shuffledNumber.charAt(pos1)+s3; // Add all strings and swap pos1 with pos2
        }
   }

Please enter a number: 1234567 The Shuffled number is: 5472136 Please enter a number: 1900001 The Shuffled number is: 0100001

Note: Please note that the shuffle is being done randomly. So each time you can get different output even if the input is same. And I have added check to do shuffle only for 7 digits number. As you want number 0 to be shuffled also, so I am doing operations on string and didn't use array anywhere in program. Thanks. Ping me back for any doubts.

Add a comment
Know the answer?
Add Answer to:
Java ((without using array)) 3. Write a program that reads a 7-digit number and then shuffles...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem 1: Write a program that reads a positive float number and displays the previous and...

    Problem 1: Write a program that reads a positive float number and displays the previous and next integers. Sample Run: Enter a float: 3.5 The previous and next integers are 3 and 4. Problem 2: Write a program that reads two integers and displays their sum, difference, product, and the result of their division. Sample Run: Enter two integers: 8 5 Sum: 8, Difference: 3, Product: 40, Division: 1.6 Problem 3: Write a program that reads a three-digit integer from...

  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

  • Write a java program for the following: Your program reads an infix expression represented by a...

    Write a java program for the following: Your program reads an infix expression represented by a string S from the standard input (the keyboard). Then your program converts the infix expression into a postfix expression P using the algorithm. Next, your program evaluates the postfix expression P to produce a single result R. At last, your program displays the original infix expression S, the corresponding postfix expression P and the final result R on the standard output ( the screen...

  • Write an application in java that reads in a five-digit integer and determines whether it is...

    Write an application in java that reads in a five-digit integer and determines whether it is a palindrome. If the number is not five digits long, display an error message and allow the user to enter a new value. Tips: 1. Determine the number of digits in the value input by the user , i.e. prompt the user to enter the number of digits of the number. Use a while loop to determine whether the user input contains the proper...

  • Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the...

    Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the opposite of what you have done in Assignment 4). Make sure that you create a new Project and Java class file for this assignment. Your file should be named “Main.java”. You can read about octal-to-decimal number conversions on wikepedia Assignment 4 is at the bottom Objective Your program should prompt the user to enter a number of no greater than 8 digits. If the...

  • In Java Write a program that reads an arbitrary number of 25 integers that are positive...

    In Java Write a program that reads an arbitrary number of 25 integers that are positive and even. The program will ask the user to re-enter an integer if the user inputs a number that is odd or negative or zero. The inputted integers must then be stored in a two dimensional array of size 5 x 5. Please create 3 methods: 1. Write a method public static int sum2DArray( int [1] inputArray ) The method sums up all elements...

  • java for netbeans Question 2: (Print distinct numbers) Write a program that reads in ten numbers...

    java for netbeans Question 2: (Print distinct numbers) Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly one space (i.e., if a number appears multiple times, it is displayed only once). (Hint: Read a number and store it to an array if it is new. If the number is already in the array, ignore it. After the input, the array contains the distinct numbers. Here is the...

  • In the Java language APCS Sorting Numbers Lab Write a program that reads in 3 floating-point...

    In the Java language APCS Sorting Numbers Lab Write a program that reads in 3 floating-point numbers (decimal numbers) and prints the three numbers in sorted order from smallest to largest. In your main method, create a scanner and get the 3 numbers from the user. Create a separate method called sort that accepts the 3 numbers prints them in the appropriate order. Sample Run#1 Please enter first number: 4 Please enter second number: 9 Please enter third number: 2.5...

  • ***USING PYTHON***Write a program that reads a five-digit positive integer and breaks it into a sequence...

    ***USING PYTHON***Write a program that reads a five-digit positive integer and breaks it into a sequence of individual digits. for example , thin put 16384 is displayed as: 1 6 3 8 4

  • Write the following program in Java: You are to write a Java program that reads an...

    Write the following program in Java: You are to write a Java program that reads an input file containing a list of words and their synonyms. You will repeatedly ask the user if they want to look up the synonyms of a word, add a new synonym for a word, or quit. Here is a sample run of the program (note that the program is expecting the input file, thesaurus.txt to be in the working directory for the code.): Enter...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT