Hi guys, Just wondering if someone could help point out why the string value s1 in the for loop cannot be passed on to String input....
here is my code.

Please ignore the commented out parts. Cheers for any help

import java.io.*;
import java.util.*;
import java.lang.*;

public class gaelan3

{
  static int size;
  static int count;
  static char[] charArray;

  public static void main(String[] args) throws IOException
  {

	/*System.out.println("Please enter letters for anagram problem to be solved. Thankyou");

	BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
	String s = read.readLine();
	String input = s;
	*/

	String validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	Random r = new Random();


	for(int i=1; i<10; i++)
			  {  char randomChar = validChars.charAt(r.nextInt(validChars.length()));
			     String s1 = Character.toString(randomChar);
			     //System.out.println(s1);

			  }


	String input = s1;

    size = input.length();
    count = 0;
    charArray = new char[size];
    for (int j = 0; j < size; j++)
   	  charArray[j] = input.charAt(j);
    doAnagram(size);
  }

  public static void doAnagram(int newSize)
  {
    int limit;
    if (newSize == 1) // if too small, return;
      return;
    // for each position,
    for (int i = 0; i < newSize; i++) {
      doAnagram(newSize - 1); // anagram remaining
      if (newSize == 2) // if innermost,
        display();
      rotate(newSize); // rotate word
    }
 }

  // rotate left all chars from position to end
  public static void rotate(int newSize)
  {
    int i;
    int position = size - newSize;
    // save first letter
    char temp = charArray[position];
    //shift others left
    for (i = position + 1; i < size; i++)
      charArray[i - 1] = charArray[i];
    //put first on right
    charArray[i - 1] = temp;
  }

  public static void display()
  {
    System.out.print(++count + " ");
    for (int i = 0; i < size; i++)
      System.out.print(charArray[i]);
    System.out.println();
  }
}

Recommended Answers

All 13 Replies

The compiler error should be enough to help you fix this. Read it carefully.
Focus on the part: "Cannot resolve symbol s1"

Hi.

for(int i=1; i<10; i++)
{ char randomChar = validChars.charAt(r.nextInt(validChars.length()));
String s1 = Character.toString(randomChar);
//System.out.println(s1);
}
String input = s1;

You variable s! is declard inside the loop for so it is not known outside;
So if you tray to declare s! in some place where it can be known.

When i declare the s1 outside the loop I get a compiler error saying for..

String input = s1;

Saying that s1 may not have been intiliazed. So I assume for some reason its not reconizing it in the loop then.

So you have to initialize it like that
String s1= null;

Yes that would compile fine but it does not allow for the program to work properly as...

String input = s1; (reconizes s1 is null)...

I assume as to the first comment by JavaAddict, that it has something to do with returning a value from inside the loop so passing it as an object or am I way off base....

Why don't you post the code that compiles

This is the code that compiles....

import java.io.*;
import java.util.*;
import java.lang.*;

public class gaelan3

{
  static int size;
  static int count;
  static char[] charArray;

  public static void main(String[] args) throws IOException
  {



	String s1= null;

	/*System.out.println("Please enter letters for anagram problem to be solved. Thankyou");

	BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
	String s = read.readLine();
	String input = s;
	*/

	String validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	Random r = new Random();


	for(int i=1; i<10; i++)
			  {  char randomChar = validChars.charAt(r.nextInt(validChars.length()));
			      s1 = Character.toString(randomChar);

			     //System.out.println(s1);

			  }



	String input = s1;

    size = input.length();
    count = 0;
    charArray = new char[size];
    for (int j = 0; j < size; j++)
   	  charArray[j] = input.charAt(j);
    doAnagram(size);
  }

  public static void doAnagram(int newSize)
  {
    int limit;
    if (newSize == 1) // if too small, return;
      return;
    // for each position,
    for (int i = 0; i < newSize; i++) {
      doAnagram(newSize - 1); // anagram remaining
      if (newSize == 2) // if innermost,
        display();
      rotate(newSize); // rotate word
    }
 }

  // rotate left all chars from position to end
  public static void rotate(int newSize)
  {
    int i;
    int position = size - newSize;
    // save first letter
    char temp = charArray[position];
    //shift others left
    for (i = position + 1; i < size; i++)
      charArray[i - 1] = charArray[i];
    //put first on right
    charArray[i - 1] = temp;
  }

  public static void display()
  {
    System.out.print(++count + " ");
    for (int i = 0; i < size; i++)
      System.out.print(charArray[i]);
    System.out.println();
  }
}

Try to print the value s1 from inside the loop.
Then print the value of s1 and input after the assignment.

Im not sure I understand what you mean.

I can print the the String s1 with the set from inside the loop but what I want to do is produce all the possible combinations of the result of s1. Then print them to the screen.

I dont understand what you mean by this

Then print the value of s1 and input after the assignment.

Im not sure I understand what you mean.

I can print the the String s1 with the set from inside the loop but what I want to do is produce all the possible combinations of the result of s1. Then print them to the screen.

I dont understand what you mean by this

import java.io.*;
import java.util.*;
import java.lang.*;

public class gaelan3

{
  static int size;
  static int count;
  static char[] charArray;

  public static void main(String[] args) throws IOException
  {



	String s1= null;

	/*System.out.println("Please enter letters for anagram problem to be solved. Thankyou");

	BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
	String s = read.readLine();
	String input = s;
	*/

	String validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	Random r = new Random();


	for(int i=1; i<10; i++)
			  {  char randomChar = validChars.charAt(r.nextInt(validChars.length()));
			      s1 = Character.toString(randomChar);

			     System.out.println("s1: "+s1);

			  }



	String input = s1;
			     System.out.println("input: "+input);
    size = input.length();
    count = 0;
    charArray = new char[size];
    for (int j = 0; j < size; j++)
   	  charArray[j] = input.charAt(j);
    doAnagram(size);
  }

Thanks for the help but this still doesnt work. The only output I wont is the output produced at the bottom of the program from the following code....

public static void display()
  {
    System.out.print(++count + " ");
    for (int i = 0; i < size; i++)
      System.out.print(charArray[i]);
    System.out.println();
  }

What I want to do is put the random String produced into the following code via string input.. Then print all the combinations of the random string

String input = s1;
    System.out.println("input: "+input);
    size = input.length();
    count = 0;
    charArray = new char[size];
    for (int j = 0; j < size; j++)
   	  charArray[j] = input.charAt(j);
    doAnagram(size);
  }

  public static void doAnagram(int newSize)
  {
    int limit;
    if (newSize == 1) // if too small, return;
      return;
    // for each position,
    for (int i = 0; i < newSize; i++) {
      doAnagram(newSize - 1); // anagram remaining
      if (newSize == 2) // if innermost,
        display();
      rotate(newSize); // rotate word
    }
 }

  // rotate left all chars from position to end
  public static void rotate(int newSize)
  {
    int i;
    int position = size - newSize;
    // save first letter
    char temp = charArray[position];
    //shift others left
    for (i = position + 1; i < size; i++)
      charArray[i - 1] = charArray[i];
    //put first on right
    charArray[i - 1] = temp;
  }

We know that it doesn't run. The code I told you to add was so that we can see what are the values of the variables so we can figure out what is the problem.
You changed the code and instead of posting the results we keep repeating that it doesn't work.

You complained about printing null.
After you added the code, what does it print?

Hi figured out what was wrong with my code.... Thanks for the support guys but here is my final code if anyone wants to see the finished product....

import java.io.*;
import java.util.*;
import java.lang.*;

public class program3

{
  static int size;
  static int count;
  static char[] charArray;
  static Scanner in;

  public static void main(String[] args) throws IOException
  {

	in = new Scanner(System.in);

	String s1= null;
	String temp = "";
	String wait = "";

	String validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	Random r = new Random();


	for(int i=0; i<9; i++)
			  {
				  char randomChar = validChars.charAt(r.nextInt(validChars.length()));
			      s1 = Character.toString(randomChar);
				  temp = s1 + temp;
				  System.out.print(s1);		
			  }
	

	System.out.println("\nPress Y then enter to continue");		    
	wait = in.next();
	System.out.println(temp);
	String input = temp;
   
	size = input.length();
    count = 0;
    charArray = new char[size];
    for (int j = 0; j < size; j++)
   	  charArray[j] = input.charAt(j);
    doAnagram(size);
	
  }

  public static void doAnagram(int newSize)
  {
    int limit;
    if (newSize == 1) // if too small, return;
      return;
    // for each position,
    for (int i = 0; i < newSize; i++) {
      doAnagram(newSize - 1); // anagram remaining
      if (newSize == 2) // if innermost,
        display();
      rotate(newSize); // rotate word
    }
 }

  // rotate left all chars from position to end
  public static void rotate(int newSize)
  {
    int i;
    int position = size - newSize;
    // save first letter
    char temp = charArray[position];
    //shift others left
    for (i = position + 1; i < size; i++)
      charArray[i - 1] = charArray[i];
    //put first on right
    charArray[i - 1] = temp;
  }

  public static void display()
  {
    System.out.print(++count + " ");
    for (int i = 0; i < size; i++)
      System.out.print(charArray[i]);
    System.out.println();
  }
}
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.