Jul 20, 2008

Having Trouble Getting Program To Print Out Multip

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > Java, Java Servlets, Java Script, & JSP

free web hosting

Having Trouble Getting Program To Print Out Multip

kvarnerexpress
i'm trying to have a user input a number between 1 to 5, and that number will be the number of times the program will print out random 10 digit integers. Example, if user inputs 3, then the program will print out 3 random 10 digit intergers.

Here's what i have so far

Code:
CODE

import java.util.Scanner;
import java.util.Random;
public class test3
{
public static void main (String[] args)
{
 Scanner scan = new Scanner(System.in);
 Random generator = new Random();
 int n=0, times;

 String number = "";
 
 System.out.print ("Number of times: ");
 times = scan.nextInt();
 for (int i=0; i<10; i++)
 {
 n = generator.nextInt(10);
 number += n * times;
 }

System.out.println(number);
}
}


I tried multiplying it by "times" but it just multiplies that random 10 digit interger by whatever the number the user inputs for "time".

Anyone know what i'm doing wrong?
Thanks,kvarnerexpress

 

 

 


Reply

mike_savoie
QUOTE(kvarnerexpress @ Sep 22 2005, 07:27 AM)
i'm trying to have a user input a number between 1 to 5, and that number will be the number of times the program will print out random 10 digit integers. Example, if user inputs 3, then the program will print out 3 random 10 digit intergers.

Here's what i have so far

Code:
CODE

import java.util.Scanner;
import java.util.Random;
public class test3
{
public static void main (String[] args)
{
 Scanner scan = new Scanner(System.in);
 Random generator = new Random();
 int n=0, times;

 String number = "";
 
 System.out.print ("Number of times: ");
 times = scan.nextInt();
 for (int i=0; i<10; i++)
 {
 n = generator.nextInt(10);
 number += n * times;
 }

System.out.println(number);
}
}


I tried multiplying it by "times" but it just multiplies that random 10 digit interger by whatever the number the user inputs for "time".

Anyone know what i'm doing wrong?
Thanks,kvarnerexpress
*




I haven't coded Java in a while. But programming logic tells me your error is in here:
CODE

for (int i=0; i<10; i++)
{
n = generator.nextInt(10);
number += n * times;
}


Why are you doing "n*times"?

What you'll want to do is use variable times in your for loop...
CODE

for (int i=0; i< times; i++)

I seem to recall not being able to use a variable as the control counter in the loop, so you may be better off using a different loop.
CODE

do
{
 //your code goes here
 value++;
} while(value < times);

Then simply concatenate the 10 digits with a newline. Similar to this:
CODE

number += '\n' + n;


This code probably won't work as written, I'm at work and do not have a compiler here...but it should give you enough to get started.

 

 

 


Reply

deadlytedly
QUOTE(mike_savoie @ Sep 22 2005, 12:36 PM)
I haven't coded Java in a while.  But programming logic tells me your error is in here:
CODE

for (int i=0; i<10; i++)
{
n = generator.nextInt(10);
number += n * times;
}


Why are you doing "n*times"? 

What you'll want to do is use variable times in your for loop...
CODE

for (int i=0; i< times; i++)

I seem to recall not being able to use a variable as the control counter in the loop, so you may be better off using a different loop. 
CODE

do
{
 //your code goes here
 value++;
} while(value < times);

Then simply concatenate the 10 digits with a newline.  Similar to this:
CODE

number += '\n' + n;


This code probably won't work as written, I'm at work and do not have a compiler here...but it should give you enough to get started.
*





The above poster is right, but you can use a variable in a loop. The modification to make is as follows:

CODE
for(int i = 0; i < times; i++)  // Loops the number of times inputed.
{
   n = generator.nextInt(10);
   number += n + " ";           // int number will automatically get converted to a string here with a space added on or replace the " " with a "\n" for a newline.
}


The above code should compile fine

Reply

cse-icons
hi,

There obviously is a logical error. I am not able to understand u clearly. But from what I understand:

If you want 10 random nos, each to be printed 'time' times in a line

CODE
for(int i=0;i<10;i++){
n = generator.nextInt(10);

for(int j=0;j<times;j++)
      System.out.print(n);

System.out.println("");
}


If you want 'time' no of random nos. then probably:

CODE
for(int i=0;i<times;i++){
n = generator.nextInt(10);
System.out.println(n);
}


Cheers.

Reply

patelg
It is so simple....here is the code...

import java.util.Scanner;
import java.util.Random;
public class test3
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
Random generator = new Random();
int times;
System.out.print ("Number of times: ");
times = scan.nextInt();

for (int i=0; i<times; i++)
{
int n = generator.nextInt(10);
System.out.println(times+1 +" : " +n);
}

}
}

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : program multip

  1. Auto Run Java Program - Run Java program on double click (11)
    Some of the installables in java comes in form of .jar file, one has to just double click or type
    "java -jar file.jar", and it starts executing. The reason for this is a line appended in the
    MANIFEST.MF file of that jar file. For writing a similar jar file of your own, just write your java
    file, then compile the same and create a jar file. Create a MANIFEST.MF file and the content should
    have the followings: CODE Manifest-Version: 1.0 Created-By: xyz Main-Class:
    xyz.MainClass Here xyz.MainClass is the main class. Now create a jar file with the man...
  2. Java Basic Program Guidance - (6)
    aving some problems taking in the Java at Uni, Was wondering if anyone could shine some light on it
    for me. My task is to write a program that will ask you to enter 2 words. the program will then
    print out both words on one line. However the words will be seperated by dots that will make the
    total length of the line 40. so if your first word was turtle and the second was abc, the output
    would be turtle...............................abc The program should check for certain conditions:
    1. a word can not be longer than 37 characters; 2. there must always be atlest 2 do...
  3. Ajax: Achieve Ajax Program In 5 Lines Of Code! - (1)
    Well Last night, after a week of irritation and errors I finally created an easy Javascript object
    that easily and quickly allows you to develop an AJAX program in just 5 easy steps! Here's
    the link to the object I've created: http://www.demolaynyc.astahost.com/ajaxConn/ajaxConn.js
    Download it and read the Readme file that's under the same directory:
    http://www.demolaynyc.astahost.com/ajaxConn/Readme.html Basically what this object does is to
    connect to a server-side script (".php, .asp, .jsp, etc"), and what ever this script displays is
    sent back ...
  4. Eclipse Exporting .jar Files - Could not find main class. Program will exit. (4)
  5. Is There A Way To Use Command Prompt Commands? - To make a program execute cmd stuff (4)
    I looked in the System class because that would make the most sense, but I didn't see anything.
    In C++ it's system(stuff) in stdlib.h. Just looking for something like that....
  6. Practice More Important To Study A Program Lanuage - The way to learn a program language (1)
    I had studied the c++ for long time before I got a program work. The books about c++ is seen by me
    again and again. But at that time I wasn't able to do any big program by c++. Though I knew all
    the syntax of c++. It was very terrible. I felt it was very diffcult as a programmer. Now I am a
    excellent programmer, I have written lots of jave code. Some of them is very difficult, for example:
    Workflow Engine. Now I know why I am not able to write a big c++ program but I know all the syntax
    of c++. Use a sentence to express: "Practice more important to study a program ...



Looking for trouble, program, print, multip

Searching Video's for trouble, program, print, multip
advertisement



Having Trouble Getting Program To Print Out Multip



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE