Reputation: 1787
I'm having a bit of trouble figuring out how I should approach this problem even with some pseudo code that the book gives as hints ( This chapter is about arrays and arrayLists). By the way this isn't homework, and I am self learning.
Problem
A run is a sequence of adjacent repeated values. Write a program that generates a sequence of 20 random die tosses and that prints the die values, marking the runs by including them in parentheses, like this: 1 2 (5 5) 3 1 2 4 3 (2 2 2 2) 3 6 (5 5) 6 3 1
Hint in Book
Set a boolean variable inRun to false.
For each valid index i in the array list If inRun
If values[i] is different from the preceding value Print ) inRun = False Else
If values[i] is the same as the following value Print ( inRun = True Print Values; [i]
What I don't get is how the hint is really helping. Say we have multiple numbers like 2 2 2 2
Wont this just do something like (2 2) 2 2
Heres what I am trying so far
import java.util.ArrayList;
import java.util.Random;
class seq
{
private ArrayList<Integer> nums;
private Random randNum;
private boolean inRun;
public seq()
{
nums = new ArrayList<Integer>();
randNum = new Random();
inRun = false;
}
public void addToArrList()
{
for(int i = 0; i < 20 ; i++)
{
int addThisNum = randNum.nextInt(20)+1;
nums.add(addThisNum);
}
}
public void checkSides()
{
int count = 0;
for(int i = 0 ; i < nums.size(); i++)
{
if(nums.get(i) != nums.get(i-1))
{
inRun = false;
}
if(nums.get(i) == nums.get(i+1))
{
inRun = true;
}
}
}
public String toString()
{
String output = "Array is:";
if (output.)
}
}
ANOTHER ATTEMPT
import java.util.Random;
import java.util.ArrayList;
class Seq
{
private ArrayList<Integer> nums;
private Random randNum;
public Seq()
{
nums = new ArrayList<Integer>();
randNum = new Random();
}
public void fillArrList()
{
for (int i = 0 ; i < 20 ; i++)
{
int thisRandNum = randNum.nextInt(20)+1;
nums.add(thisRandNum);
}
}
public String toString() {
StringBuilder result = new StringBuilder();
boolean inRun = false;
for (int i = 0; i < nums.size(); i++) {
if (i < nums.size() - 1 && nums.get(i).equals(nums.get(i + 1))) {
if (!inRun) {
result.append("(");
}
result.append(nums.get(i));
inRun = true;
} else {
result.append(nums.get(i));
if (inRun) {
result.append(")");
}
inRun = false;
}
}
return result.toString();
}
}
public class Sequence{
public static void main(String [] args)
{
Seq seqObj = new Seq();
seqObj.fillArrList();
System.out.println(seqObj.toString());
}
}
OUTPUT
85641520612614320473181113612
201362181920141020(1919)514920162914
ETC
Upvotes: 0
Views: 440
Reputation: 366
I think they mean something like this:
@Override
public String toString() {
StringBuilder result = new StringBuilder();
boolean inRun = false;
for (int i = 0; i < nums.size(); i++) {
if (i < nums.size() - 1 && nums.get(i).equals(nums.get(i + 1))) {
if (!inRun) {
result.append("(");
}
result.append(nums.get(i));
inRun = true;
} else {
result.append(nums.get(i));
if (inRun) {
result.append(")");
}
inRun = false;
}
}
return result.toString();
}
Also you probably want to generate Randoms between 1 and 6, i.e.
int addThisNum = randNum.nextInt(6) +1;
Upvotes: 2
Reputation: 2048
Here randNum.nextInt(20)+1;
you are generating a random value from 0 to 20 you need to generate a random value which need to contain only one digits,
in the for loop i begin with zero in the first condition you call nums.get(i-1)
which is incorrect as i = 0 so i-1 will be -1
There more logic issues you can check them from the code above
package com.run;
import java.util.ArrayList;
import java.util.Random;
public class Run {
private ArrayList<Integer> nums;
private Random randNum;
private boolean inRun;
private StringBuffer sb;
public Run(){
nums = new ArrayList<Integer>();
randNum = new Random();
inRun = false;
sb = new StringBuffer();
}
public static void main(String[] args) {
Run run = new Run();
run.addToArrList();
run.checkSides();
}
public void addToArrList()
{
for(int i = 0; i < 20 ; i++)
{
int addThisNum = randNum.nextInt(9)+1;
nums.add(addThisNum);
}
}
public void checkSides()
{
int count = 0;
for(int i = 1 ; i < nums.size(); i++)
{
if(nums.get(i) != nums.get(i-1))
{
if(inRun)
sb.append(")");
inRun = false;
}
else
{
if(!inRun){
System.out.println(nums.get(i));
inRun = true;
sb.replace(sb.length()-1, sb.length(), "(" + nums.get(i-1));
}
}
sb.append(String.valueOf(nums.get(i)));
}
System.out.println(sb.toString());
}
}
Upvotes: 1