hello,


can you help me please how can i random words and displaying it with the length of the string...example if the user inputed "stamp" then it will random the letters and display it 5 times...

example:

input : stamp

output: pmast
pmast
stamp
mastp
sptma


can you help me please...i don't have idea on this hoping for your positive responds..thank you in advance...

Recommended Answers

All 31 Replies

You can get any of the characters in the string with String.charAt(), so all you need is to generate lists of integers. The lists should use each of the integers (0..length-1) exactly one time.

So you need a method that returns a randomly shuffled list of integers from (0..n). You can probably write that. Don't worry about finding the best algorithm, just get something that meets the spec and then you can think about improving it.

You can get any of the characters in the string with String.charAt(), so all you need is to generate lists of integers. The lists should use each of the integers (0..length-1) exactly one time.

So you need a method that returns a randomly shuffled list of integers from (0..n). You can probably write that. Don't worry about finding the best algorithm, just get something that meets the spec and then you can think about improving it.

hello sir,

thanks for the reply...sir i have question do i make if statement after the forloop?please correct me if i am wrong...hoping for your positive responds...

public class Randomwords
	{	
		public static Scanner console = new Scanner(System.in);
		public static void main(String []args)
		{
			
			String name;
			
			Random ran = new Random();
			
		
	        
			for(int i=0;i<name.length();i++)
			{
			    
				if(name.charAt(i)==name.length()-1)
				{
					
				}
				
				
				
			}
			
		
				
			
		}
	}

I suggest you design how your program should work before you start writing code.
Have you done that yet?
Can you explain how the program will solve the problem, step by step?

The if statement in your code doesn't make sense. What is it supposed to do?

Um, I have no idea what you're trying to do here. You iterate over the characters in the string, and if each char is equal to the length of the string, you do nothing.

Listen to the voice of Norm, which should be echoing in the back of your head by now: Start with the logic. Work out how you're going to solve the problem first. Than - and only then - you can start writing code.

There, you see? :)

I suggest you design how your program should work before you start writing code.
Have you done that yet?
Can you explain how the program will solve the problem, step by step?

The if statement in your code doesn't make sense. What is it supposed to do?

hello sir,


thank you for the reply sir...sorry sir about this...i have made another code sir...it says n must be positive..please help me on this sir hoping for your positive responds..

String name;
			
			Random ran = new Random();
			
			System.out.println("Please input words");
			name=console.next();
		    
	        
			for(int i=0;i<name.length();i++)
			{
			   int swap=0; 
			   int t=name.length()-1;	 
			   swap = t;
			   t=swap;
			      
			   ran.nextInt(i);     
			    
			   
			   System.out.println(name);
			   
			   
				
				
			}
			
		
			
			
		}
	}

Before you try to write code, you need to design what the program steps are to solve the problem.

Can you write a list of the steps that are needed to solve this problem.
When we have that list to work from, then we'll work on the code.

There, you see? :)

okay sir i will do my best thank you for the reply...

Before you try to write code, you need to design what the program steps are to solve the problem.

Can you write a list of the steps that are needed to solve this problem.
When we have that list to work from, then we'll work on the code.

sir, the user will input string sample

sample run:

input: july
output:

yluj
lyju
july
jlyu


that is the output sir....if the characters are 4 then it will display for times in randomly...hoping for your positive responds...

Okay, you're spelling out your requirements. Do you require that the permutations be unique? Or would

yluj
lyju
yluj
jlyu

be acceptable output? It's good to get these things defined early.
This is different from working out the logic that will achieve the goal, but thinking in detail about what you want to have at the end is a good idea.

Before you try to write code, you need to design what the program steps are to solve the problem.

Can you write a list of the steps that are needed to solve this problem.
When we have that list to work from, then we'll work on the code.

the steps sir?


first determine the length of the string
second swapping of the characters
third displaying randomly...

am i correct with my steps sir?...please correct me if i am wrong...hoping for your positive responds...

Okay, you're spelling out your requirements. Do you require that the permutations be unique? Or would

yluj
lyju
yluj
jlyu

be acceptable output? It's good to get these things defined early.
This is different from working out the logic that will achieve the goal, but thinking in detail about what you want to have at the end is a good idea.

sir i just want only the output randomly and display....it's okay if july will be display again from the output because it is randomly... can you help me sir hoping for your positive responds...

swapping of the characters

Now break the swapping down into steps. That is the hardest part of the assignment.

Now break the swapping down into steps. That is the hardest part of the assignment.

sir can you give me idea on this

Now break the swapping down into steps. That is the hardest part of the assignment.

sir please correct me if i am wrong with this swapping procedure...hoping for your positive responds.

String name;
			
			Random ran = new Random();
			
			System.out.println("Please input words");
			name=console.next();
		    
	                int t;
	                int last=ran.nextInt(name.length()-1);
			for(int i=0;i<name.length();i++)
			{
			    
			    t= name.charAt(i);
			    t=last;
			    
			    last=t;
			   
			     
			 
			      System.out.println(name);
			   
			   
				
				
			}

Please write a description of what you want the code to do to solve your problem.
Talk about code after the design is done.

Please write a description of what you want the code to do to solve your problem.
Talk about code after the design is done.

sir i am confuse how can i swap the elements?

You can get any of the characters in the string with String.charAt(), so all you need is to generate lists of integers. The lists should use each of the integers (0..length-1) exactly one time.

So you need a method that returns a randomly shuffled list of integers from (0..n). You can probably write that. Don't worry about finding the best algorithm, just get something that meets the spec and then you can think about improving it.

hello sir can you help me what should i do can you give me some steps how can i accomplish this...

You can get any of the characters in the string with String.charAt(), so all you need is to generate lists of integers. The lists should use each of the integers (0..length-1) exactly one time.

So you need a method that returns a randomly shuffled list of integers from (0..n). You can probably write that. Don't worry about finding the best algorithm, just get something that meets the spec and then you can think about improving it.

hello sir please correct my code sir, i got problem it will display the index,how can i disaplay this into words..hoping for your positive repsonds...

String name;
			
			
			Random ran = new Random();
			
			System.out.println("input words");
			name = console.next();
			
			
			int first=0;
			int last=name.length()-1;
			for(int i=0;i<name.length();i++)
			{
			    last=ran.nextInt(name.length()-1);//in this part sir i am trying to swap the character but it's wrong i don't have idea how to swap characters...
			    int temp=0;
			    
			    temp=last;
			    
			    
				System.out.println(temp);.//this is wrong because i am displaying the index...
				
				
				
			}
			
			
			
			
			
			
		}

Yeah, that's not much help. Tell me what you're trying to do there. From line 10 through 20, what is each line of code supposed to do, and why?

Yeah, that's not much help. Tell me what you're trying to do there. From line 10 through 20, what is each line of code supposed to do, and why?

sir i'm sorry but i can't answer i really don't know what i'm doing...sir please help me give me the steps how to do this random words and displaying it the size of length..hoping for your positive responds...

First of all, stop writing random code. Throwing spare parts at each other will not produce an engine. You have to have some idea of what you want to do.

Now, suppose you want to rearrange the letters of a word, and you don't have a computer. Better yet, suppose you want to teach me to do it, and you know that I can't remember more than one thing at a time (unless I write it down, and even then I forget what it was there for unless I label it) but I can use tools and I can read instructions. I don't know Java or any other computer language.

The goal is for me to take a word - any word, we don't know which one it will be - and give back a sequence consisting of the letters in that word, each used exactly once. The sequence should not be predictable or repeated; if I do this a second time I should get a different sequence. I have a lot of paper and a pencil, and I might have other stuff lying around as well (ruler, scissors, tape) if it's needed.

What do I do?

You may do in the following way. Assume the word you are going to random has n characters. In the original character order of the word, each time you select one as the first letter and print it. And then print the rest of the letter (with their order) so that you may have n random words.

For example the word july has 4 characters.

we will have:
july
ujly
ljuy
yjul

A new random word would be printed in the following way.
Each run of the outer loop will do:
(1) select a letter from word in their original order in the word. Then print this letter as the first letter of each new random word.
(2) Then use a nested loop to print the rest letters. That is, print all the characters whose indexex are not the index of the selected letter.
In this way a newly created random word is printed.
The outloop runs n times so that n random words are printed


Hope you understand my idea.

You can get any of the characters in the string with String.charAt(), so all you need is to generate lists of integers. The lists should use each of the integers (0..length-1) exactly one time.

So you need a method that returns a randomly shuffled list of integers from (0..n). You can probably write that. Don't worry about finding the best algorithm, just get something that meets the spec and then you can think about improving it.

sir need help on swapping of character

int first=0;
   	    int last= name.length()-1;
   	  
   	    
   	          
   	          
   	    for(int i=0;i<name.length();i++)
   	    {
   	    	
   	    	while(first<last)
   	    	{
   	    	
   	    	   int t = name.charAt(first);
   	           
   	                name.charAt(first)= name.charAt(last);//i get error in this line what should i do in this line...
   	           	 
           	    	
   	    	}
   	    }

You get an error there because you can't assign to a function call.
name.charAt(first) returns a character. It doesn't point to memory, so you can't write to it.

You get an error there because you can't assign to a function call.
name.charAt(first) returns a character. It doesn't point to memory, so you can't write to it.

Hello sir,

for how many nights that i did not sleep, i think i got no the answer correct me if i am wrong...hoping for your positive responds...

char [] c = name.toCharArray();
		
		    
		    
	int forward=0;
		    
	for(int i=0;i<name.length();i++)
	{
		 
		
		        int n=ran.nextInt(name.length()-1);
		   	char temp = c[forward];
			c[forward]=c[n];
			c[n]=temp;
			 
		
		
		 
	
	 	System.out.println(c);
		
	}

You may do in the following way. Assume the word you are going to random has n characters. In the original character order of the word, each time you select one as the first letter and print it. And then print the rest of the letter (with their order) so that you may have n random words.

For example the word july has 4 characters.

we will have:
july
ujly
ljuy
yjul

A new random word would be printed in the following way.
Each run of the outer loop will do:
(1) select a letter from word in their original order in the word. Then print this letter as the first letter of each new random word.
(2) Then use a nested loop to print the rest letters. That is, print all the characters whose indexex are not the index of the selected letter.
In this way a newly created random word is printed.
The outloop runs n times so that n random words are printed


Hope you understand my idea.

hello sir i think i got it, and it works for me correct me if i am wrong...
hoping for your postive responds...

int forward=0;
		    
	
	for(int i=0;i<name.length();i++)
	{
		 
		
		int n=ran.nextInt(name.length()-1);
		char temp = c[forward];
		c[forward]=c[n];
		c[n]=temp;
		
	 	System.out.println(c);
		
	}

Very good. That's a perfect example of the classic swap. The only trouble is - what are you swapping? You go through the loop once for each letting in the string. What happens each time in the loop?

The swap is correct, it exchanges two items in the array, but I think you'll find that the objects you're swapping are not the ones you intended.


The bigger problem is, you're still giving us code when what you need is a plan for the code to follow. So how does this swap fit into the larger problem? What will you swap, and under what circumstances?

Very good. That's a perfect example of the classic swap. The only trouble is - what are you swapping? You go through the loop once for each letting in the string. What happens each time in the loop?

The swap is correct, it exchanges two items in the array, but I think you'll find that the objects you're swapping are not the ones you intended.


The bigger problem is, you're still giving us code when what you need is a plan for the code to follow. So how does this swap fit into the larger problem? What will you swap, and under what circumstances?

ahm sir it swap to the first element and last element of the string.then the last element will random...am i right sir?hoping for your positive responds...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.