Questions about Random and Int (Java) [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Assuming I generate a random number between 1-10, how do I use the random number generated into a for loop? Is there a way to convert random to int?
public class CityTravelerApp {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// TODO code application logic here
Queue myQueue= new Queue(80);
//a. Generate a random number called num between 1 and 10, inclusive.
Random number = new Random();
System.out.println(number.nextInt(10)+1);
for(int i =1;i<=number;i++){
System.out.println("Enter the name of country #" + i);
String name = input.next();
}
}

number is Random Object which you can not use in loop for comparing it with int i, instead store result of number.nextInt to some int variable and use that variable in for loop. Basically, you can not compare int with Random, because both types are incompatible.
Random number = new Random();
int limit = number.nextInt(10) + 1;
for(int i = 1; i <= limit; i++){
...
}

Related

Java multiplication (in the type PrintStream is not applicable for the arguments (String, int)) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
Table of a any number
getting this error - on System.out.println(number+" x "+i+" = ",+number*i);
(in the type PrintStream is not applicable for the arguments (String, int))
package JAVAS;
import java.util.Scanner;
public class number {
public static void main(String[] args) {
Scanner num = new Scanner(System.in);
System.out.println("Enter the number ??");
int number = num.nextInt();
int i=1;
System.out.println("the table of the following number is ");
while (i <= 10)
{
System.out.println(number+" x "+i+" = ",+number*i);
i++;
}
}
}
Your problem is you have an extra comma in your println. However, for clarity and to expose to you better methods of doing this, consider the following:
public static void main(String[] args) throws IOException {
try (Scanner scanner = new Scanner(System.in)) {
System.out.println("Enter the number ??");
int number = scanner.nextInt();
System.out.println("the table of the following number is ");
String format = "%d x %d = %d";
for (int i = 1; i < 11; i++) {
System.out.println(String.format(format, number, i, number * i));
}
}
}

Why is my substring causing an error in my program? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
When I run this program, an error comes up, saying that it is due to "String.substring(int, int)line: not available. However, it outputs the correct answer (with a number of 123 and a target of 2, the final should be 35). Any help would be great, thanks!
import java.util.Scanner;
public class Math{
public static void main(String[] args) {
for (int i=1; i<=5; i++) {
System.out.println("Please enter your number:");
Scanner input = new Scanner(System.in);
String number= input.nextLine();
System.out.println("Please enter your target:");
int target= input.nextInt();
// input.close();
String outcome= "0";
long final = Long.parseLong(outcome);
for (int h=0; h<=((number.length())-target+1); h++) {
String result = number.substring(h, (target+h));
long output = Long.valueOf(result);
final = final + result;
System.out.println(final);
}
}
}
}
You have several problems here:
final is a reserved keyword, and cannot be a variable name. You must rename it
You are trying to add a String to a long in the line:
final = final + result;
You have a index out of bounds error when you call substring. You are looping one more time then necessary. Change <= to <:
Code:
String outcome= "0";
long finalVar = Long.parseLong(outcome);
for (int h=0; h<((number.length())-target+1); h++) {
String result = number.substring(h, (target+h));
long output = Long.valueOf(result);
finalVar = finalVar + output;
}
System.out.println(finalVar);
Input/Output:
Please enter your number:
123
Please enter your target:
2
35

Sum of Digits of the Input from user [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to create a code that will get the sum of all the digits of the input,
sample input: 241 sample output: 7
But there are restrictions in in making the program, only the basic operations, functions should be used and no string function is to be used in getting the said sum of the digits only (loop, /, %, *,-,+)are allowed to be used.
The program I am thinking should start with this..
public class SumOfDigits{
public static void main(String args[]) throws Exception{
Scanner input = new Scanner(System.in);
int num = input.nextInt();
while(){
}
}
}
One option would be to use a modulus trick to isolate, and then sum, each digit in the input number:
Scanner input = new Scanner(System.in);
int num = input.nextInt();
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
Follow the link below for a working demo.
Demo
modulu 10 of the number will give you the last digit, the dividing it by 10 will give you the other digits, due to integer division. You can loop until you've handled all the digits:
int sum = 0;
while (num != 0) {
sum += (num % 10);
num /= 10;
}
you can use a recursive function like this
import java.util.Scanner;
public class Main {
Scanner scanner ;
int result = 0;
public static void main(String[] args) {
// initialize new instance of current class
Main thisIns = new Main();
// initialize scanner instance for this instance
thisIns.scanner = new Scanner(System.in);
//calling for getting user inputs
int i = thisIns.getUserInput();
//get the sum of the integer
thisIns.getSumOfInterger(i, 0);
System.out.println("result is " + thisIns.result);
}
public int getUserInput(){
System.out.println("Please enter an integer : ");
int i = scanner.nextInt();
return i;
}
public void getSumOfInterger(int i, int moduler) {
if (i != 0) {
moduler += i%10;
getSumOfInterger(i/10, moduler);
// return 0;
}else{
result = moduler;
}
}
}

How do I display the actual input instead of option number? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to get this to display the actual input instead of the option number.
import java.util.*;
public class RandomGenerator {
public static void main(String[] args) {
int length;
Scanner input = new Scanner(System.in);
System.out.println("How many options?"); //user input food options
length = input.nextInt();
String[] names = new String[length];
for(int counter = 0; counter < length; counter++){
System.out.println("Enter option #" + (counter+1) + ":");
names[counter] = input.next();
}
input.close();
System.out.println("You are going to eat " + new Random().nextInt(names.length));
You've already generated a random number here:
new Random().nextInt(names.length)
You can use this random number to access an element in the names array.
int randomNumber = new Random().nextInt(names.length);
String option = names[randomNumber]; // here is the important bit!
Now you can print option out!
System.out.println("You are going to eat " + option);

I am trying to input sentences in string arrays and display them in reverse order [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
So far I have set it up so that the user can enter the number of sentences and input into each position of the String array using a for loop.
public class Test5 {
public static String inputline;
public static void main(String[] args) {
System.out.print("Enter the number of lines:");
Scanner kb=new Scanner(System.in);
int number=kb.nextInt();
String []line=new String[number];
for(int i=0;i<line.length+1;i++){
line[i]=kb.next();
}
}
}
First and foremost your code is going to read in 1 more time than you want which will cause an array out of bounds exception. Next you will want to do nextLine() to account for the new line character being entered by the user. Try this:
System.out.print("Enter the number of lines:");
Scanner kb=new Scanner(System.in);
int number=Integer.parseInt(kb.nextLine());
String []line=new String[number];
//loop through only the size of the array
for(int i=0; i < line.length; i++){
line[i]=kb.nextLine();
}
//now to output the array in reverse order you need to start from the
//other end of the array
for(int i = line.length - 1; i >= 0; i--){
System.out.println(line[i]);
}
//always close the Scanner when done
kb.close();
Some useful resources about Scanners - https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

Categories

Resources