i have to write a program for generating 20 tosses of a die and then grouping them by placing parentheses around them like this:
1 3 ( 2 2 ) 3 5 2 ( 6 6 ) 5 ( 1 1 1 ) ( 4 4 ) 2 3 ( 5 5 ) 1

i have to have a seperate class to create the array, but in that class i have to create a method to mark the runs in parentheses...i have figured out how to do that, but it must be returned as a string and i cannot figure out how to do that...any help is appreciated...

here is what i have for my constructor class:

/**
This class marks "runs", i.e., adjacent repeated values, by
including them in parenthesis.

For example:
1 2 (5 5) 3 1 2 4 3 (2 2 2 2) 3 6 (5 5) 6 3 1
*/
public class Sequence
{
private int[] values;

public Sequence(int capacity)
{
values = new int[capacity];
}

/**
Returns the string of values, with runs enclosed in parentheses.
@return the string of values with the runs marked, for example 
"1 2 (5 5) 3 1 2 4 3 (2 2 2 2) 3 6 (5 5) 6 3 1"
*/
public String markRuns()
{
String answer = "";
for (int i = 1; i < values.length; i++)
{
if (values[i] == values[i-1] && values[i] == values[i+1])
answer = values[i] + "";

if (values[i] != values[i-1] && values[i] == values[i+1])
answer = "(" + values[i] + "";

if (values[i] != values[i-1] && values[i] == values[i+1])
answer = "" + values[i];

if (values[i] == values[i-1] && values[i] != values[i+1])
answer = "" + values[i] + ")";
}
}

and here is the main class and what it is calling for:

import java.util.Random;

/**
This class generates random die tosses and prints them out,
marking "runs", i.e., adjacent repeated values, by
including them in parenthesis.

For example:
1 2 (5 5) 3 1 2 4 3 (2 2 2 2) 3 6 (5 5) 6 3 1
*/
public class DieSequencePrinter
{
public static void main(String[] args)
{
final int TOSSES = 20;
Random die = new Random();
Sequence seq = new Sequence(TOSSES);

/* fill the array with random die tosses */
for (int i = 1; i <= TOSSES; i++)
{
seq.add(die.nextInt(6) + 1);
}

System.out.println(seq.markRuns());
}
}

>> i have figured out how to do that, but it must be returned as a string and i cannot figure out how to do that...

Seems to me that you'd just stick this line at the bottom of the markRuns function.

return answer;

But looking at the code, I question this statement: "i have figured out how to do that". I see no string concatenation in this function, which to me seems essential to the function. I'm wondering if perhaps you are using the = operator when you intend the += operator in many places.

answer = values[i] + "";

answer = "(" + values[i] + "";

answer = "" + values[i];

answer = "" + values[i] + ")";

are these 4 lines not concatenating the position in the array to a string? i did have

return answer;

in my original code, but took it out because i was getting a runtime error...

No, the first statement assigns a value to answer, the following 3 replace it.
You need something like

answer = answer + "(" + values[i] + ""; // + is String concatenation

which can be written more concisely as

answer += "(" + values[i] + "";

Your method signature is

public String markRuns()

so every possible path through that method must end in a

return <a string expression of some sort>;

eg

return answer;

@ JamesCherrill isn't that about String.valueOf(values[?]), because its touch Casting (justMyQuickHelicopterView)

According to the Language Ref 15.18.1
"Hello" + intArray
is evaluated as
"Hello" + (new Integer(intArray)).toString()
ie primitive types are boxed and the resultt of toString() on the boxed value is concatenated.

@JamesCherrill i see what you are saying with the += operator, it makes sense, i just didn't know that you could do that...so i changed my code to match (which had a "return answer;" there, i don't know why it didn't paste in)...i have run it in debug mode and am now getting a runtime error...i will post what i have changed to see what you all think...again, i appreciate the help on this...i have changed some the code from the first post...

here is the constructor class:

/**
   This class marks "runs", i.e., adjacent repeated values, by
   including them in parenthesis.

   For example:
      1 2 (5 5) 3 1 2 4 3 (2 2 2 2) 3 6 (5 5) 6 3 1
*/
public class Sequence
{
   private int[] arr;
   private int size;

   public Sequence(int capacity)
   {
      arr = new int[capacity];
      size = 0;
   }

   public void add(int value)
   {
      if (size < arr.length)
      {
         arr[size] = value;
         size++;
      }
   }

   /**
      Returns the string of values, with runs enclosed in parentheses.
      @return the string of values with the runs marked, for example 
      "1 2 (5 5) 3 1 2 4 3 (2 2 2 2) 3 6 (5 5) 6 3 1"
   */
   public String markRuns()
   {
	  String answer = "";
      for (int i = 1; i < arr.length; i++)
      {
    	  if (arr[i] != arr[i-1] && arr[i] == arr[i+1])
    		  answer += "(" + arr[i] + "";
    	  
    	  else if (arr[i] == arr[i-1] && arr[i] != arr[i+1])
    		  answer += ""  + arr[i] + ")";
    	  
    	  else
    		  answer += arr[i] + "";
      }
      return answer;
   }
}

here is a tester class:

public class SequenceTester
{
   public static void main(String[] args)
   {
      Sequence seq1 = new Sequence(5);
      seq1.add(3);
      seq1.add(3);
      seq1.add(4);
      seq1.add(5);
      seq1.add(5);
      System.out.println(seq1.markRuns());
      System.out.println("Expected: (3 3) 4 (5 5)");
   }
}

here is a tester class given by my professor:

import java.util.Random;

/**
   This class generates random die tosses and prints them out,
   marking "runs", i.e., adjacent repeated values, by
   including them in parenthesis.

   For example:
      1 2 (5 5) 3 1 2 4 3 (2 2 2 2) 3 6 (5 5) 6 3 1
*/
public class DieSequencePrinter
{
   public static void main(String[] args)
   {
      final int TOSSES = 20;
      Random die = new Random();
      Sequence seq = new Sequence(TOSSES);

      /* fill the array with random die tosses */
      for (int i = 1; i <= TOSSES; i++)
      {
         seq.add(die.nextInt(6) + 1);
      }

      System.out.println(seq.markRuns());
   }
}

So what's the full error message and which line is it on?

(but at a guess - you have an array for the data, you set its size based on the constructor, but loop
i = 1 to 20 regardless. and in that loop access element i+1 - a good way to create array reference errors)

So what's the full error message and which line is it on?

(but at a guess - you have an array for the data, you set its size based on the constructor, but loop
i = 1 to 20 regardless. and in that loop access element i+1 - a good way to create array reference errors)

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20
	at Sequence.markRuns(Sequence.java:39)
	at DieSequencePrinter.main(DieSequencePrinter.java:26)

Just as I suspected! See previous post.
(going offline now till tomorrow)

I follow the train of thought here right up until the error & and reasoning for it. What is the issue, or rather what is it that is creating the error code?

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.