So I have this assignment, and it says to create a arraylist using standard input, and constantly prompt the user for commands. Now, I understand how to add commands, WITHIN the code, but I dont understand how to take what the user puts in, and turn that into a command in the arraylist. Here's what I got so far:
import java.util.*;
import java.util.Scanner;
public class assignment216{
public static void main (String args[]){
ArrayList<String> names = new ArrayList<String>();
Scanner s = new Scanner(System.in);
Scanner n = new Scanner(System.in);
System.out.println("Enter a command:");
String command = s.nextLine();
System.out.println("If you entered 'add', then enter a name:");
String input = n.nextLine();
}
}
EDIT New Code:
import java.util.*;
import java.util.Scanner;
public class assignment216{
public static void main (String args[]){
ArrayList<String> names = new ArrayList<String>();
Scanner e = new Scanner(System.in);
System.out.println("Enter command, or type 'quit' to exit");
String quit = e.next();
boolean exit = quit;
if(exit = true){
System.exit();
}
}
}
You can request that the user types in his commands and then check using the method from the String class equalsIgnoreCase() and compare it and see if it is the same as add, remove, etc. (whatever commands you need). Use if-else statements to implement the logic. For example if user input resolves to add, then you add an element. If user input is not any of the common functions, then print message that there was an error and so on. I cannot type out the answer in code as this is an assignment.
import java.util.*;
import java.util.Scanner;
public class assignment216{
public static void main (String args[]){
ArrayList<String> names = new ArrayList<String>();
Scanner e = new Scanner(System.in);
System.out.println("Enter command, or type 'quit' to exit \n");
while (!e.nextLine().equalsIgnoreCase("quit")) { // As long as input not equal to quit
if (e.nextLine().equalsIgnoreCase("add") {
System.out.println("Enter name to add:\n");
String name = e.nextLine();
names.add(name);
}
if (e.nextLine().equalsIgnoreCase("remove") {
System.out.println("Enter name to remove:\n");
String name = e.nextLine();
names.remove(name);
}
}
if(e.nextLine().equalsIgnoreCase("quit"){
scanner.close();
System.exit();
}
}
}
Check out the add() method in the java.util.List interface. Might help.
Related
So I am using eclipse to practice coding using java and till now it was doing great, until I wrote this program:
import java.util.*;
public class cars {
String name;
int dom;
Scanner in = new Scanner(System.in);
void takedata()
{
name=in.next();
dom=in.nextInt();
}
void displaydata()
{
System.out.print("Enter the name of the car"+name);
System.out.print("Enter the Date of Manufacture of the car"+dom);
}
public static void main(String[] args)
{
cars x = new cars();
cars y = new cars();
cars z = new cars();
x.takedata();
y.takedata();
z.takedata();
x.displaydata();
y.displaydata();
z.displaydata();
}
}
whenever I am trying to run the code it is showing me nothing.
I need help.
It wont show anything. Your code will wait till you enter the care name. So, first you need to provide input as your takeData() method is getting called before displayData(). Modify your takeData() method as shown below:
void takedata()
{
System.out.println("Enter Name of the Car: ");
name=in.next();
System.out.println("Enter Date of Manufacture: ");
dom=in.nextInt();
}
Then after executing your program your will be able to see below message:
Enter Name of the Car:
i was running the PMD tool for my code and checking it for a simple program that inputs two strings for beginning purposes.when i do something like a=in.nextLine(); it shows object not created locally.Can you help?
import java.util.Scanner;
class Test {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
String a, b;
a = in.nextLine();
b = in.nextLine();
in.close();
}
}
I am very new in programming into Java.
My question is that I have a code (see below) and I want to compare them with if statement. An errors occur at line 9 (string test) and 11(if(test.equals). I completely do not have idea.
I have made a code with int and it works perfect, but that.
package bucky;
import java.util.Scanner;
class apples {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
Scanner sc = new Scanner(System.in);
String test = sc.nextLine();
if (test.equals("YES")) {
System.out.println("YES");
} else {
System.out.println("TIS IS ELSE");
}
}
}
You are almost there... define YES as string and that it
String test = sc.nextLine();
String YES = "yes";
if (test.equals(YES)) {
or even better use equalsIgnoreCase() so you can get rid off the case sensitive input
if (test.equalsIgnorecase(YES))
I've been doing a ton of research on this for the past few hours, with no luck. I am pretty sure this is a problem with .next() or .nextLine() (according to my searches). However, nothing has helped me solve my problem.
When I run the code below, I have to type in input twice, and only one of the inputs is subsequently added to the arrayList (which can be seen when you print the contents of the arrayList).
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
public class Tester{
public static void main(String[] args) {
AddStrings();
}
public static void AddStrings() {
Scanner console = new Scanner(System.in);
ArrayList<String> strings = new ArrayList<String>(); //this arraylist will hold the inputs the user types in in the while loop below
while(true) {
System.out.println("Input file name (no spaces) (type done to finish): ");
if(console.next().equals("done")) break;
//console.nextLine(); /*according to my observations, with every use of .next() or .nextLine(), I am required to type in the same input one more time
//* however, all my google/stackoverflow/ reddit searches said to include
//* a .nextLine() */
//String inputs = console.next(); //.next makes me type input twice, .nextLine only makes me do it once, but doesn't add anything to arrayList
strings.add(console.next());
}
System.out.println(strings); //for testing purposes
console.close();
}
}
Problem with your code is that you are doing console.next() two times.
1st Inside if condition and
2nd while adding to ArrayList.
Correct Code :
public class TestClass{
public static void main(String[] args) {
AddStrings();
}
public static void AddStrings() {
Scanner console = new Scanner(System.in);
ArrayList<String> strings = new ArrayList<String>(); //this arraylist will hold the inputs the user types in in the while loop below
while(true) {
System.out.println("Input file name (no spaces) (type done to finish): ");
String input = console.next();
if(input.equals("done")) break;
strings.add(input);
System.out.println(strings);
}
System.out.println(strings); //for testing purposes
console.close();
}
}
In your code, you are asking for two words to be inserted. Just remove one of them.
Use it this way:
String choice = console.next();
if (choince.equals('done')) break;
strings.add(choice);
Java Project
public ArrayShoppingList()
{
// initialise instance variables
super();
}
//public void addItem(int itemPosition,String item){
// super.add(itemPosition,item);
// }
public void addItem(){
System.out.println("Please enter the item you wish to enter into the shopping
List");
Scanner reader1 = new Scanner(System.in);
String item = reader1.next();
super.add(super.size(),item);
}
public void getPosition(){
System.out.println("Please enter the item name that you wish to find
theposition of");
Scanner reader1 = new Scanner(System.in);
String item = reader1.next();
super.indexOf(item);
}
public void removeItem(){
System.out.println("Please enter the item number that you wish to remove");
Scanner reader1 = new Scanner(System.in);
String item = reader1.next();
int itemIndex = super.indexOf(item);
super.remove(itemIndex);
}
I want to know how to test such methods in a Test Class that ask for user input.
The methods call other methods from an ArrayLinearList and pass data that the user has entered in. I want to create code that simulates what the user might enter in.
You can use frameworks like Mockito or Powermock to mock the Scanner. If the class then calls the Scanner for input, you can let your mock return some strings which will be handled as user input by your class. Your class will not see a difference between the "real" scanner and your mock implementation.
See also:
https://code.google.com/p/mockito/
https://code.google.com/p/powermock/
You can use System.setIn(InputSteam)
Here you can specify your own inputstream and add the input you want to it.
Use System.setIn for Unitesting Scanner. For example:
String input = "Your input data";
System.setIn(new ByteArrayInputStream(input.getBytes()));
Scanner scanner = new Scanner(System.in);
System.out.println(scanner.nextLine());
Output:
Your input data