You have an extraneous semicolon after your if, which effectively makes it
if (x.equals("0")) { }
break;
Answer from Jon Newmuis on Stack OverflowVideos
Instantiate DVDProperty inside the loop. Currently you are reusing the same instance, and thus overriding its properties:
while (res.next()) {
DVDProperty context = new DVDProperty();
...
}
You have to create new object of type DVDProperty for every record. At this time you change the same object (context) in every iteration. Try:
List<DVDProperty> DVDList = new ArrayList<DVDProperty>();
while (res.next()) {
int i = res.getInt("idnew_table");
String s = res.getString("dvdName");
DVDProperty context = new DVDProperty();
context.setDVDId(i);
context.setDVDName(s);
DVDList.add(context);
}
I think fundamentally the code is correct. I would check your inputs and make sure they're really what you think.
I would perhaps rewrite your loop as:
for (String s : contain) {
if (s.contains(code)) {
// found it
}
}
to make use of the object iterators (the above assumes you have an ArrayList<String>). And perhaps rename contain. It's not very clear what this is.
The code is correct assuming List of strings. I have not modified any of your source code just to give you idea that it works fine.
List<String> contain = new ArrayList<String>();
contain.add("HPDH-1,001, Check-out date: 7/7/7");
contain.add("JTI-1,001, Check-out date: 7/7/7");
String code = "JTI-1 ";
for (int i = 0; i < contain.size(); i++) {
if (contain.get(i).contains(code.trim())) {<---Use trim it is possible that code may have extra space
System.out.println(contain.get(i));
}
}
Separate the logic of asking the question for more information from that of updating the list
String decision = "No";
do {
System.out.println("Would you like to add another book?");
decision = keybd.nextLine();
if (decision.equals("Yes")) {
// Get information about book
String anyTitle = keybd.nextLine();
String anyAuthor = keybd.nextLine();
addBooks(anyTitle,anyAuthor);
}
} while (decision.equals("Yes"));
You could go a step further, making a addBook method which prompts the user for the informaiton...
public void addBook() {
String anyTitle = keybd.nextLine();
String anyAuthor = keybd.nextLine();
addBooks(anyTitle,anyAuthor);
}
Then you could just call addBook from the while-loop
do {
System.out.println("Would you like to add another book?");
String decision = keybd.nextLine();
if (decision.equals("Yes")) {
addBook();
}
} while (decision.equals("Yes"));
Further decoupling your code, allowing you to call addBook whenever you want to prompt the user for information about the book and adding it to the list
If I understand your various snippets of code, I would strongly recommend you consider something that calls your method setOneBook... but I suggest you change the method visibility (and name) -
private void addOneBook(String anyTitle, String anyAuthor){
bookList.add (new Book(anyTitle,anyAuthor));
}
This way you have used encapsulation to hide the method that adds a book. Next, change the public method. You are adding multiple books, so don't pass in a title or an author -
public void addBooks() {
do {
String anyTitle = keybd.nextLine();
String anyAuthor = keybd.nextLine();
addOneBook(anyTitle, anyAuthor);
System.out.println("Would you like to add another book?");
} while (keybd.next().equalsIgnoreCase("yes"));
}
Try like below
ArrayList<String[]> data = new ArrayList<String[]>();
while(scanner.hasNext()){
String[] text = scanner.nextLine().split(","); //assuming comma separated text in the line
data.add(text);
}
printing the content
for (String[] text : data) {
System.out.println(Arrays.toString(text));
}
Follow these high level steps:
- Create a new instance of ArrayList
- Open file and start reading
- Read each row and split text into each cell.
- Build an array of the data from the row
- Add the row to ArrayList
- Clean up any resources.