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) {
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 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));
}
}
}
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 2 years ago.
Improve this question
I tried reading about this and I can't find the real problem. I'm new to java and it's my first time that I've encountered this problem. I tried putting the counter inside main method and outside of it and it still says unreachable statement.
Here is my code:
import java.util.Random;
import java.util.Scanner;
public class Harrylist{
static Random Userinput = new Random();
static Scanner input = new Scanner(System.in);
static String cancellor;
static int counter = 0;
static int integer = 0;
public static void main(String[] args) {
System.out.println("Guessing game! please enter a total number of guesses");
int y = input.nextInt();
int[][] GuessingGame = new int[2][];
GuessingGame[0] = new int[y];
GuessingGame[1] = new int[y];
//AI inputs
for(int i = integer; i < y; i++){
GuessingGame[1][i] = Userinput.nextInt(50);
//System.out.println(GuessingGame[1][i]);
//User inputs
System.out.println("Please enter a number within the range of 50");
cancellor = input.nextLine();
GuessingGame[0][i] = input.nextInt();
for(int j = 0; j<y; j++){
if(GuessingGame[0][integer] == GuessingGame[1][j]){
System.out.println("Correct!");
break;
counter++;
}
You cannot put anything after break statement because it break your loop. counter++ will never be reached. Use this:
counter++;
break;
instead this:
break;
counter++;
the break keyword causes your code to interrupt and exit out of the loop. Therefore everything insite of the if(GuessingGame[0][integer] == GuessingGame[1][j]) condition and after the break will not be reached. To fix this you just need to make sure that the break statement is the last thing in its block of code.
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
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);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hi there I am trying to create an array of records in java in which you have to enter 3 details, the name of a town, the population and the county in which is resides. Before then outputting all the data on a county which you have asked for. I was wondering if anyone could show me why a null.point.exception occurs if I enter the population of a town when does not occur when i enter another one.
import java.util.*;
public class CathedralTowns
{
public static String name;
String population;
String county;
public static int count = 0;
public static int continuation = 0;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int loop1 = 0;
while (loop1 <= 0) {
System.out.println("Please enter the name of the town. ('no' to end)");
String nameEntered = input.nextLine();
System.out.println("Please enter the county in which the town resides. ('no' to end)");
String countyEntered = input.nextLine();
System.out.println("Please enter the population of the town. ('no' to end)");
String populationEntered = input.nextLine();
if (nameEntered.equals("no") || populationEntered.equals("no") || countyEntered.equals("no") ) {
loop1 = 5;
System.out.println("Thank you for entering your county.");
continuation = 1;
}
WorkingDemCathedrals(nameEntered, populationEntered, countyEntered);
}
}
public static void WorkingDemCathedrals(String nameEntered, String populationEntered, String countyEntered) {
Scanner input = new Scanner(System.in);
CathedralTowns[] allTowns = new CathedralTowns[50];
allTowns[count] = new CathedralTowns();
int loop2 = 0;
int loop3 = 0;
while (loop2 == 0){
allTowns[count].name = nameEntered;
allTowns[count].population = populationEntered; //the error relates back to here according to bluej
allTowns[count].county = countyEntered;
if (continuation == 1) {
loop2 = 1;
System.out.println("please enter the name of a county for which you wish to know the details.");
String countyOfChoice = input.nextLine();
while (loop3 > 0){
if ((allTowns[loop3].county).equals(countyOfChoice)){
System.out.println(allTowns[loop3].name);
System.out.println(allTowns[loop3].population);
loop3 = -2;
}
loop3 = loop3 +1;
}
}
count = count + 1;
}
}
}
Elements in an Object array are null by default. Initialialise the elements prior to attempting to access them
for (int i=0; i < allTowns.length; i++) {
allTowns[i] = new CathedralTowns();
}
This lines is very suspicious
allTowns[count] = new CathedralTowns();
You allocate only one object in the array while you have a line before allocated an array of the length 50.
CathedralTowns[] allTowns = new CathedralTowns[50];
Not to mention that it is prone to ArrayIndexOutOfBoundsException if count is equal or more that 50
Then you start to loop and increment count and that's where it happens!
your should loop over the entire array and allocate an object in each slot.
The NullPointerException occurs at "population" and not at "name" is because the "name" field is static, whereas the "population" is non-static.
Also the allocation of the array of CathedralTowns has to be done as per the first answer.
The while (loop2 == 0) could end up in a infinite loop. There is no end condition for this while loop, if the user wants to enter details of more than one county.