Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
this is the way I am adding the data to the arrayList
System.out.println("please select part/s (-1 for return to the main menu)");
//array to store multiple inputs
String[] part = scanner.nextLine().split(",");
//adding customer computer parts to its order
for (int k = 0; k < part.length; k++) {
if (Integer.parseInt(part[k]) == -1) {
//thats just to break the loop but its not creating any problem
bool = false;
} else {
os.ord.addComputerPart(os.computerParts.get(Integer.parseInt(part[k])));
}
}
here is the method to add the inputs the ArrayList
public void addComputerPart(ComputerPart computerpart ){
parts.add(computerpart);
totalPrice += computerpart.getPrice();
}
Now here is the method to remove the data from the arrayList
//os is the class object and ord is the order class object which is the seperate class and order class contains the parts arraylist
System.out.println( os.ord.getParts().size());
//printing the information about the customer and the order
System.out.println("here is the summary of your current order");
System.out.println(os.ord);
Scanner scxx = new Scanner(System.in);
//using the array to take more than one inputs
System.out.println("Please select part/s to be removed from the order (-1 for return to the main menu)");
String[] part = scxx.nextLine().split(",");
//deleting customer computer parts from its order
for (int k = 0; k < part.length; k++) {
if (Integer.parseInt(part[k]) == -1) {
delete = false;
} else {
os.ord.removeComputerPart(Integer.parseInt(part[k]));
}
}
Now in again order class I have a method to remove the data from the arrayList which removes the data
public void removeComputerPart(int index){
parts.remove(index);
totalPrice-= parts.get(index).getPrice();
}
Now problem is that when i remove the element from the arraylist it give me indexoutofbound error
4 --> as u can see that while printing the size of the arraylist i am getting 4 elements but when i delete the 4th element it gives me error
here is the summary of your current order
(0): Product ID : INTCPU94496 Brand : INT Model : 9700K Price : 462.0 Core : i7
Socket : Intel
not compatible with :
(1): Product ID : INTCPU12673 Brand : INT Model : 9700F Price : 396.0 Core : i7
Socket : Intel
not compatible with :
(2): Product ID : INTCPU72675 Brand : INT Model : 9900K Price : 591.0 Core : i9
Socket : Intel
not compatible with :
(3): Product ID : AMDCPU54823 Brand : AMD Model : Ryzen 2200 Price : 200.0 Core : 4
Socket : AMD
not compatible with :
Order Id : O104181 Total Price : $1649.0
Member customer discount : $82.45 total price after discount : $1566.55
Please select part/s to be removed from the order (-1 for return to the main menu)
3
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 3
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)
Inside removeComputerPart() method first deduct the price of part then remove that parts from the list. removeComputerPart() method will be as follows-
public void removeComputerPart(int index){
totalPrice-= parts.get(index).getPrice(); // first line
parts.remove(index); // second line
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
i wrote this code for project Euler problem 10 . but in line 24 it has an error. how to fix it?
public static void main(String[] args) {
int i;
int b = 2000;
List<Integer> notPrime = new ArrayList<Integer>();
notPrime.add(2);
notPrime.add(3);
notPrime.add(5);
notPrime.add(7);
for (i = 2; i < b; i++) {
if (i % 2 != 0 && i % 3 != 0 && i % 5 != 0 && i % 7 != 0) {
notPrime.add(i);
}
}
for(int primesNum:notPrime){
int dd = (int) Math.pow(primesNum, 2);
int indexofdd = Arrays.asList(notPrime).indexOf(dd);
//here is the error
notPrime.remove(indexofdd);
}
int summy = notPrime.stream().mapToInt(Integer::intValue).sum();
System.out.println(summy);
}
The type of Arrays.asList(notPrime) is List<List<Integer>>, meaning that Arrays.asList(notPrime).indexOf(<some int>) is always going to be -1 (not found), because a List<List<Integer>> cannot contain an Integer.
Hence the call to List.remove will fail, since as the Javadoc states:
Throws IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()).
You can simply write:
notPrime.remove(Integer.valueOf(dd));
(No need for separate indexOf call)
You need the Integer.valueOf in order to ensure that List.remove(Object) is invoked, rather than List.remove(int): the latter removes the element at the given index, whereas the former removes the list element with the given value.
However, the logic of this code looks more generally faulty.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a jqgrid, with pagination. User can enter data by themselves to neviaget between pages.
I am getting error if user enter more than available pages on line 199 as shown in the above example. How to solve this issue?
if(noOfRows != null && !recipeList.isEmpty())
if ((noOfRows * pageNo) < recipeList.size()) {
recipeList = recipeList.subList((noOfRows * (pageNo - 1)),
(noOfRows * pageNo));
} else {
recipeList = recipeList.subList((noOfRows * (pageNo - 1)),
recipeList.size()); //line 199: giving error
}
for (Recipe recp : recipeList) {
..............
..............
I tried to change the else part of code where on line 199 :
int totalCustomPagesNums=noOfRows * (pageNo - 1);
int firstIndex=totalCustomPagesNums < recipeIdList.size()?totalCustomPagesNums:1;
recipeList = recipeList.subList(firstIndex,
recipeList.size());
I would simplify it to:
Work out the lower bound, which must be at least 0 and at most recipeList.size()
Work out the exclusive upper bound, which must be at least 0 and at most recipeList.size()
Take the sublist
So:
int start = Math.min(Math.max(noOfRows * (pageNo - 1), 0), recipeList.size());
int end = Math.min(Math.max(noOfRows * pageNo, start), recipeList.size());
recipeList = recipeList.subList(start, end);
Now you know for sure that 0 <= start <= end <= recipeList.size(), so it'll be fine, even if the user specifies bizarre row counts or page numbers.
You are trying to create a sublist that starts at index 100 and ends at index 72. Since the beginning of your list is behind the end, you are getting the IllegalArgumentException.
Add another check, if (noOfRows*pageNo < recipeList.size()).
I would add two extra blocks, to the beginning of your code.
if (pageNo < 1) {
pageNo = 1;
}
if ((pageNo - 1) * noOfRows >= recipeList.size()) {
pageNo = 1 + ( recipeList.size() - 1 ) / noOfRows;
}
The first block fixes things if the page number is too low. The second block fixes things if the page number is too high.
The check at the beginning of the second block is making sure that the index of the first recipe to be displayed (which is (pageNo - 1) * noOfRows) is within the range of recipeList. The assignment inside is setting pageNo to the highest value for which this is true.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I want to insert a csv file content to a list .
col1,col2,col3
b,a,v
a,c,p
d,a,z
q,z,a
r,a,b
So that I can get a specific col value
for example : if 2 nd col then
a
c
a
z
a
I tried to insert it into a list
list: [b,a,v, a,c,p, d,a,z, q,z,a, r,a,b]
But how will I get the 2nd col value.
UPDATE
ArrayList<String> ar = new ArrayList<String>();
ar.add(line);
System.out.println("list: "+ar);
I am able to print first index.
using
System.out.println("get index: "+ar.get(0));
get index: b,a,v
but i need to get 'a' ie need all 2 nd column of the given data
As per your update, You need a from b,a,v . Here you can split the string and get the middle element i.e index 1 element. So following code snippet will get the 2nd value in each element and print it
for(int i=0;i<ar.size();i++) {
String[] tokens=(ar.get(i)).split(",");
System.out.println(tokens[1]);
}
With out using indexes of arraylist, You can use for each loop
for(String value:ar) {
String[] tokens=value.split(",");
System.out.println(tokens[1]);
}
A simple for loop will provide a trick,
Assign i with the initial value of columnnumber - 1, where columnnumber is the column value you are looking for,
for (int i = columnnumber - 1; i < args.length; i+=3)
{
System.out.print(args[i] + ", ");
}
Since each row contains 3 values and you have created a single list.
List is 0 index based. Index 1 will return the 2nd element from the list.
Use like this.
list.get(1)
You can do something like this.
ArrayList list = ... ;
...
int colId = 1;
int numCols = 3;
for(int i = colId; i < list.size(); i += numCols){
System.out.println(list.get(i));
}
Solution 1
Instead of list use List.so your list become
List<List>: [ [b,a,v], [a,c,p], [d,a,z], [q,z,a], [r,a,b]]
And to get any row you just have to call
List<List<String> list;
List<String> row = list.get(index); \\will return you corresponding row
String content = row.get(1); \\will return col2 value
Solution 2
if you go by single list then to get any element you must have to perform some calculation like
list<String> ar = new ArrayList<String>();
ar.get(row * 3 + column) // will return you column of row you want.
Note: row and column count starts from 0 not 1
You can use % operator if you are having only ArrayList.
Logic goes like this :
int colNo = 3 ; // column no which you want to get from list
for ( int i = 0; i < list.size(); i++ ) {
if ( i % colNo == 0) {
System.out.println(list.get(i));
}
}
However in your case it is recommended to use list of list.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
So for example if the original list x was 3 5 6 8 9 2, the new linked list h would be 3 6 9
So I thought my method was working and being awesome but when the original list had more than 3 elements, my list odd doesn't seem to be linked to the next node when list odd has more than 3 elements.
I believe the problem is in my for loop when the condition of my odd List isn't empty.
So if u guys can let me know what I need to do i would appreciate it a lot!
Since I'm new here it wont let me just add a print screen of my method so here is the best next thing :
public static Node oddPosition( iNode x){
int count = 1;
iNode oddList = null;
for(Node temp = h; temp != null; temp = temp.next){
if(count % 2 != 0 ){//<-----declares whether the position is odd or not
//if oddList is empty
if(oddList == null){
oddList = new Node(temp.item);
oddList.next = null;
}
//if oddList is not empty
oddList.next = new Node(temp.item);//<----here is where I believe the problem is for some reason my linked list isnt linking together
oddList.next.next = null;
}
count++;
}
System.out.print("Odd list : ");
print(oddList);
return oddList;
}
Output :
Original list : 3 5 6 8 9 2
What is should display : 3 6 9
What I am getting : 3 9
You keep adding new elements to oddList.next. ou never change value of oddList so this way your result has only the first element and the last element. Somewhere you have to assign oddList = oddList.next to be able to add new values to the end of your list. And you probably also want to keep the first node in a separate value, for example startOfList.
So the result could be something like this:
public static Node oddPosition( iNode x){
int count = 1;
iNode oddList = null;
iNode startOfList = null;
for(Node temp = h; temp != null; temp = temp.next){
if(count % 2 != 0 ){//<-----declares whether the position is odd or not
//if oddList is empty
if(oddList == null){
oddList = new Node(temp.item);
startOfList = oddList;
oddList.next = null;
}
//if oddList is not empty
oddList.next = new Node(temp.item);
oddList.next.next = null;
oddList = oddList.next;
}
count++;
}
System.out.print("Odd list : ");
print(startOfList);
return startOfList;
}