+ 1

Java

In Java, How to generate random numbers and nouns changing each time we run? Also... I didn't quite understand Scanner How is it different from BufferedReader; when and how to use it? (Can anyone please just make a small simple program using Scanner for my better understanding...)

2nd Dec 2018, 6:52 AM
Coding Niffler
Coding Niffler - avatar
4 odpowiedzi
+ 16
1) Generating Random number //using Random Class ● import Random class //import java.util.Random; ●create an object of that class & generate a random number from integer a to b /* Random R1 = new Random(); number1 = a + R1.nextInt(b) + 1 ; //a+1 is added because R1.nextInt(b) generates random number from 0 to (b-1). //edited Michal ☺ (thnx for noticing) */ 2) Using Scanner Class to take input ● import java.util.Scanner; //importing Scanner class ● Scanner sc = new Scanner(System.in); //making object of Scanner class ● int number = sc.nextInt(); //for taking Integer input //U can have a look at more input methods for different data types here : https://www.sololearn.com/learn/Java/2220/ 3) Follow this link for proper differences between Scanner & Buffer Class in java : https://www.google.com/amp/s/www.geeksforgeeks.org/difference-between-scanner-and-bufferreader-class-in-java/amp/
2nd Dec 2018, 7:55 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 3
Pro Coder You can get random city for example that way https://code.sololearn.com/cOoW0Jmo7SPU/?ref=app // Gaurav Agrawal, actually nextInt(b) generates from 0 to b-1
2nd Dec 2018, 10:01 AM
Michal
Michal - avatar
+ 2
Gaurav Agrawal Thanks Michal Thanks
2nd Dec 2018, 10:03 AM
Coding Niffler
Coding Niffler - avatar
+ 1
Gaurav Agrawal Do you know how to print random names(city names etc.)?
2nd Dec 2018, 9:28 AM
Coding Niffler
Coding Niffler - avatar