Question

I need some help with Java. I need to write a Java program that generates a...

I need some help with Java. I need to write a Java program that generates a set of random strings from a given string of same length where no character is ever repeated and character belong to the original string. See example.

Example Input: “Hello World” (length 9)

Output: “World oHlel” (length 9)

0 0
Add a comment Improve this question Transcribed image text
Answer #1
/**
 * Title: Student
 * FileName: Student.java
 *
 * @author Next Sphere Technologies
 *         Copyright: Copyright Ⓒ Next Sphere Technologies 2019.
 * @version 1.0
 * @Created date: 2/15/2019
 */
import java.util.Scanner;
public class Student {

    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        String name = "";
        // reading input string
        name += sc.nextLine();
        int num = name.length();
        int array[] = new int[name.length()];
        String response = null;
        int i =1;
        double randomDouble = Math.random();
        randomDouble = randomDouble * num + 1;
        int randomInt = (int) randomDouble;
        response =  String.valueOf(name.charAt(randomInt));
        array[randomInt-1] = 1;
        while(i<num){
             randomDouble = Math.random();
            randomDouble = randomDouble * num + 1;
             randomInt = (int) randomDouble;
             if(array[randomInt-1]!=1){
                 response = response + name.charAt(randomInt-1);
                 i++;
               array[randomInt-1] = 1;
             }
        }
        //response
        System.out.println(response);
    }
}
Add a comment
Know the answer?
Add Answer to:
I need some help with Java. I need to write a Java program that generates a...
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
  • Write a Java program which takes a string as a user input. Create 2 functions. The...

    Write a Java program which takes a string as a user input. Create 2 functions. The first function expects a string as argument and returns void. It converts all upper case characters to lower case and converts all lower case characters to upper case. It then prints out the converted string to a console. The second function also expects a string as argument and returns void. It will find first charactor that is repeated exactly 2 times in the string....

  • Java Program Write a recursive method that takes a string and return a string where every...

    Java Program Write a recursive method that takes a string and return a string where every character appears twice. Output must match exactly as below: only two LL's since there is already two of the character For example, if the string is “HELLO”, it will return “HHEELLOO”. Write a program to test it. must be recursive!!!

  • JAVA QUESTION 2.One use of a Stack is to reverse the order of input. Write a...

    JAVA QUESTION 2.One use of a Stack is to reverse the order of input. Write a complete method that reads a series of Strings from the user. The user enters "end" to stop inputting words. Then, output the Strings in reverse order of how they were entered. Do not output the String “end”. Use a stack to accomplish this task. Invoke only the methods push, pop, peek, and isEmpty on the stack object. Here is an example of how the...

  • Need help with java programming. Here is what I need to do: Write a Java program...

    Need help with java programming. Here is what I need to do: Write a Java program that could help test programs that use text files. Your program will copy an input file to standard output, but whenever it sees a “$integer”, will replace that variable by a corresponding value in a 2ndfile, which will be called the “variable value file”. The requirements for the assignment: 1.     The input and variable value file are both text files that will be specified in...

  • java find and replace code pls help me We write code that can find and replace...

    java find and replace code pls help me We write code that can find and replace in a given text file. The code you write should take the parameters as command line arguments: java FindReplace -i <input file> -f "<find-string>" -r "<replace-string> -o <output file> *question mark(?) can be used instead of any character. "?al" string an be sal ,kal,val *In addition, a certain set of characters can be given in brackets.( "kng[a,b,f,d,s]ne" string an be kngane,hngbne,kangfne,kangdne,kangsne So, all you...

  • Using C++, and <iostream> if possible, ​ Write a program that can compress a text file...

    Using C++, and <iostream> if possible, ​ Write a program that can compress a text file consisting of English language alphabet A-zl using run-length encoding scheme. (Weight 50%) Run-Length Encoding The key idea is to replace the repeating character by itself and the number of times that it has been repeated. Consider the following string HELLO WORLD This can be re-written as HEL20 WORLD Notice that since LL was repeated, it was repeated by L2. 2 here indicates that letter...

  • I need some help on Java. I'm stuck on trying to generate a new string by...

    I need some help on Java. I'm stuck on trying to generate a new string by concatenating the reversed substrings of even indexes and odd indexes separately from a given string. I'm not sure how to figure this out. See example. Example: Input: abscacd Output: dasaccb Substrings: asad, bcc Reversed substrings: dasa, ccb.

  • Very difficult and confusing assignment for C++ need help! Write a program that prompts a user...

    Very difficult and confusing assignment for C++ need help! Write a program that prompts a user to enter a line of text (including whitespace). Please use string object NOT C-string, a string library can be included. 1. Convert the input into uppercase and output the string to the screen. 2. Get the first and the last characters then appends the line with "I Love C++" and output the string to the screen. Example: Input: Hello, How are you? Output 1:...

  • C++ help Format any numerical decimal values with 2 digits after the decimal point. Problem descr...

    C++ help Format any numerical decimal values with 2 digits after the decimal point. Problem description Write the following functions as prototyped below. Do not alter the function signatures. /// Returns a copy of the string with its first character capitalized and the rest lowercased. /// @example capitalize("hELLO wORLD") returns "Hello world" std::string capitalize(const std::string& str); /// Returns a copy of the string centered in a string of length 'width'. /// Padding is done using the specified 'fillchar' (default is...

  • python Programming Exercise 4.7 Write a script that decrypts a message coded by the method used...

    python Programming Exercise 4.7 Write a script that decrypts a message coded by the method used in Project 6. Method used in project 6: Add 1 to each character’s numeric ASCII value. Convert it to a bit string. Shift the bits of this string one place to the left. A single-space character in the encrypted string separates the resulting bit strings. An example of the program input and output is shown below: Enter the coded text: 0010011 1001101 1011011 1011011...

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