I am trying to take out only one value from String list(Array) that corresponds user's input(number). If a user input number 4 the result should be 60. Here is the code that i've been trying to finish. Error is incompatible int and string(of course), but other than that i am sure i made mistakes somewhere else as well. I would like array to be as simple as possible without objects and classes.
import java.util.Scanner;
public class FindStringValue{
public static void main(String[] args){
final int[] VALID_NUMBERS=new int[8];
String[] someArray = {"20", "30", "40", "50", "60", "70", "80", "90"};
int inputNumber;
boolean foundIt=false;
int x=0;
Scanner in = new Scanner(System.in);
System.out.print("Enter a number from 0-7 >> ");
inputNumber = in.nextInt();
for(x=0;x<VALID_NUMBERS.length;++x)
if(x==(someArray[x])){
foundIt=true;
break;}
System.out.println("The Number "+inputNumber+" corresponds to String "+someArray[x]);
}
}
Maybe I'm misreading the question, but it sounds like all you want is:
if(inputNumber >= 0 && inputNumber < someArray.length) {
System.out.printf("The Number %d corresponds to String %s\n", inputNumber, someArray[inputNumber]);
} else {
// you don't really show how you want to handle this case...
}
It's not clear what the purpose of VALID_NUMBERS is.
I meant you should replace the for loop completely:
import java.util.Scanner;
public class FindStringValue {
final String[] someArray = {"20", "30", "40", "50", "60", "70", "80", "90"};
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number from 0-7 >> ");
int inputNumber = in.nextInt();
if(inputNumber >= 0 && inputNumber < someArray.length) {
System.out.printf("The Number %d corresponds to String %s\n", inputNumber, someArray[inputNumber]);
} else {
// you don't really show how you want to handle this case...
}
}
}
You dont need to go over the array and do any comparison since you have the element index to be removed .You can achieve your goal by the following snippet:
public static void main(String[] args) {
String[] someArray = {"20", "30", "40", "50", "60", "70", "80", "90"};
int inputNumber;
boolean foundIt = false;
Scanner in = new Scanner(System.in);
System.out.print("Enter a number from 0-7 >> ");
inputNumber = in.nextInt();
in.close();
LinkedList<String> list = new LinkedList(Arrays.asList(someArray));
list.remove(inputNumber);
String []newArray = list.toArray(new String[list.size()]);
System.out.println("The Number " + inputNumber + " corresponds to String " + someArray[inputNumber]);
}
I'd do away with the array altogether and just use a List as shown here.
public static void main(String[] args){
List<String> numbers = Arrays.asList("20", "30", "40", "50", "60", "70", "80", "90");
int inputNumber;
Scanner in = new Scanner(System.in);
System.out.print("Enter a number from 0-7 >> ");
inputNumber = in.nextInt();
in.close();
if(inputNumber < numbers.size()) //validity check of input value
{
System.out.println("The Number "+ inputNumber + " corresponds to String "+ numbers.get(inputNumber));
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.