-2

Simply not printing the same output as the line above and I can't figure out why this is happening, I've noticed that it's printing the last N numbers from the end backwards, whatever i input into the parameter it prints that amount a second time.

Here's the main

public class main {
    public static void main(String args[]) {
    ScalesSolution s1 = new ScalesSolution(11);
    s1.println();
    ScalesSolution s2 = new ScalesSolution(s1.GetSol());
    s2.println();
}
}

Heres the ScalesSolution Class

import java.util.ArrayList;
import java.util.Random;

public class ScalesSolution {
private String scasol;

public void print() {
    System.out.print(scasol);
}

// Display the string with a new line
public void println() {
    print();
    System.out.println();
}



public String GetSol()
{
    return scasol;
}
}

Heres the randomOther Class

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

public class randomOther {
// Shared random object
static private Random rand;

// Create a uniformly distributed random integer between aa and bb inclusive
static public int UI(int aa, int bb) {
    int a = Math.min(aa, bb);
    int b = Math.max(aa, bb);
    if (rand == null) {
        rand = new Random();
        rand.setSeed(System.nanoTime());
    }
    int d = b - a + 1;
    int x = rand.nextInt(d) + a;
    return (x);
}

// Create a uniformly distributed random double between a and b inclusive
static public double UR(double a, double b) {
    if (rand == null) {
        rand = new Random();
        rand.setSeed(System.nanoTime());
    }
    return ((b - a) * rand.nextDouble() + a);
}
static public ArrayList<Double> ReadNumberFile(String filename) {
    ArrayList<Double> res = new ArrayList<Double>();
    Reader r;
    try {
        r = new BufferedReader(new FileReader(filename));
        StreamTokenizer stok = new StreamTokenizer(r);
        stok.parseNumbers();
        stok.nextToken();
        while (stok.ttype != StreamTokenizer.TT_EOF) {
            if (stok.ttype == StreamTokenizer.TT_NUMBER) {
                res.add(stok.nval);
            }
            stok.nextToken();
        }
    } catch (Exception E) {
        System.out.println("+++ReadFile: " + E.getMessage());
    }
    return (res);
}
}

Here is the issue the Output:

00101001010101101011001011010101101001011010001011010010101101001001011010010
01011010010

I believe that both outputs should be the same and I see that there is a problem here, not sure why they aren't

Galfi
  • 1
  • 4
  • 3
    Please provide a smaller example which still reproduces the error. Say, by removing some functions. Also, I see that you use `Random` in the code and seed it with current time, please get rid of randomness as well (e.g. by seeding it with constant data, or, even better, by removing dependency on random at all and using hard-coded consts). – yeputons Feb 03 '17 at 05:36
  • i am unsure on how to edit it like that :s – Galfi Feb 03 '17 at 05:41
  • 2
    Check out StackOverflow's tutorial on [MCVE](http://stackoverflow.com/help/mcve). – yeputons Feb 03 '17 at 05:45
  • 1
    If you don't know what the issue is, how do you expect anyone else to? What are you expecting? – DevilsHnd - 退職した Feb 03 '17 at 05:47
  • @DevilsHnd hi sir i know the problem the output are different when they should be the same, i don't know why they are different though thats my issue that i'm having, i was hoping someone here knew what is going on and could help me – Galfi Feb 03 '17 at 05:50
  • Your output lines are fine.. Just the way you have printed is messy.. – anacron Feb 03 '17 at 05:56
  • @anacron Hi SIR so what would I need to do to make both outputs the same, what could I change because i don't know whats making it so horrible! – Galfi Feb 03 '17 at 05:59
  • Why not just use System.out.println(s1.getSol()) instead of ...print and println? – Adrian M. Feb 03 '17 at 06:00
  • @Galfi, see my answer below. – anacron Feb 03 '17 at 06:02

1 Answers1

0

I see that the way your are using System.out.print inside your RandomBinaryString(int n) is causing confusion. It is printing and appending to the String s. Try to avoid that. Replacing the System.out.print(s += '0'); and System.out.print(s += '1'); with s += '0'; and s += '1';in the RandomBinaryString will fix your output.

Use the snippet below in your code:

private static String RandomBinaryString(int n) {
    String s = new String();

    // Code goes here
    // Create a random binary string of just ones and zeros of length n
    for (int i = 0; i < n; i++) {
        int y = randomOther.UI(0, 1);
        if (y == 0) {
            s += '0';// this line here was changed
        } else {
            s += '1';// and this line here was changed too
        }
    }

    return (s);
}

Hope this helps!

anacron
  • 6,443
  • 2
  • 26
  • 31