Am about to do a homework, and i need to store quite a lot of information (Dictionary) in a data structure of my choice. I heard people in my classroom saying hash-tables are the way to go. How come?
Hi guys,
it may be a nooby question, but I've never needed it before:
I have several strings and I want to compare them to given ones...
At first glance it would lead to a switch/case construction in what every available entry is checked.
Is there a more elegant way to swap those strings as key/value datas?
greets,
poeschlorn
this and super are keywords isn't it, then how can we use them for passing arguments to constructors the same way as with a method??
In short how is it that these can show such distinct behaviours??
My problem is i have a class and in it there is a list of elements of another class.
public class Branch
{
private ArrayList<Player> players = new ArrayList<Player>();
String brName;
public Branch() {}
public void setBr(String brName){this.brName = brName;}
public String getBr(){return brName;}
public ArrayList<Player> getPlayers() { return players; }
public void setPlayers(ArrayList<Player> players) { this.players =new ArrayList<Player>(players); }
}
public class Player
{
private String name;
private String pos;
private Integer salary;
private Integer number;
public Player(String name, String pos, Integer salary, Integer number)
{
this.name = name;
this.pos = pos;
this.salary = salary;
this.number = number;
}
public Player(){}
public String getName() { return name; }
public String getPos() { return pos; }
public Integer getSalary() { return salary; }
public Integer getNumber() { return number; }
public void setName(String name) { this.name = name; }
public void setPos(String pos) { this.pos = pos; }
public void setSalary(Integer salary) { this.salary = salary; }
public void setNumber(Integer number) { this.number = number; }
}
My problem is to print the players of a Branch with their name,pos,salary,number.
For this i tried this simply :
String p1,p2;
int a1,a2;
p1 = input.readLine();
p2 = input.readLine();
a1 = Integer.parseInt(input.readLine());
a2 = Integer.parseInt(input.readLine());
players[0].setName(p1);
players[0].setPos(p2);
players[0].setSalary(a1);
players[0].setNumber(a2);
ptmp.add(players[0]);
myBranch[0].setPlayers(ptmp);
System.out.println(myBranch[0].brName + " " + myBranch[0].getPlayers());
I wrote this just to try how to display. I created an array of Players, and Branches so they already defined. The problem is getPlayers() doesn't give me any result. What is the way to do this?
This is a jFrame to connect to the database and this is in the button connect. My issue is in the passwordField NetBeans make me do a char[], but my .getConnection not let me insert the char[] ERROR: "no suitable method found for getConnection(String,String,char[])". So I will change to String right? So when I change and run the jFrame said access denied. when I start doing the System.out.println(l) " Give me the right answer" Like this: "Alex". But when I do the System.out.println(password) "Give me the Array spaces and not the value"
Like this: jdbc:mysql://localhost/home inventory root [C@5be5ab68 <--- Array space . What I doing wrong?
try {
Class.forName("org.gjt.mm.mysql.Driver"); //Load the driver
String host = "jdbc:mysql://"+tServerHost.getText()+"/"+tSchema.getText();
String uName = tUsername.getText();
char[] l = pPassword.getPassword();
System.out.println(l);
String password= l.toString();
System.out.println(host+uName+l);
con = DriverManager.getConnection(host, uName, password);
System.out.println(host+uName+password);
} catch (SQLException | ClassNotFoundException ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
}
}
I want to have a generic object that implements an interface.
I mean if i have a class A
Class A <E>{
E x;
}
I want to make sure that x will implement a particular interface(myInterface). In other words, that the type E implements an interface.
I am wanting to create a gihub repository that offers benchmarking code that
works for concurrent features available only in JDK 1.7 (Fork/Join) as well as for older ones found in JDK 1.6.
Offering both options is important for what I need.
Does anyone have a recommendation how should I structure the repository.
I was planning on having a repo called and under it:
jdk17
build
src
mycode ...
jdk16
build
src
mycode
Please suggest any alternatives, possibly use of Maven or other more practical approaches, if any.
I am making a program that lets a user input a chemical for example C9H11N02. When they enter that I want to split it up into pieces so I can have it like C9, H11, N, 02. When I have it like this I want to make changes to it so I can make it C10H12N203 and then put it back together. This is what I have done so far. using the regular expression I have used I can extract the integer value, but how would I go about get C10, H11 etc..?
System.out.println("Enter Data");
Scanner k = new Scanner( System.in );
String input = k.nextLine();
String reg = "\\s\\s\\s";
String [] data;
data = input.split( reg );
int m = Integer.parseInt( data[0] );
int n = Integer.parseInt( data[1] );
I think I may be a victim of type erasure but thought I'd check with others here first.
I have the requirement to do something like this:
public interface FooFactory {
public <T extends Bar> Foo<T> createFoo( Class<T> clazz );
}
It is perfectly valid to write this code. However, I'm trying to implement this functionality using a Spring BeanFactory and I can't do it.
What I'd like to do is this...
public class FooFactoryImpl implements BeanFactoryAware {
private BeanFactory beanFactory;
public <T extends Bar> Foo<T> createFoo( Class<T> clazz ) {
return beanFactory.getBean( ????????? );
}
public void setBeanFactory( BeanFactory beanFactory ) {
this.beanFactory = beanFactory;
}
}
As you can see, I've put in ???????? where I'd like to retrieve a bean of type Foo<T>, where T extends Bar. However, it is not possible to derive a Class object of type Foo<T> and so I assume what I'm trying to do is impossible?
Anyone else see a way round this or an alternative way of implementing what I'm trying to do?
Thanks,
Andrew
I am executing a stored procedure which has 2 cusors within the stored procedure.
The 1st cursor which is not return to the jdbc as a resultset is closed within the stored procedure.
The 2nd cursor which returns the resultset to the jdbc is not closed within the stored procedure.
Upon executing I encounter cursor is closed exception which is puzzling.
Since the ResultSet will close all underlying cursor upon invoking the close() method
I seem to have two options on how to implement arrays, and I want to know which I should go with:
Use the ARRAY data type and (from what I understand) effectively serialize data objects into the database (which in my case are just wrapped primitive types; don't know of another way to make this work).
Use a separate table and map with foreign keys for each array item.
If you have experience with this (especially with H2), which would you recommend?
I have data from a database loaded into a JTable through a custom table model. I want to have a column (should be the first column) which simply shows the display row number (i.e. it is not tied to any data (or sorting) but is simply the row number on the screen starting at 1). These "column headers" should be grayed out like the row headers.
Any idea how to do this?
Thanks
I'm maintaining this Swing app that has a "print" option. Users need to be kept from interacting in any way with the underlying file system, but the print dialog offers "print to file" as one printer, and that of course allows selecting a directory and file from the file system.
Is there a painless way to override/modify the print dialog to hide the "to file" printer from this dialog? I understand the API will let me do this piecemeal but I'd rather not have to re-create most of the dialog GUI and functionality to do this.
I have a string;
String allIn = "(50 > 100) AND (85< 100)";
Now I need to evaluate if the conditions inside are TRUE or FALSE, how can I do it?
In real the string will be a value from a field in my DB, where I will substitute different values and they will form a string as shown above.
I'm trying to match the username with a regex. Please don't suggest a split.
USERNAME=geo
Here's my code:
String input = "USERNAME=geo";
Pattern pat = Pattern.compile("USERNAME=(\\w+)");
Matcher mat = pat.matcher(input);
if(mat.find()) {
System.out.println(mat.group());
}
why doesn't it find geo in the group? I noticed that if I use the .group(1), it finds the username. However the group method contains USERNAME=geo. Why?
Runtime.getRuntime().exec("C:\mysql\bin\mysqldump -u root -pmypassword Databasename -r C:/backup.sql");
I am using this code to create back up from my sql. but It creates the empty file in the path.Because it is waiting in the command prompt to get the password. How can i give password to it
Using command prompt directly when i press enter after typing, it asks password.After giving password,It creates the backup.Give me any solution for this
Thanks in advance
I came across this weird (in my opinion) behavior today. Take this simple Test class:
public class Test {
public static void main(String[] args) {
Test t = new Test();
t.run();
}
private void run() {
List<Object> list = new ArrayList<Object>();
list.add(new Object());
list.add(new Object());
method(list);
}
public void method(Object o) {
System.out.println("Object");
}
public void method(List<Object> o) {
System.out.println("List of Objects");
}
}
It behaves the way you expect, printing "List of Objects". But if you change the following three lines:
List<String> list = new ArrayList<String>();
list.add("");
list.add("");
you will get "Object" instead.
I tried this a few other ways and got the same result. Is this a bug or is it a normal behavior? And if it is normal, can someone explain why?
Thanks.
Hi i am trying to match a string against a pattern
this is the possible string
signal CS, NS, dl: stateType := writeOrRead0;
signal CS, pS : stateType := writeOrRead0;
signal dS : stateType := writeOrRead0;
i am only concerned with the pattern as far as the first colon.
but the number of signals define can be more than one it could be three or four even
this is the regular expression i have
^signal\\s*(\\w+),*\\s*(\\w+)\\s*:
it will pick up the second two signal but and for the second one it picks up CS and pS and but the d and S in the next signal when i use
matcher.group()
come up seperately
Can anyone give me an expression that will pick up all signal names whether there is one two three or more?
Say I have the following:
Class myclass
{
public string stra ="", strb = ""
myclass(String a, String b){stra=a;strb=b}
}
//then in the app I want to do:
myclass myclassinst1 = new myclass("blah","xxxx");
myclass myclassinst2 = new myclass("blah2","yyyy");
myclass myclassinst3 = new myclass("blah3","zzzz");
list <myclass> mylist = new ArrayList<myclass>();
mylist.add(myclassinst1 );
mylist.add(myclassinst2 );
mylist.add(myclassinst3 );
//How would I then convert that to a String[] Array of all the stra elements without using a loop.
//eg:
String[] strarr_a = mylist.toarray(myclass.stra);
String[] strarr_b = mylist.toarray(myclass.strb);
//instead of having to do
String[] strarr_a = new String[mylist.size()];
String[] strarr_b = new String[mylist.size()];
for (int i=0;i<mylist.size();i++)
{
strarr_a[i] = mylist.get(i).stra;
strarr_b[i] = mylist.get(i).strb;
}
Hi,
I need to find out how many even values are contained in a binary tree.
this is my code.
private int countEven(BSTNode root){
if ((root == null)|| (root.value%2==1))
return 0;
return 1+ countEven(root.left) + countEven(root.right);
}
this i just coded as i do not have a way to test this out. I'm not able to test it out at the moment but need an answer so badly.
any help is deeply appreciated.
I have the following scenario :
public class A {
private int x = 5;
public void print()
{
System.out.println(x);
}
}
public class B extends A {
private int x = 10;
/*public void print()
{
System.out.println(x);
}*/
public static void main(String[] args) {
B b = new B();
b.print();
}
}
On executing the code, the output is : 5.
How to access the child class(B's) variable(x) via the parent class method?
Could this be done without overriding the print() method (i.e. uncommenting it in B)?
[This is important because on overriding we will have to rewrite the whole code for the print() method again]
Can anyone help me find where the execption is? I can't seem to find the problem..
public void fieldChanged(Field f, int context){
//if the submit button is clicked
try{
stopTime = System.currentTimeMillis();
timeTaken = stopTime - startTime;
timeInSecs = ((timeTaken/1000));
speed = 45/timeInSecs;
Dialog.alert("Speed of Delivery: " + speed + "mph");
}
catch(ArithmeticException e){
Dialog.alert("error " + speed);
e.printStackTrace();
}
}
startTime variable is a global variable..
Hello,
I'm not able to understand the following multi-dimensional code. Could someone please clarify me?
int[][] myJaggedArr = new int [][]
{
new int[] {1,3,5,7,9},
new int[] {0,2,4,6},
new int[] {11,22}
};
May I know how it is different from the following code?
int[][] myArr = new int [][] {
{1,3,5,7,9},
{0,2,4,6},
{11,22} };
I am using LinkedList and retrieving an Iterator object by using list.iterator(). After that, I am checking it.hasNext(), real issue is while checking it.hasNext(), sometimes it returns false. I need help why this is happening, though I have elements in the list.
Some code:
public synchronized void check(Object obj) throws Exception {
Iterator itr = list.iterator();
while(itr.hasNext()) { //This Line I get false.. though i have list size is 1
Item p = (Item)itr.next();
if(p.getId() == null) {continue;}
if(p.getId().getElemntId() == obj.getId() || obj.getId() == 0 ) {
p.setResponse(obj);
notifyAll();
return;
}
}
Log.Error("validate failed obj.getId="+obj.getId()+" **list.size="+list.size()*This shows 1*);
throw new Exception("InvalidData");
}