public int[] allIndicesOf(E itemSought) {
ArrayList<Integer> toUse = new ArrayList<>();
for (E anArray : container) {
if (anArray.equals(itemSought)) {
toUse.add(container.indexOf(itemSought));
[Code] ....
I have an array list of strings. I want to be able to return an array of integers telling me which indexes in the string array list contain the itemSought object.
I am trying to return an array and I keep getting a null error. The below class sets the material numbers into an array and should return that array if called :
public class Jobs { private int[] materialsNumber; //change to parts and create another class that gets the materials for the parts public int[] job1() { materialsNumber[0] = 11960120;
[Code] ....
I later try to call the method. The program executes but stops after I println "test in loop"
public class PurchaseOrdersToParts { private Jobs job = new Jobs(); int[] getPartsForPurchaseOrder(BigDecimal purchaseOrder) { System.out.println("inside getparts"); BigDecimal testNum = new BigDecimal(123.0);
[Code] ....
This is the method that is calling the method in the GenerateOrdersToParts class
private PurchaseOrdersToParts purchaseOrdersToParts = new PurchaseOrdersToParts(); @Inject PoRepository poRepository; public GenerateShopJobTickets() {
I'm making a program with several functions for an array of integers, one being to find the last occurrence of a given integer and returning it. The problem is that the method is returning -1 for every input, even though it's only supposed to do so for numbers not in the array. Here's the part of my code with the sections I'm having an issue with:
Driver
import java.util.Arrays; public class ArrayMethodsDriver { public static void main(String[] args) { int[] a = {7,8,8,3,4,9,8,7}; System.out.println("Last index of 8: " + ArrayMethods.findLast(a, 8)); System.out.println("Last index of 2: " + ArrayMethods.findLast(a, 2));
[Code] ....
As you can see, I'm testing 8(which should give a 6) and 2(which should give -1 because it's not in the array), and 4(which should give 4). The print statements all say -1.
I'm working on an assignment that asks for the user to input 2 lists of numbers and my program will merge and sort the lists using arrays and 2 methods. I think I have most of it down, but I'm not sure how to go about getting the user inputs. In my current code, it's giving me a bunch of 0s instead of a sorted list.
import java.util.Scanner; public class merge2sortedlists { public static void main(String[] args) { Scanner input = new Scanner(System.in);
I have studied that Generics are used to shift the Class Cast Exception into Compile time errors , So that we get errors at compile time error and we do correct them before executing ,but Here is a program in which i am getting Class Cast Exception
class Animal { } class Dog extends Animal { } class Cat extends Animal
[code]..
Getting Exception at line no 29 which i know why it occurs but just wanna ask that isn't it should be caught at compile time According to Generics ?
Set<? super TreeMap> s = new HashSet<SortedMap>();
SortedMap<String,String> sm = new TreeMap<String,String>(); TreeMap<String,String> tm = new TreeMap<String,String>(); s.add(sm); //This fails s.add(tm);
Why does adding sorted map to a Set that allows ? super TreeMap and instantiated as such fail?
Why java uses the keyword extends when setting the bound of a type parameter(Generic) to an interface. I think using the keyword implements is more intuitive.
public static <T extends Comparable<T>>
why use extends? and not implements.
int countGreaterThan(T[] anArray, T elem) { int count = 0; for (T e : anArray) if (e.compareTo(elem) > 0) ++count; return count; }
I know if I want to set multiple bounds I will use extends keyword, and I will concatenate the bounds using & operator.
Is this a design decision to always use extends keyword to set bounds?
public static <E extends Comparable<E>> void sort(E[] list... mh_sh_highlight_all('java');
Comparable is an interface and from how i look at this piece of code is that I can only use a class that implements the Comparable interface; however, this is the context my book uses when explaining the following code
First, it specifies that E is a subtype of Comparable.
Second, it specifies that the elements to be compared are of the E type as well.
I've an interface with generic methods in it. I would like to have specialized methods in the sub types. While doing that I'm seeing the following warnings in eclipse.
class Sorter { <E> void sort(E[] elements); };
class StringSorter {
// This gives me a warning 'hiding' to 'sort' <String> void sort(String[] elements) { }
// Gives me an error "The method someCrap(String[]) in the type StringSorter is not applicable for the arguments (String[])" void someCrap(String[] elements) { } };
I would like to understand why eclipse gives the above warnings and errors.
I'm working with Doubly Linked Lists and using Java Generics..
My nodes looks like this: class DNode<E> { DNode<E> previous; DNode<E> next; E element;
//and all methods inside }
My list of Nodes looks like this: class DLL<E>{ private DNode<E> head; private DNode<E> tail; private int size;
[code]....
As you can see, as arguments they get "E o"...I need to write a program, which from the main function asks the users how long is the list, and after they type it's length, I ask them to start typing the elements (integers)...and this is how my main method is written, but I can't seem to make it work, specialy when I call the "insLast" method,I guess it's because the arguments i'm giving to the function...how to read the elements and write them into the list?
public static void main(String[] args) throws IOException { DLL<Integer> lista=new DLL<Integer>(); BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); String s = stdin.readLine(); int N = Integer.parseInt(s); s = stdin.readLine(); String[] pomniza = s.split(" "); for (int i = 0; i < N; i++) { lista.instLast(Integer.parseInt(pomniza[i])); }
i am interested to add integer objects and String objects into any collection object ..... while iterating the collection object i am not interested to do any type cast in java
I am trying to make a generic method that will replace the data type T with those number types usable with a Scanner object. However, whenever I try to compile, I get errors saying that a Byte/Integer/Double etc are found when only a type T is allowed. This is the beginning of my method. I can;t understand what is wrong with it.
Java Code:
public <T extends Number> T nextRanged(T lowerBound, T upperBound, boolean inclusive, String errorMessage){ // Holds program execution until user inputs a numeric value between the bounds. Prevents all other input without exception. // Output data type determined by the type of the bounds. T input = null; try{ if(input instanceof Byte){ input = new Byte(internalScanner.nextByte());
[Code] ....
The purpose of the method, in the end, will be to provide the nextXXX() functionality of a Scanner object but with built in validation procedures. I could easily do this by making a nextIntRanged(), nextDoubleRanged() etc methods, but this seems wasteful to me.
I meant "incompatible type errors"!
Error example:
ValidatedScanner.java:57: error: incompatible types input = new Byte(internalScanner.nextByte()); ^ required: T found: Byte
where T is a type-variable:
T extends Number declared in method <T>nextRanged(T,T,boolean,String)
The erasures of all constituent types of a bound must be pairwise different, or a compile-time error occurs.
Well I know what type erasure is, and I think I kind got what this statement means. My understanding from it is that if your type parameter has more than one bound and those bounds occurs to be the same type after erasure that is a compile-time error. Is that it?
The only thing I could found related is something like this:
class A<T extends List<Integer> & List<Integer>>{ }
Which as you might know gives the Duplicated bound error.
I'm new to Java and I have a problem with a method, I can't see the code of the method, I just have a jar, but it should return a boolean, something like this:
boolean band = false; band = TestClass.testMethod("blabla"); // band = false
The problem is that the method seems that is returning nothing (band remain false), and if I initialize band to true:
boolean band = false; band = TestClass.testMethod("blabla"); // band = true
band remain true, in other words, the value of band is never modified, the question is, how is this possible? because it should return the same value on both calls, true or false, no matter the initial value of the variable that is receiving the returning value of the method.
This simple program should build a new table with random values and on the getValue method (perhaps called from othe TestClass) return back content of particular cell. why my getValue method doesn't work. Nay! It returns error while compilation.
import java.util.Random; class TablicaIntowa{ public int getValue(int a){ return tabl[a]; } //getValue method end
I have created a class and a matrix of doubles (or at least, I think I have, that's partly what I want to verify).I need to return the values of the array,Here is my class:
public class example{ double[][] Position=new double[2][11]; double calculate(){ for (int time=0;time<=10;time=time+1){ Position[1][time]=time; Position[2][time]=time+1; double A=Position[2][time]; return A; } } }
I am getting the error: "This method must return a result of type double", though to me it looks like I am returning double (A).
I've made a class called Car with a method which will tell me a category for the engine size regarding the actual size (which I've included in the main just so I could see if it works) but everytime I test it I get an error.
public class Car { public String b; public String c; public double es; public double cs; public String getCategory() { if (es < 1.3)
[Code] ....
Figured it out. Was missing parenthesis on audiCar.getCategory();
i am trying to run a command in terminal the code is below if i run the command in terminal it works fine however when i run it from netbeans with code below nothing gets printed. however if i run a different command such as (ip addr) it works fine?
public static void a() throws IOException{ ArrayList lister=new ArrayList(); Runtime rt = Runtime.getRuntime(); Process proc = rt.exec("ps -ef | grep firefox");// the command i am trying to run to get pid of application InputStream stderr = proc.getInputStream();
I am writing a code where in the first method the question will ask whats your favorite website. for example www.javaprogrammingforums.com...when it outputs it will read just "javaprogrammingforums" without the www. and the .com.
Because the program will ask a series of questions in the main, I would like website question to be returned to the main. Here is my code, and what can I do?
import java.util.Scanner; public class chapter3 { public static String website(Scanner kb) { String website; System.out.println("What is your favorite website?"); website = kb.next();