+ 1

Can any find whats wrong with my code please cant figure it out. Thanks

import java.util.Scanner; public class Pc { String devices []; static void Message(String x) {System.out.println(x);} public static void main(String[] args) { Pc pc = new Pc(); Scanner scan = new Scanner(System.in); String x = scan.next(); pc.devices = {"keyboard","mouse","monitor"}; if(x.equalsIgnoreCase(pc.devices[0]|| pc.devices[1]||pc.devices[2])) {Message(x + " is a peripheral device");} } }

8th Aug 2017, 9:16 PM
D_Stark
D_Stark - avatar
5 ответов
+ 1
First you are initializing your array incorrectly. In order to use the {} type of initialization you need to do so at the time of declaration. Otherwise, you are trying to re-initialize the array after it has been declared with zero length. Also in your if statement you need to do each check separately. You can't pass a bunch of OR'd strings into a method that only takes 1 String as a parameter, as it would evaluate to a boolean anyhow. The following are all valid ways to initialize the array and the if statement is fixed: import java.util.Scanner; public class Pc { String[] devices = new String[3]; static void Message(String x) { System.out.println(x); } public static void main(String[] args) { Pc pc = new Pc(); Scanner scan = new Scanner(System.in); String x = scan.next(); pc.devices[0] = "keyboard"; pc.devices[1] = "mouse"; pc.devices[2] = "monitor"; if(x.equalsIgnoreCase(pc.devices[0]) || x.equalsIgnoreCase(pc.devices[1]) || x.equalsIgnoreCase(pc.devices[2])) { Message(x + " is a peripheral device"); } } } import java.util.Scanner; public class Pc { String[] devices = {"keyboard","mouse","monitor"}; static void Message(String x) { System.out.println(x); } public static void main(String[] args) { Pc pc = new Pc(); Scanner scan = new Scanner(System.in); String x = scan.next(); if(x.equalsIgnoreCase(pc.devices[0]) || x.equalsIgnoreCase(pc.devices[1]) || x.equalsIgnoreCase(pc.devices[2])) { Message(x + " is a peripheral device"); } } }
8th Aug 2017, 9:46 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Thanks you thats so helpful 😊
8th Aug 2017, 9:51 PM
D_Stark
D_Stark - avatar
+ 1
thanks chaotic! im just curious about somthing do have to declare the values in class to use an array in main?
8th Aug 2017, 9:41 PM
D_Stark
D_Stark - avatar
+ 1
import java.util.Scanner; public class Pc { String[] devices; static void Message(String x) { System.out.println(x); } public static void main(String[] args) { Pc pc = new Pc(); Scanner scan = new Scanner(System.in); String x = scan.next(); pc.devices = new String[]{"keyboard", "mouse", "monitor"}; if(x.equalsIgnoreCase(pc.devices[0]) || x.equalsIgnoreCase(pc.devices[1]) || x.equalsIgnoreCase(pc.devices[2])) { Message(x + " is a peripheral device"); } } }
8th Aug 2017, 9:47 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Your welcome. Here's a bit more information on arrays in Java and how you can initialize them. Just remember that you can't use the shortcut syntax (curly braces method) without the use of the new keyword anywhere other than at declaration. https://stackoverflow.com/questions/1200621/how-do-i-declare-and-initialize-an-array-in-java https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
8th Aug 2017, 10:00 PM
ChaoticDawg
ChaoticDawg - avatar