'How do I display an output where a user would be the one to give the elements of an array and then choose the next step he wants to do? --Java
I am so confused right now. I have very little knowledge in programming and just managed to do this task regarding arrays with much difficulty.. This is what I was tasked to do: (Please ignore the commented statements at the bottom, I haven't gotten to do those yet.)
public interface MyArrayInterface {
public String toString();
//this returns true if the item is was added in the array, otherwise false
public boolean add(int x);
// this returns true if the array is full
public boolean isFull();
//will return the index of the element if the element x is in the array, otherwise returns -1
public int locate(int x);
//this returns true if the arrays is empty
public boolean isEmpty();
//will return true if the item x is in the array otherwise false
public boolean search(int x);
//clears all the elements in the array
public void clear();
//this returns true if the item is removed from the array, otherwise false
public boolean remove(int x);
//insert data to array at given location
//public boolean insertAt(int x, int loc);
/*
public boolean insertAtBegining(int x); //insert element at the beginning of the array
//public boolean bubbleSort();
public boolean removeAt(int loc); //remove an element from a specific location in the array
public boolean removeAll(int x); //remove all elements in an array
public int getSum(); //get sum of all elements in the array
public int getMax(); //get maximum value of elements in the array
public int getMin(); //get minimum value of elements in the array
public boolean fillArray(int otherArray[]); //replay the existing array with this
*/
}
I've only coded these methods separately based on the teacher's instructions and description on what each method is supposed to do.. Now I'm having a problem on using them all together to generate a kind of menu where the user is asked to input the elements of the array and then choose the next step they want to take i.e. add elements to the array or remove elements from the array.. This is what I've finished so far:
public class MyArrayImplementation implements MyArrayInterface {
private int [] arr; // this will be our actual array
private int count; // this will be used to determine the number of elements inside our array
//default constructor
public MyArrayImplementation() {
arr = new int [10];
count = 0;
}
public MyArrayImplementation(int size) {
arr = new int[size];
count=0;
}
//this will return a string equivalent of the array
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("[");
for(int i=0;i<count;i++) {
sb.append(arr[i]);
if(i<count-1) {
sb.append(",");
}
}
sb.append("]");
return sb.toString();
}
//this returns true if the item is was added in the array, otherwise false
public boolean add(int x) {
boolean retVal=false;
if(isFull()) {
System.out.println("The array is full!");
}
else {
arr[count++]=x;
retVal = true;
System.out.println("Item "+x+" successfully added !");
}
return retVal;
}
// this returns true if the array is full
public boolean isFull() {
return (arr.length == count);
}
//this returns true if the arrays is empty
public boolean isEmpty() {
return (count==0);
}
//will return the index of the element if the element x is in the array, otherwise returns -1
public int locate(int x) {
int loc = -1;
for(int i=0;i<count;i++) {
if(x == arr[i]) {
loc = i;
break;
}
}
return loc;
}
//will return true if the item x is in the array otherwise false
public boolean search(int x) {
return (locate(x)!= -1);
}
//clears all the elements in the array
public void clear() {
/*
for(int i=0;i<count;i++) {
arr[i]=0;
}*/
count=0;
}
//this returns true if the item is removed from the array, otherwise false
public boolean remove(int x) {
boolean retVal = false;
if(isEmpty()) {
System.out.println("The array is empty, cannot remove an item!");
}
else {
if(search(x)) {
if (locate(x) < count-1) {
for(int i=locate(x);i<count-1;i++) {
arr[i]= arr[i+1];
}
}
System.out.println("Item "+x+" is succesfully removed!");
count--;
retVal = true;
}
else {
System.out.println("Item "+x+" is not found in the array!");
}
}
return retVal;
}
}
I've only been able to display the output through this code:
public class TestArray {
public static void main(String[] args) {
MyArrayImplementation ar1 = new MyArrayImplementation();
System.out.println(ar1);
ar1.add(10);
System.out.println(ar1);
ar1.add(15);
System.out.println(ar1);
ar1.add(20);
System.out.println(ar1);
System.out.println("Locating 30 in the array:"+ar1.locate(30));
System.out.println("Searching 30:"+ar1.search(30));
ar1.add(10);
ar1.add(30);
ar1.add(40);
ar1.add(20);
System.out.println(ar1);
ar1.remove(20);
System.out.println(ar1);
ar1.remove(20);
System.out.println(ar1);
}
}
How do I get this to display an output without having to type ar1.add or ar1.remove on the editor? Like, the user will have to input those... Also, if C has scanf, what's the equivalent of that in java?
I really do hope I'm making sense... HELPPP T_T
Solution 1:[1]
If you want to let users type in input, I'd utilize the Scanner class
For getting the array initialized:
Scanner scan = new Scanner(System.in);
System.out.println("Enter array: "); // Asks for the array
String str = scan.nextLine();
// Pass str for integers to store in the array
Then set up a loop reading the nextLine, e.g.
System.out.println("Instructions for what you can input ... or just hit enter to end");
str = scan.nextLine();
// do a loop until input is empty
// inside loop parse the string
I highly reccommend that you check out https://www.w3schools.com/java/java_user_input.asp
If this doesn't answer your question, please revise so that we can give a more accurate answer - and welcome to Stack Overflow :-)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Lars |
