Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm having a bit of trouble generating 10 Random numbers while using toString. I can get the number within dbl[1].toString() to print but its a fixed value not a random one

What I have tried:

Java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fraction;

/**
 *
 * @author stephenwessels
 */
import java.util.*;
import java.util.Random;
public class Console 
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        Scanner in = new Scanner(System.in);
        Random r = new Random();
        
        Double[] dbl = new Double[10];
        
        
        for(int i = 0; i < dbl.length; i++)
        {
            dbl[i] = new Double(i);
        }
        System.out.println(dbl[1].toString());
  }
    
}
Posted
Updated 10-Mar-18 23:39pm
v2

OK, this is bloody obvious if you read the code. You create an instance of Random and put it in the variable r, but then YOU NEVER USE THE THING to generate a random number!

Also, "while using ToString" has nothing to do whit this at all.
public static void main(String[] args) 
{
    Scanner in = new Scanner(System.in);
    Random r = new Random();

    Double[] dbl = new Double[10];

    for(int i = 0; i < dbl.length; i++)
    {
        dbl[i] = r.nextDouble();
        System.out.println(dbl[i].toString());
    }
}
 
Share this answer
 
Comments
Maciej Los 11-Mar-18 5:40am    
5ed!
CPallini 11-Mar-18 5:54am    
:-)
5.
Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- It is also a great learning tool because it show you reality and you can see which expectation match reality.

secondary effects
- Your will be proud of finding bugs yourself.
- Your learning skills will improve.

You should find pretty quickly what is wrong.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900