How do I fix the unreachable statement? [closed] - java

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.

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));
}
}
}

I am trying to use the scanner inputs for a for loop that counts the number from the min to max and display the numbers in the terminal [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 3 years ago.
Improve this question
I want to count a range of number with a for loop using input from scanner.
Tried writing the code but still no results
package com.example.myapplication;
import java.util.Scanner;
public class dsd {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter first number:");
int firstnum = sc.nextInt();
System.out.println("Enter first number:");
int secondnum = sc.nextInt();
System.out.print("counting ");
for (int i = firstnum; i >= secondnum; i++) ;
{
System.out.print(i);
}
}
Upper limit :5
Lower Limit :2
Expected to see COunting 2 ,3 ,4, 5
I think it would be better to use a camel case to match the coding convention. And if you put a semicolon after the for loop, the following code will not be executed. Of course, the conditional statements in the for loop seem a little wrong.
For the example given in the question, see the code below:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Upper limit:");
int firstNum = sc.nextInt();
System.out.print("Lower Limit:");
int secondNum = sc.nextInt();
System.out.print("counting ");
for (int i = secondNum; i <= firstNum; i++) {
System.out.print(i);
}
}
Result (in the terminal)
Upper limit:5
Lower Limit:2
counting 2345
I have seen this mistake many times:
for (int i = firstnum; i >= secondnum; i++) ;
Please remove the ;
Is the first number always greater than the second?
If so, you should write it as the code below.
// Delete semicolon. and less then change
for (int i = firstnum; i <= secondnum; i++) {
System.out.print(i);
}
In your code you have used the "sc" scanner for both firstnum/secondnum variable. The loop of your code will print only the last number inserted.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int i;
int start, end;
Scanner sc = new Scanner(System.in);
Scanner sc1 = new Scanner(System.in);
/*
* The block try-catch highlights the fact that you cannot insert min
* number bigger than the max number
*/
try {
System.out.print("Enter first number:");
start = sc.nextInt();
System.out.print("\nEnter second number: ");
end = sc1.nextInt();
if(start > end) throw new Exception();
i = start;
while (i<=end){
System.out.println("count: " + i);
i++;
}
}catch(Exception e){
System.out.println("You cannot start with number bigger than 'end-number' ");
}
}
}

Cannot Find symbol Java MultiDimensional Array [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
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) {

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

Simple bulls and cows java game [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I try to create a verry simple Bulls and Cows game (https://en.wikipedia.org/wiki/Bulls_and_Cows). The game is for my school project. My knowledge is limited, so I need to make the game using only loops, IF-else constructions and other simple functions.
The written code works somewhat - generate code and understands that number is hited, but did not indicate how many cows and bulls have in the wrong assumptions.
I would be glad if someone point me in the right direction :) Thanks in advance
import java.util.Random;
import java.util.Scanner;
public class BCgame{
public static void main(String[] args){
Random r= new Random();
int number= 0;
int trynum = 0;
while(uniq(number= (r.nextInt(9000) + 1000)));
String targetStr = number +"";
boolean game = true;
Scanner input = new Scanner(System.in);
do{
System.out.println(number);
int bulls = 0;
int cows = 0;
System.out.print("Guess a number: ");
int guess;
guess = input.nextInt();
if (uniq(guess) || guess < 1000) continue;
trynum++;
String guessStr = guess + "";
for(int i= 0;i < 4;i++){
if(guessStr.charAt(i) == targetStr.charAt(i)){
bulls++;
}else if(targetStr.contains(guessStr.charAt(i)+"")){
cows++;
}
}
if(bulls == 4){
game = false;
}else{
System.out.println(cows+" Cows and "+bulls+" Bulls.");
}
}while(game);
System.out.println("You won after "+trynum+" guesses!");
}
public static boolean uniq(int num){
String checknum = num+"";
if(checknum.charAt(0) == checknum.charAt(1)) return false;
else if(checknum.charAt(1) == checknum.charAt(2)) return false;
else if(checknum.charAt(2) == checknum.charAt(3)) return false;
return true;
};
}
You've already gone a long way towards the solution using the Rosetta example (http://rosettacode.org/wiki/Bulls_and_cows#Java). Just continue to work through it.
Experimentation is key to learning a language well, so make sure you understand what each line is doing and test the results.
Also, learn how to use your debugging tools to see which lines are behaving differently to what you expect, and then try and understand why.
If you get stuck on a specific statement then post that. Otherwise, this question is too unspecific, especially when Rosetta has a working example for you.

Categories

Resources