Confused with "If statement" in Java [duplicate] - java

This question already has answers here:
Semicolon at end of 'if' statement
(18 answers)
Closed 4 years ago.
I got to code Java for an assignment where am I going wrong with this "if statement" surely if I enter anything but 10 in won't print out Variable information. Picture attached.
import java.util.Scanner;
public class JavaApplication7 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int code = 0;
code = input.nextInt();
if (code == 10); {
System.out.println(code);
}
}
}

Never mind I think I figured it out I was ending my statement to early or something this stuff is not very forgiving. Edit: Just saw snr answered haha thank you.

Related

Can't get program to break on specific string [duplicate]

This question already has answers here:
Scanner only reads first word instead of line
(5 answers)
Closed 2 years ago.
The code works for the most part, but if I type "No way" it still stops the loop. Should I set it up a different way or use a length function ? Everything I've searched on breaking a loop used integers.
See the code below:
import java.util.Scanner;
public class CarryOn {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Shall we carry on?");
String answer = String.valueOf(scanner.next());
if (answer.equalsIgnoreCase("no")){
break;
}
}
}
}
Using .next in this case only stores "no" in "no way". Use .nextLine instead:
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Shall we carry on?");
String answer = String.valueOf(scanner.nextLine());
if (answer.equalsIgnoreCase("no")){
break;
}
}
Output:
Shall we carry on?
no way
Shall we carry on?
no
Check this post for more information.
scanner.next()
only reads a single token. No way is two tokens: No and way.
Use scanner.nextLine() instead, if you want to read the whole line.

How do I solve this error with Scanner class: Exception in thread "main"...? [duplicate]

This question already has an answer here:
java.lang.IllegalStateException: Scanner closed
(1 answer)
Closed 4 years ago.
import java.util.Scanner;
public class Main {
public static void main(String[]args) {
double num = 0;
double counter = 0;
double ncot = 0;
Scanner scan = new Scanner(System.in);
while (!(num == -1)) {
ncot = scan.nextDouble();
if (ncot == -1) {
System.out.println("The average is: " + (double)(num/counter));
}
else {
num = num+ncot;
counter++;
}
}
scan.close();
}
}
Exception in thread "main" java.lang.IllegalStateException: Scanner closed
at java.util.Scanner.ensureOpen(Scanner.java:1070)
at java.util.Scanner.next(Scanner.java:1465)
at java.util.Scanner.nextDouble(Scanner.java:2413)
The Code it seems to be triggering a error when im running it on Ideone.com but when i run in eclipse it's fine however.
This is really puzzling. According to the published source code for the standard versions of Scanner (Java 6 and Java 8), the only way that you can get that exception at that point is if something has already called close() on the Scanner object.
(Note that if you instantiated a Scanner around an input stream that was already closed, or a dummy, or null, you would get a different exception. This particular exception only happens if the Scanner object's private closed flag is true, and that only happens when you call Scanner.close().)
But with the code you have shown us, that is not possible. The Scanner can only be closed after you exit the loop.
I can think of only two explanations:
This isn't the code that you ran on ideone.
The guys have implemented ideone have tweaked the behavior of the standard Scanner class. In a way that is inexplicable ... to me.

Why this program is giving wrong output for palindrome? [duplicate]

This question already has answers here:
Why does a Java if statement fail when it ends in semicolon [duplicate]
(9 answers)
Closed 7 years ago.
I am making the program forthe largest palindrome made from the product of two 3-digit numbers.
But program is giving product of 999*999=998001
Can anyone tell what's wrong in this code?
Program-
public class abc {
public static void main(String[] args)
{
int p=0,temp=0;
for(int i=100;i<=999;i++)
{
for(int j=100;j<=999;j++)
{
p=i*j;
StringBuilder sb=new StringBuilder(Integer.toString(p));
sb.reverse();
if((sb.toString()).equals(Integer.toString(p)) && p>temp)
{
temp=p;
}
}
}
System.out.println(temp);
}
}
You have an extra ; :
if((sb.toString()).equals(Integer.toString(p)) && p>temp);
^
It ends the if statement, so the following block, with temp=p; is always executed.
Remove it and you'll get
906609

Can't figure out why my method isn't executing [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
Below is a snippet of code from a class in a simple program I'm writing that plays a simple game of Blackjack. I don't understand why the hit method isn't executing whenever, upon execution of the program, I enter "hit". It goes to the else part of the statement every time regardless of what I enter. I even added a System.out.println statement to make sure that the strings matched. I feel like I must be making a very basic mistake but I just can't seem to figure it out.
System.out.println("Would you like to hit or stand?");
Scanner input = new Scanner(System.in);
String playerDecision = input.nextLine();
//System.out.println(playerDecision);
if(playerDecision == "hit") {
hit();
}
else { System.out.println("ERROR");
}
}
public void hit(){
player.makeHand(deck.draw());
System.out.println("You have the following cards: ");
player.getHand();
System.out.println("Your hand total is ");
System.out.println(player.findHandTotal());
}
Wrong string comparison. Try
if("hit".equals(playerDecision)) {

Whenever I execute my code it gives me what looks like memory addresses and other nonesense [duplicate]

This question already has answers here:
Printing array error
(3 answers)
Closed 8 years ago.
Whenever I execute my code in java with eclipse (code is as follows)
import java.util.*;
import java.io.*;
public class PracticeOne {
public static void main(String[] args) throws FileNotFoundException {
Scanner scn = new Scanner (System.in);
int first = scn.nextInt();
scn.nextLine();
for(int i =0; i<first; i++){
String preSplt = scn.next();
String postSplt[] = preSplt.split("--");
System.out.println(postSplt);
}
}
}
and once I type in
3
Honda element--19--17950
Ford Edge--19--18130
it gives me this
[Ljava.lang.String;#c0fa1f5
[Ljava.lang.String;#4e15f6af
in between "Honda element" and "Ford Edge" as I press enter and then it spits out
[Ljava.lang.String;#3f68336
after "Ford Edge" and for the record it does not split the input I give it or do anything after it spits out that last bit although this may be due to ineffective code (I am not done with this program yet) I thought it might be a little helpful
I find assembly language quite fascinating but I would like to know whats causing this and if it even has anything to do with assembly
The problem here is that you're attempting to print the array object itself instead of its contents. Use Arrays.toString:
System.out.println(Arrays.toString(postSplt));

Categories