Is there a way to change a java variable declaration? [duplicate] - java

This question already has answers here:
Can I change declaration type for a variable in Java?
(4 answers)
What is 'scope' in Java?
(2 answers)
Closed 15 hours ago.
//imports
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//vars
double x = 5;
Boolean y = true;
while (y){
String x = sc.nextLine();
y = false;
}
System.out.println(x); // x should output the String but doesn't.
//how do I fix this or is there a different way?
}
}
Actual code that I am working on if that helps.
https://replit.com/#AlexanderJensen1024/Tests#Main.java
I tried to null the variable an answer to someone else's question.

Related

Why I input a string with space but can not print out it fully? [duplicate]

This question already has answers here:
Scanner doesn't see after space
(7 answers)
Closed 1 year ago.
I am writing a very simple program to input a string with space and then output it, my problem is, it not printed out fully as I expected.
Here is my code, as you can see, very simple
import java.util.Scanner;
public class Testjapanese {
public static void main(String[] args) {
String x;
Scanner keyboard = new Scanner(System.in);
System.out.println(" Add a string");
x = keyboard.next();
System.out.println(x);
}
}
For example, it print "Add a string" but when I input a string "Today is very", it gave me "Today", not "Today is very".
I search and they said to me that I should use input.nextLine(), but I do not know how to use it.
May be I must use public java.lang.String nextLine() first ?
Sorry if my question is easy to solve. Thank you for your answer.
import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
String x;
Scanner keyboard = new Scanner(System.in);
System.out.println(" Add a string");
x = keyboard.nextLine();
System.out.println(x);
}
}
You should just replace next with nextLine as below
public static void main(String[] args) {
String x;
Scanner keyboard = new Scanner(System.in);
System.out.println(" Add a string");
x = keyboard.nextLine();
System.out.println(x);
}

can you please tell why there is no output while running [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 2 years ago.
I am trying to solve https://www.codechef.com/problems/FLOW010 problem.
I wrote
`
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t>0){
String s=sc.next();
if(s=="b" || s=="B"){
System.out.println("BattleShip");
}
else if(s=="c" || s=="C"){
System.out.println("Cruiser");
}
else if(s=="d" || s=="D"){
System.out.println("Destroyer");
}
else if(s=="f" || s=="F"){
System.out.println("Frigate");
}
t--;
}
}
`
There is no mistake in terms of syntax. Please help me what is mistake
Replace all your == comparisons for String like this:
String s=sc.next();
if(s.equalsIgnoreCase("b") {
System.out.println("BattleShip");
}
// ... etc.

Do While loop terminates and doesn't execute scanner function [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 2 years ago.
Below is my code for a "Do-While" loop. I can't figure out why it's terminating before I even get to enter in the value for my variable "redo" at the end of the do {} section. It terminates right after printing the "would you like to do this again" part.
import java.util.Scanner;
public class AlgWorkbench0402 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String redo;
do {
System.out.println("Enter in two integers and get the sum.");
int val1, val2;
val1 = Integer.valueOf(scanner.nextInt());
val2 = Integer.valueOf(scanner.nextInt());
System.out.println(val1 + val2);
System.out.println("Would you like to do this again? [y/n]");
redo = scanner.nextLine();
}
while (redo == "y" || redo == "Y");
}
}

Java resource leak not closed [duplicate]

This question already has answers here:
Close a Scanner linked to System.in
(5 answers)
Closed 6 years ago.
I am writing a program where I want the console to output the remainder of the user inputted number. However, every time I compile the code the console prints this out and I get this console error:
1 problem (1 warning)
Compiler is using classPath = '[C:\Users\Darien Springer\Documents\Java, C:\Users\Darien Springer\Desktop\drjava-beta-20160913-225446.exe]'; bootClassPath = 'null'
----------
1. WARNING in C:\Users\Darien Springer\Documents\Java\PrintDigits.java (at line 5)
Scanner scnr= new Scanner(System.in);
^^^^
Resource leak: 'scnr' is never closed
----------
1 problem (1 warning)
I am not sure what the console means by a "resource leak". I have looked it up in several different places (including the API and other Stack Overflow questions) and I am not sure why nothing prints to the console. I am using the program DrJava in case anyone is wondering.
Here is my code for reference:
import java.util.Scanner;
public class PrintDigits {
public static void main(String [] args) {
Scanner scnr= new Scanner(System.in);
int userInput = 0;
int positiveInt = 0;
System.out.println("enter a positive integer:");
userInput = scnr.nextInt();
positiveInt = userInput % 10;
System.out.println(positiveInt);
return;
}
}
All that that warning says is tha you never call scnr.close(); anywhere in your code. To get it to go away, just call scnr.close(); after you are done using the scanner.
import java.util.Scanner;
public class PrintDigits {
public static void main(String [] args) {
Scanner scnr= new Scanner(System.in);
int userInput = 0;
int positiveInt = 0;
System.out.println("enter a positive integer:");
userInput = scnr.nextInt();
positiveInt = userInput % 10;
System.out.println(positiveInt);
scnr.close();
return;
}
}

Why won't this If statement work for the string? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
import java.util.Scanner;
public class Methods {
/*
* Compound interest Program Quarterly
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner (System.in);
System.out.println("What is the Rate");
int rate = keyboard.nextInt()/100;
System.out.println("What is the Amount");
int amount = keyboard.nextInt();
System.out.println("How many years?");
int years = keyboard.nextInt();
keyboard.nextLine();
System.out.println("How is it compounded? Press Q = Quarterly, A = Anually, S = SemiAnnualy, M = Monthly");
String answer = keyboard.nextLine();
int easy = amount*(1+(rate/years));
int pow = 4 * years;
if (answer == "/Q"){
System.out.println("Your answer compounded Quarterly is: " + Math.pow(easy,pow));
This is the code, but the if statement with String == "Q" doesn't work because when I press Q nothing happens? what's the issue?
Java right? use equalsIgnoreCase because you could be typing 'q' and that / they told you about too, keep it in mind
Maybe you can try
if(answer.equals("Q"))
First issue is that you have a typo (the forward slash) in this line:
if (answer == "/Q"){
The second issue is that you don't compare strings like this.
Instead use the "equals()" method.
if(answer.equals("Q")){
See here for more information: How do I compare strings in Java?

Categories

Resources