Print all strings of array [duplicate] - java

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 2 years ago.
Very new to Java and programming in general. trying to figure out how to print the contents of my array
public class GameClass {
public static void main(String args[]) {
String [] gameList = new String [] {"Call of Duty", "Skyrim", "Overwatch", "GTA", "Castlevania", "Resident Evil", "HALO", "Battlefield", "Gears of War", "Fallout"};
System.out.println(gameList[]);
}
}

You can use the util package for one line printing
System.out.println(java.util.Arrays.toString(gameList));

Should do the trick.
for(int i = 0 ; i <gameList.length; i ++)
System.out.print(gameList[i]+" ");

Just iterate each element and print:
for(String s : gameList){
System.out.println(s);
}

You can print out your array of games by using the following loop:
for(String game : gameList){
System.out.println(game);
}
This runs through each item in your gameList array and prints it.

Related

Check if input is any of the values in an array [duplicate]

This question already has answers here:
How do I determine whether an array contains a particular value in Java?
(30 answers)
Closed 2 years ago.
I want to be able to check if the string input matches any of the strings in array, and then run only if it matches any of the strings.
String[] list = {"hey","hello"};
if (input == anyofthestringsinarray) {
}
Thought something like if(input.equalsIgnoreCase(list)) {} could work, but dont. Any tips?
Try this:
public static void stringContainsItemFromList(String inputStr, String[] items)
{
for(int i =0; i < items.length; i++)
{
if(inputStr.contains(items[i]))
{
// if present do something
}
}
}

Java - Why is the output of this enum array null? [duplicate]

This question already has answers here:
Default or initial value for a java enum array
(5 answers)
Can we assume default array values in Java? for example, assume that an int array is set to all zeros?
(4 answers)
Closed 4 years ago.
I created an enum array like this:
enum MyEnums {
FIRST, SECOND, THIRD, FOURTH;
}
public class MyEnumsTest {
public static void main(String[] args) throws Exception {
MyEnums[] myEnums = new MyEnums[4];
for(int i = 0; i< myEnums.length; i++) {
System.out.println(myEnums[i]);
}
}
}
But why is the output null, null, null and null? And how can I get the element by myEnums[i].FIRST?
What you're doing here is creating an array of MyEnums, and the default value is null (you haven't set the values in the array).
If you wanted to print out the enum values you can use the values() method:
for(MyEnums en : MyEnums.values()) {
System.out.println(en);
}
or (more like your original code)
for(int i = 0; i < MyEnums.values().length; i++) {
System.out.println(MyEnums.values()[i]);
}
This prints:
FIRST
SECOND
THIRD
FOURTH

how to check if the variable is null? [duplicate]

This question already has answers here:
Check whether a String is not Null and not Empty
(35 answers)
Closed 5 years ago.
I know this doesnt work but how can I know if the user added even one char?
public class Program
import java.util.Scanner;
{
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
String b = a.nextLine();
//I know this doesnt work but how can I know if the user added even one char?
if (b!=null){
System.out.println(b);
}
}
}
you can use .equals("") or .isEmpty()
check check if the variable is null
You can do this
if (!b.isEmpty()) {
System.out.println(b);
}

Java palindrome [duplicate]

This question already has answers here:
Check string for palindrome
(42 answers)
Closed 7 years ago.
i have beginner question in java, as i don't know what is wrong with my array, because i just can't index through them. yes i know there is another faster way to check palindrome but pls take a look.
public boolean palindrom (String a){
List<String> normal = new ArrayList<String>();
List<String> modified = new ArrayList<String>();
for (String x: a.split("")){
normal.add(x);
}
for (String x:new StringBuilder(a).reverse().toString().split("")){
modified.add(x);
}
for (int i=0;i<a.split("").length;i++){
if (normal[i]!=modified[i]){
//in this line above is error as it doesnt recognise "normal" and "modified" arrays
return false;
}
}
return true;
Those are not Arrays but ArrayList
To get element i you have to do normal.get(i)
https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
Lists aren't indexed in the same way arrays are. Rather than use normal[i], you would use normal.get(i).
for (int i=0;i<a.split("").length;i++){
if (normal.get(i) != modified.get(i)){
return false;
}
}

Java: Array List Printing object reference instead of array [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 am very much a rookie when it comes to coding. What I'm trying to accomplish is to load grades from a txt file and put them into an existing array.
I have actually solved this. and In the program the array does retain the values. But when I got to print them I get the object reference aka [I#5c647e05 rather than the array. Below is my code.
public static void main(String[] args) {
int[] list = new int[15];
Scanner in = null; // create a scanner object
loadGrades(in, list);
System.out.println(list);
}
public static void loadGrades(Scanner in, int[] list) {
int grades; // int variable
try { // try
in = new Scanner(new FileReader("Proj5Data.txt"));// change filename
} catch (FileNotFoundException ex) { // catch
System.out.println("File not found");
System.exit(1);
}
for (int i = 0; i < list.length; i++) {
grades = in.nextInt(); // var
list[i] = grades ;// put the int in the array at the counter value
}
in.close();
}
Print results as:
[I#5c647e05
BUILD SUCCESSFUL (total time: 0 seconds)
When you print an array behind the scenes the method toString() is being called and it doesn't print the content of the array (which is too bad IMO). But you can easily get over it using the library Arrays:
System.out.println(Arrays.toString(list));
You can't print the contents of a list like that in Java. You need to loop over the contents of the array and print them individually.
for(int i : list){
System.out.println(i);
}
System.out.println(list) uses a .toString() method to resolve what to print.
You can either use Arrays.toString(list) or print it one by one.
So All the methods you guys gave me did indeed work. But I ended up just using printf to print them out. Thanks for the help though.

Categories