Java if (string=="0") check doesn't work [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I want to make a program that copies strings into a vector until the hold string is set to a certain value, at which point the program should print out the elements of the array.
I'm not sure how the in.nextline() function works, so it could be that I'm not erasing previous entries from hold, or is it that my if(hold=="0") check is simply not valid java?
import java.util.*;
import java.util.Scanner;
public class startingPoint
{
public static void main(String roark[])
{
String hold;
boolean finished=false;
Scanner in = new Scanner(System.in);
Vector<String> vec = new Vector<String>();
while(finished==false){
System.out.print("Enter the string you'd like to save, or enter 0 to print out saved strings\n");
hold=in.nextLine();
if(hold=="0"){
for(int looper=0;looper<vec.size();looper++){
System.out.print(vec.get(looper));
System.out.print("\n");
}
finished=true;
}else{
vec.add(hold);
}
}
}
}

You need to use equals method:
if("0".equals(hold){

Related

Why isn't this program ending? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed last year.
I'm a beginner in coding and would appreciate any feedback. This code seemed easy but I'm not sure why the "STOP" exit condition isn't being fulfilled.
Code:
```
import java.util.Scanner;
import java.util.ArrayList;
public class U7_L2_Activity_One{
public static void main(String[] args){
System.out.println("Please enter words, enter STOP to stop the loop.");
Scanner scan=new Scanner(System.in);
ArrayList<String> list=new ArrayList<String>();
String x;
while(true){
x = scan.nextLine();
if (x = = "STOP")
{
System.out.println(list);
for (int i = 0; i <= list.size()- 1; i++){
System.out.println(list.get(i));
}
System.exit(0);
}
else{
list.add(x);}
}
}
}
So basically you want your program to end if the variable X is equal to the word "STOP" right? So in that case you need to change de comparison inside if.
In java you can't compare strings using the operator ==. When you do that you're comparing if the objects reference are equal.
Solution:
To compare Strings you can use YourVariable.equals(SomeString). In your case just change x=="STOP" to x.equals("STOP")
For reference:
Strings Method Equals: https://www.w3schools.com/java/ref_string_equals.asp

How to check whether the user entered the appropriate string? (Java) [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 2 years ago.
package com.company;
import java.lang.String;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String str = "";
do{
Scanner input = new Scanner(System.in);
System.out.print("Input: ");
str = input.nextLine();
}while(str != "key123");
System.out.print("Good!");
}
}
The user must enter the correct key, but the code doesn't work and I can't figure out why?
Screen shot:
enter image description here
The == operator works correctly all the time for primitives only. That's char, boolean, int double, float, byte, long, short. It does not work for classes like String or Object.
Instead use: object.equals(anotherObject); like so
String str = "";
Scanner input = new Scanner(System.in);
do {
System.out.print("Input: ");
str = input.nextLine();
} while (!str.equals("key123"));
System.out.println("Good!");
System.out.println(str == "key123"); // false
System.out.println(str.equals("key123")); // true
And avoid creating a new object in a loop every time it iterates unless you absolutely have to. Object creation takes memory.

How to end do while loop with user input with if statement [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 3 years ago.
import java.util.Scanner;
public class main {
public static void main(String[] args) {
boolean a = true;
do {
Scanner input = new Scanner(System.in);
System.out.println("Press any on keyboard:");
String keys = input.nextLine();
System.out.println("You pressed:");
System.out.println(keys);
System.out.println("Your hash is:");
String B = "#B";
String hash = B+keys;
System.out.println(hash);
System.out.println("To end loop press f");
//End Loop
Scanner exit = new Scanner(System.in);
String end = exit.nextLine();
if (end=="f") {
a=false;
}
}
while(a);
}
}
I've been using python and I decided to start learning java since android studio requires it. I'm learning how to do loops again. I can't get this to work. I already looked this up I couldn't find it. How would I end this by pressing 'f'? My thought process was that once it was done going though the first lines of the do loop, it would go though the if statement changing the value of a ending the loop.
use break statement under if(){} body. also your == comparison will give false, use str1.equals(str2) for comparison.
Your problem is you are comparing strings with ==.You have to use equals to write correct if statement.
if (end.equals("f")){...}
You could use the below code to check
if (end.equals("f")) { // end == "f" , it check the reference.
a = false;
}

Searching element in String array in JAVA? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I have made a JAVA program where I have initialized a 1-D String array. I have used for loop to search any inputted String if it exists in the array(Scanner Class).
Here is the source code :-
import java.util.*;
class search
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the name to search :-");
String s=sc.nextLine();
String array[]={"Roger","John","Ford","Randy","Bacon","Francis"};
int flag=0,i;
for(i=0;i<6;i++)
{
if(s==array[i])
{
flag=1;
break;
}
}
if(flag==1)
System.out.println("The name "+s+" Exists");
else
System.out.println("The name "+s+" does not Exists");
}
}
The class even compiles successfully, but when I enter a valid string(say- Roger), the output is The name Roger does not Exists.
Please help me out with this issue, and for this I shall be grateful to you.
Thanking You,
J.K. Jha,
01.09.2018.
You are confusing == and equals
Since String is an object == just checks for if the references are same instead of actual contents
You should use String.equals() instead
Changes your if condition
for(i=0;i<6;i++)
{
if(s.equals(array[i]))
{
flag=1;
break;
}
}

Reading numbers from text files online [duplicate]

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 7 years ago.
I'm trying to take numbers (integers) from a website and put them into an array for further manipulation. Here is what I have so far
import java.io.IOException;
import java.net.URL;
import java.util.Scanner;
public class Scores {
public static void main(String[] args) throws IOException{
URL url = new URL("http://cs.armstrong.edu/liang/data/Scores.txt");
Scanner input = new Scanner(url.openStream());
int []score = new int [100];
int i = 0;
while (input.hasNextInt()) {
score[i++] = input.nextInt();
System.out.println(score);
}
}
}
The problem is all of the numbers in the array come back as [I#6bc7c054, any help would be appreciated.
System.out.println(score[i]);
you were printing the array itself

Categories