why wont my while loop break [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I can not figure out why my while loop won't break and it just keeps running I have tries making Lc 10 and I tried return but nothing will end the loop.
import java.util.Scanner;
public class BirthdayReminder
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
String[] BDay = new String[10];
String[] friend = new String[10];
int Lc = 0;
String i;
while(Lc < 10) {
System.out.println("enter a friends name or zzz to quit");
i = input.nextLine();
if(i == "zzz") {
break;
}
else if(i != "zzz"){
friend[Lc] = i;
System.out.println("enter their birthday.");
i = input.nextLine();
BDay[Lc] = i;
Lc++;
return;
}
}
System.out.println("hi");
}
}

You need to check string equality using equals(), not ==. Neither block of code inside the if is getting entered, so Lc is never incremented.

Related

Why are my code not working ?.This a code to check whether a number is a palindrome or not for positive number [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 1 year ago.
import java.util.Scanner;
public class CheckPalindrome3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your number :");
String num = input.nextLine();
String arr = "";
for(int i = num.length() - 1 ; i >= 0;i--)
{
arr = arr + num.charAt(i);
}
System.out.println(arr+"\n"+num);
if(arr == num) {
System.out.println("Yes that's a Palindrome !!");
}
else
{
System.out.println("No that's not a Palindrome !!");
}
}
}
When I put 121 it printed out No thats not a palindrome but arr is the same as num
The problem is the test arr == num. For strings, == tests whether the two strings have the same address in memory (which often is not true, even if the strings look the same). Use arr.equals(num) instead. That will test if all of the characters in arr are the same as those in num.

Calculator no work [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
Could some one please tell me why this calculator isn't working ? It just doesn't provide an awnser.
import java.util.Scanner;
public class Calcu {
public static void main( String[] args )
{
Scanner mati = new Scanner(System.in);
System.out.println("This program adds up or substracts two numbers");
System.out.println("Enter an operator");
String letter = mati.next(); //WAITS FOR THE PHRASE ADD OR SUBSTRACT
System.out.println("Enter your first number");
int userNumberone = mati.nextInt(); // Get's first Number
System.out.println("Enter your second number");
int userNumbertwo = mati.nextInt(); //Get's Following Number
if(letter == "add") {
int result = userNumberone + userNumbertwo;
System.out.println(result);
} else if(letter == "substract") {
int result1 = userNumberone - userNumbertwo; //If statement to add or substract.
System.out.println(result1);
}
}
}
You should use letter.equals("add") instead of letters == "add". This is explained here: How do I compare strings in Java?

Why is this if statement not performing how I want it too? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I am making a Game of Life program, early stages of it. When I run the program and get to the "Do you want to make..." and i input "y", it will go to the else, print my test statement of test3, and end the program. What am I overlooking?
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
String userInput = "";
char[][] initialGrid = new char[25][75];
char[][] world = makeInitialGrid(kb, userInput, initialGrid);
printGrid(world);
userInput = "y";
while (userInput == "y"){
System.out.println("Do you want to make a new generation? (y) yes (n) no");
userInput = kb.nextLine();
System.out.println(userInput);
if (userInput == "y"){
System.out.println("test1");
int numOfNeighbors = findNeighbors(world, 6, 2);
System.out.println("test2");
System.out.println(numOfNeighbors);
//makeNewGeneration(world);
} else {
System.out.println("test3");
break;
}
}
kb.close();
For string comparisons in Java, you need to use String#equals, not ==. Try if (userInput.equals("y")) { ... instead.

How to break a cycle with a String? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I want to give user a possibility to stop inputing new lines in my String array by entering the word "end". I checked out - it really inputs 'end' before the if statement, but for some reason this doesn't break the while cycle:
Scanner scan = new Scanner(System.in);
String[] str = new String[50];
int j=0;
int f = 0;
System.out.println("Input 'end' to see your previous input!");
while(f != -1)
{
the input goes well, but the if statement doesn't work completely
String choice = scan.nextLine();
if(choice == "end")
{
f = -1;
}
else
{
str[j] = choice;
j++;
}
}
Use
choice.equals("end") instead of ==
See here
Also see equalsIgnoreCase() method for string comparision

variable may not have been initialized... how to increase scope? [duplicate]

This question already has answers here:
Variable might not have been initialized error
(12 answers)
Closed 8 years ago.
I keep getting an error:
error: variable aryResponse might not have been initialized
if(answers.charAt(i) == aryResponse[i].charAt(i))
I think it's because I'm initializing the variable within a while loop. However I dont' know how to fix this?
How do I increase the scope of the variable, while I need it to be initiliazed to a value given by the loop?
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
class ExamAnalysis
{
public static void main(String[] args) throws FileNotFoundException
{
Scanner in = new Scanner(System.in);
System.out.println("Welcome to Exam Analysis. Let's begin ...");
System.out.println();
System.out.println();
System.out.print("Please type the correct answers to the exam questions, one right af$
String answers = in.nextLine();
int answersLength = answers.length();
System.out.println();
System.out.print("What is the name of the file containing each student's responses to$
String temp = in.nextLine();
File file = new File(temp);
Scanner in2 = new Scanner(file);
/*Code Relevant To This Question Begins Here */
int lines = 0;
String[] aryResponse;
while (in2.hasNextLine())
{
String line = in2.nextLine();
aryResponse = new String[lines];
aryResponse[lines] = line;
System.out.println("Student #" + lines + "'s responses: " + line);
lines++;
}
System.out.println("We have reached \"end of file!\"");
System.out.println();
System.out.println("Thank you for the data on " + lines + " students. Here's the ana$
int[] aryCorrect = new int[lines];
for (int i = 0; i < answersLength; i++)
{
if(answers.charAt(i) == aryResponse[i].charAt(i))
{
aryCorrect[i] ++;
}
}
}
}
Change this
String[] aryResponse;
to
String[] aryResponse = null;
And remember to test that aryResponse isn't null,
if (aryResponse != null) {
for (int i = 0; i < answersLength; i++) {
if (answers.charAt(i) == aryResponse[i].charAt(i)) { // what is this testing?
aryCorrect[i]++;
}
}
}
This is necessary because
while (in2.hasNextLine()) { // <-- might not be true
// so this doesn't happen.
}
If I understood your code, aryResponse has a number of lines, if the variable was never initialized inside the while loop it means you have 0 lines, so, it would be enough to do two things:
1- initialize aryresponse to null:
String[] aryResponse = null;
2 - add this line at the end of your while loop:
if(aryResponse == null) aryResponse = new String[0];
Just initialize it outside the loop
String[] aryResponse=null;
Now allocate memory to aryResponse inside loop
aryResponse = new String[n]; //n is the size of array

Categories

Resources