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);
Related
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
Simple program that asks the user about bus routes and some information about them. This then stores them in a 2-d array. And asks the user some questions. Ive only tried to code the decision to print the array out at the moment.
This error keeps popping up when I try to compile it.
Cannot find symbol
symbol : variable routes
location: class question
Its for routes and for decisions
import java.util.Scanner;
import java.util.Arrays;
public class question {
public static void main(String[] args) {
String[][] array = new String[4][2];
Scanner sc = new Scanner(System.in);
System.out.println("How many bus routes are there");
routes = sc.nextInt();
for (int i = 0; i < routes ; i++) {
System.out.println("Please enter the bus route");
System.out.println("What is the bus route");
array[i][0] = sc.nextLine();
System.out.println("Start Location");
array[i][1] = sc.nextLine();
System.out.println("End Location");
array[i][2] = sc.nextLine();
System.out.println("Who is the driver for bus route " + i);
array[i][3] = sc.nextLine();
}
System.out.println("Do you want to 1) Print all bus routes 2) Start a new day 3) Quit ? ");
decision = sc.nextInt();
if (decision = 1) {
for (int i = 0; i < array.length; i++) {
System.out.println(Arrays.toString(array[i]));
}
}
}
}
you need to declare routes as int
int routes = sc.nextInt();
same with decision
Also, use == in a condition to check equality
if (decision == 1) {
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
I am currently attempting to make a program that takes any string input and splits by the spaces to create an acronym. Currently the program only prints out the first letter of the first word input. I believe that the problem is with my for loop. Could someone care to take a look?
import java.util.Scanner;
import java.io.*;
import java.util.*;
public class WordSplitter {
public static void main(String[] args) {
String phraseToChange;
Scanner input = new Scanner(System.in);
System.out.println("This program builds acronyms");
System.out.println("Enter a phrase:");
phraseToChange = input.next();
String[] phraseChanger = phraseToChange.split(" ");
for (int i = 0; i <= phraseChanger.length; i++) {
String s = phraseChanger[i];
System.out.println(s.charAt(0));
}
}
}
You never actually build the acronym using the first letter from each word. Try this code instead:
String [] phraseChanger = phraseToChange.split(" ");
StringBuilder sb = new StringBuilder();
for (int i=0; i < phraseChanger.length ; i++) {
sb.append(phraseChanger[i].charAt(0));
}
System.out.println(sb.toString());
instead of this phraseToChange = input.next(); you have to put phraseToChange = input.nextLine(); it will work
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
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 7 years ago.
Improve this question
Im not sure how to go about making this code.. well at least the total part of this. this is my code right now, I understand the math behind it. I'm just not sure how to implement it. Would I use a loop? here is my code as of right now. I know its not correct but its the start of it.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("what number would you like me to multiply: ");
int number = in.nextInt();
System.out.println("how many multiples of "+number+" would you like to see: ");
int multiple = in.nextInt();
int total =
System.out.println("total :"+total);
}
here is the output that I would like to get:
What number would you like me to multiply? 4
How many multiples of 4 would you like to see? 7
The first 7 multiples of 4 are: 4, 8, 12, 16, 20, 24, 28
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("what number would you like me to multiply: ");
int number = in.nextInt();
System.out.println("how many multiples of "+number+" would you like to see: ");
int multiple = in.nextInt();
System.out.print("The first " + multiple + " multiples of " + number + " are: ");
for(int i=1; i<=multiple; i++){
if(i>1){
System.out.print(", ");
}
System.out.print(number*i);
}
}
Not for this site but:
String output = "The first " + multiples + " multiples of " + number + " are: "
for(int x = 1; x <= multiples; x++){
output = output + Integer.toString(x*number) + ",";
}
This can be improved using String.format but ill leave that to you.
Your code could also be improved by doing error checking because right now you could say 'a' for an input and it would crash.
You should look up the basics of programming and also just try stuff you'd be surprised by what you learn just doing random things. There is https://projecteuler.net/ if you need ideas about what to try.
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++){
...
}