Checking the existence of a certain array [duplicate] - java

This question already has answers here:
How do I determine whether an array contains a particular value in Java?
(30 answers)
Closed 8 years ago.
What is the best way to check the existence of a certain array name? for example, if i had a array called:
String array1[] = new String[]{"abc", "def", "ghi"};
what is the best way to check if array1 exists or not?
Additionally, is there a way to see if the string "abc" exists inside the array if we don't know which index "abc" is in?
EDIT: I'm planning to have the user input a string and check if the input string matches with any of the string array variable names

To check whether the array exists you could use
if(array != null){
//some code for when the array exists
} else {
//some other code for when the array does not exist.
}
If you want to check, whether an array has more than 0 entries, please edit your question.
For checking if the string "abc" is part of the array, you can use
if(Arrays.asList(array1).contains("abc")){...}
To learn more about arrays, you should read the documentation for arrays:
http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html

Related

Java - How to get items out of a list with String Arrays [duplicate]

This question already has answers here:
Ways to iterate over a list in Java
(13 answers)
Closed 3 years ago.
How do I get a specific item out of a "List" at a specific index?
private List<String[]> rows;
You have to use get(i) with the list and [j] for array :
String value = rows.get(i)[j];
In your case rows.get(i) return a String[], then [j] will return a String with the index j.
For more details, I will invite you to learn about :
Arrays
List
RTFM for the List class
All the methods are listed and explained there, e.g.: get(int):
Returns the element at the specified position in this list.
Also recommended: The Java™ Tutorials
Simply use a enhanced for loop to get items
for (String row: rows) {
// do what you want
// ...
}

String input until dot is entered [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
I want to make a program that keeps getting string input and only stops when "." is entered, any ideas how to make it?
I understand that I need to make a string array, but what's the length I'm gonna give it if I dont know how many strings the user will enter?
This is my first time to use this website, so excuse me for any mistakes.
Thank you.
Edit:
Here's a code that is confusing me. I keep entering dots but the for loop never breaks. Also the length is currently 10, how can I make it unlimited until the input is a dot?
Scanner s=new Scanner(System.in);
String[] x = new String[10];
for(int i=0;i<10;i++)
{
x[i]=s.next();
if(x[i]==".")
break;
}
Scanner s=new Scanner(System.in);
ArrayList<String> inputs = new ArrayList<>();
while (true) {
inputs.add(s.next());
if(inputs.get(inputs.size().equals("."))
break;
}
Remember to import ArrayList. Check the documentation for more information about ArrayList.
In java == is an operator used for comparing references. your new String won't have the same reference as "." which will be created at compilation time.
equals() method is used for comparing if the objects equal. In case of String it compares char by char to see if they are all the same. If yes, it returns true. False otherwise.
Remember to always #Override the public boolean equals() in your class. You have to decide what it means that to object of a certain class are equal and implement it. it'll be useful if you read this topic. It's widely described there.

Check if string array index is equal to string [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
So I'm trying to check a list of account names to see if the username entered by the operator is in the database or not. At the moment I have:
for(int i = 0; i < rowCount; i ++){
System.out.println("Stored in array:" + accounts[i+1]);
System.out.println("name entered:" + LoginPage.usrname);
if(accounts[i+1] == LoginPage.usrname){
System.out.println("match");
}else{
System.out.println("no match");
}
}
I tried messing around with things like indexOf string and can't get anything to work. I'm sure there's a simple solution, just having trouble finding one. I don't understand why I can't compare a String array index to a String variable, seems like ti should be cake.
This is what you're looking for:
if(acounts[i+1].equals(LoginPage.usrname))
Using the == operator on Strings in Java doesn't do what you think it does. It doesn't compare the contents of the Strings, but rather their addresses in memory. The equals method compares the contents of the Strings.
As a note that may help you remember, this isn't anything particularly special about Strings. Strings are objects, and in Java, using == to compare objects of ANY type will present the same problem. If you want to compare the contents of two objects of a custom class you create, you'll have to write an equals method for that class. Strings work exactly the same.
String are unique reference type that behave like value type.
At Java when trying to compare String's using == operator, Java will try to check if both of the reference are equals, Not the strings.
In order to achieve a value type comparison you will be to use one of the following:
Method 1: str1.equals(str)
Method 2: str1.compareTo(str) == 0

how to compare two string that read from csv file and saved in string array [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I compare strings in Java?
I read some records from a csv file and store them into a string type array, then save each field in proper variable after convert them. In a part of code I have to compare a field in array with one of those variables with string type. both these arrays filled from a csv file.
int count=0;
String name=resourceArray[i][j+1] ;
while(machineArray[count][0]==name)
{
machineID=Integer.parseInt(machineArray[count][1]);
machinePe=Integer.parseInt(machineArray[count][2]);
count++;
}
the problem is 'while' condition never become true. I debug it and I'm sure machineArray[0][0] and 'name' have same value.
Try changing your condition from machineArray[count][0]==name to machineArray[count][0].equals(name).
Essentially you are comparing pointers to two String objects by using == instead of comparing Strings.
To compare Strings you have to use Strings equals method:
machineArray[count][0].equals(name)
Strings do not always have the same identity (although it is common). Try:
machineArray[count][0].equals(name)

Find duplicates in an array and return the duplicates in a temp array [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java Array, Finding Duplicates
arr=[3,4,1,2,1,5,2]
How do i find the duplicates in this array and then return the duplicates in an array?
In the case the result should be result [1,2]
I am programming in Java.
I recommend taking the following steps:
1) Create a HashSeta. The HashSet will contain integers you have read.
2) Iterate through the entire array [0 ... size - 1]. Keep track of what index you are at with an index variable.
3) In each iteration, do a HashSet.contains(arr[index]) operation. If it is true, it is a duplicate. Save this integer somewhere. Add arr[index] to the set.
4) Return the HashSet as the result.
Use nested for loop. take the first element and compare it with all the array. and then send the duplicated ones to a new array by using if/else logic.
I am not giving a code block because it is tagged as homework.

Categories