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} };
Hi I am creating a method that will take a number and print it along with its binary representation. The problems is that my method prints all 0's for any positive number, and all 1's for any negative number
private static void display( int number ){
System.out.print(number + "\t");
int mask = 1 << 31;
for(int i=1; i<=32; i++) {
if( (mask & number) != 0 )
System.out.print(1);
else
System.out.print(0);
if( (i % 4) == 0 )
System.out.print(" ");
}
}
Hi
I have a method that has a reference to a linked list and a int value. So, this method would count and return how often the value happens in the linked list. So, I decided to make a class,
public class ListNode{
public ListNode (int v, ListNode n) {value = v; next = n;)
public int value;
public ListNode next;
}
Then, the method would start with a
public static int findValue(ListNode x, int valueToCount){
// so would I do it like this?? I don't know how to find the value,
// like do I check it?
for (int i =0; i< x.length ;i++){
valueToCount += valueToCount;
}
So, I CHANGED this part, If I did this recursively, then I would have
public static int findValue(ListNode x, int valueToCount) {
if (x.next != null && x.value == valueToCount {
return 1 + findValue(x, valueToCount);}
else
return new findvalue(x, valueToCount);
SO, is the recursive part correct now?
Using
double variable = inputFile.nextDouble();
Gives the mismatch error and I can't figure out why... Anyone know what's up?
The input file is just a bunch of doubles like 5.0...
Okay here is the code snippet
String fileName;
Scanner scanner = new Scanner(System.in);
System.out.println("\nEnter file name that contains the matrix and vector: ");
fileName = scanner.nextLine();
Scanner inputFile = new Scanner(fileName);
double a1 = inputFile.nextDouble();
the input file is a plain text document .txt in this format
5.0 4.0 -3.0
4.0 2.0 5.0
6.0 5.0 -2.0
-13.0 4.0 12.0
I don't understand why it wouldn't take those as doubles...
As far as what its expecting the format of the file to be... I suppose binary? isn't that the default? I didn't specify in the code...
Hi, I have a string like this String str = "la$le\$li$lo".
I want to split it to get the following output "la","le\$li","lo". The \$ is a $ escaped so it should be left in the output.
But when I do str.split("[^\\\\]\\$") y get "l","le\$l","lo".
From what I get my regex is matching a$ and i$ and removing then. Any idea of how to get my characters back?
Thanks
Hi,
I'm trying to upload a file via URLConnection, but i need to read/write it as a binary file without any encoding changes. So i've tried to read byte[] array from FileInputStream, but now i have an issue. The PrintWriter object i use for outputing to the server does not allow me to do writer.write(content) (where content is of type byte[]). How can i fix this? Or is there another way to quickly copy binary data from a FileInputStream to a PrintWriter?
Thank you
I'm making a BMI calculator that doesn't seem to be working.
The math operations work if i just do something like w/h, but once i had the brackets, it returns an error.
If i change the variables w and h and use a constant number, the operation works.
Another problem is that although i'm making result a double, it seems to be rounding to the nearest int.
Could someone tell me what I'm doing wrong here?
public class ass10 {
public static void main(String[] args) {
bmi(223,100);
}
public static bmi(int w, int h){
double result;
result = (w/(h*h))*703
System.out.println(result)
}
}
I Have dynamically render row. WE have fields like FROM TO.
For eg: From TO
2 10,
2 3,
8 12
It cannot accept this combination row.. That means no number should be overlapping.
For eg: From TO
2 10,
0 1,
11 12
This combination is allowed.the row may also increased.
I need need to write a validation for this overlapping.
Can any 1 help to solve this problem.
Hello everyone,
In my code, all of the scripts are contained in .js files. Whenever one of the scripts contains an error, I get this:
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "nonexistant" is not defined. (<Unknown source>#5) in <Unknown source> at line number 5
What bugs me is the <Unknown Source>. Multiple files are in one ScriptContext, and it can be hard to track down an error. It also looks horrible.
Is there a way to replace <Unknown Source> with the actual file name? None of the methods I see support passing a File object, so I'm really confused here.
I'm trying to make like a skype-instant messager, my idea for it is to have
one server which handles multiple connections for the clients. What I now have is a friend list etc, but now I want to create Threads both for server and client to handle a conversation. The problem is that I need multiple connections between a server and one client for every conversation(I think). but i dont think it's possible. Does someone have another way for doing this or maybe a way to make multiple connections between the server and a client?
Thanks for helping me out
PS: English is not my main language so please excuse me for my grammar.
Hi Folks,
I want to pass the String value between the classes both are same package. so i created the the classs like the code:
public class Map_Delegate {
String myLatitude;
String myLongitude;
String myName;
private String TAG = "Map_Delegate";
public String getMyName() {
return this.myName;
}
public void setMyName(String value) {
Log.v(TAG, value);
this.myName = value;
}
public String getMyLatitude() {
return this.myLatitude;
}
public void setMyLatitude(String value) {
Log.v(TAG, value);
this.myLatitude = value;
}
public String getMyLongitude() {
return this.myLongitude;
}
public void setMyLongitude(String value) {
Log.v(TAG, value);
this.myLongitude = value;
}
}
But it can't pass the value. I done like this code to set the value:
Map_Delegate map = new Map_Delegate();
map.setMyName(first_name_temp + " " + last_name_temp);
map.setMyLatitude(homeLatitude_temp);
map.setMyLongitude(homeLongitude_temp);
code to get the value:
Map_Delegate map = new Map_Delegate();
name_val = getMyName();
lat_val = getMyLatitude();
long_val = getMyLongitude();
Why get the Null value can you Guess? All classes in the same package and public .AnyIdea?
I have a servlet that receives some POST data. Because this data is x-www-form-urlencoded, a string such as ???? would be encoded to サボテン.
How would I unencode this string back to the correct characters? I have tried using URLDecoder.decode("encoded string", "UTF-8"); but it doesn't make a difference.
The reason I would like to unencode them, is because, before I display this data on a webpage, I escape & to & and at the moment, it is escaping the &s in the encoded string so the characters are not showing up properly.
I have a problem here that still cannot solve, the thing is I have this abstract class:
public abstract class AbstractBean<T> {
private Class<T> entityClass;
public AbstractBean(Class<T> entityClass) {
this.entityClass = entityClass;
}...
Now I have another class that inherits this abstract:
@Stateless
@LocalBean
public class BasicUserBean<T extends BasicUser> extends AbstractBean<T> {
private Class<T> user;
public BasicUserBean() {
super(user); // Error: cannot reference user before supertype contructor has been called.
}
My question is how can I make this to work?, I am trying to make the class BasicUserBean inheritable, so if I have class PersonBean which inherits BasicUserBean then I could set in the Generic the entity Person which also inherits the entity BasicUser. And it will end up being:
@Stateless
@LocalBean
public class PersonBean extends BasicUserBean<Person> {
public PersonBean() {
super(Person.class);
}
...
I just want to inherit the basic functionality from BasicUserBean to all descendants, so I do not have to repeat the same code among all descendants. Thanks!.
I'm hoping for some advice or suggestions on how best to handle multi threaded access to a value store.
My local value storage is designed to hold onto objects which are currently in use. If the object is not in use then it is removed from the store.
A value is pumped into my store via thread1, its entry into the store is announced to listeners, and the value is stored. Values coming in on thread1 will either be totally new values or updates for existing values.
A timer is used to periodically remove any value from the store which is not currently in use and so all that remains of this value is its ID held locally by an intermediary.
Now, an active element on thread2 may wake up and try to access a set of values by passing a set of value IDs which it knows about. Some values will be stored already (great) and some may not (sadface). Those values which are not already stored will be retrieved from an external source.
My main issue is that items which have not already been stored and are currently being queried for may arrive in on thread1 before the query is complete.
I'd like to try and avoid locking access to the store whilst a query is being made as it may take some time.
How can I convert a given object (in a generic way with reflection) to pretty printable HTML?
What ready made library do you recommend that does this? I need support for simple nested objects (as long as they don't create loops in the object graph).
I tried to convert it to JSON, but DefaultPrettyPrinter is not HTML friendly.
Write a recursive method that converts a decimal number into a binary number as a string. The method header is as follows:
public static String convertDecimalToBinary(int value)
I use the following trick to get the array type of a specific class:
@SuppressWarnings("unchecked")
public static <T> Class<T[]> getArrayType(Class<T> componentType) {
String arrayClassName = "[L" + componentType.getName() + ";";
try {
return (Class<T[]>) Class.forName(arrayClassName);
} catch (ClassNotFoundException e) {
throw new UnexpectedException("Can't get the array type for " + componentType, e);
}
}
But, is there any more elegant way to get this?
I have two button one textset on JFrame ,i need user used first button to browse
in your computer and other button to attche documents or image
and displayed in textset
hi,
I'm reusing the same ArrayList in a for loop, and I use
for loop
results = new ArrayList<Integer>();
experts = new ArrayList<Integer>();
output = new ArrayList<String>();
....
to create new ones.
I guess this is wrong, because I'm allocating new memory. Is this correct ?
If yes, how can I empty them ?
Added: another example
I'm creating new variables each time I call this method. Is this good practice ? I mean to create new precision, relevantFound.. etc ? Or should I declare them in my class, outside the method to not allocate more and more memory ?
public static void computeMAP(ArrayList results, ArrayList experts) {
//compute MAP
double precision = 0;
int relevantFound = 0;
double sumprecision = 0;
thanks
I have 1 thread who sole job is to grab DatagramPackets off of a socket and stick them in a buffer. Another thread works out of that buffer, processing the DatagramPackets. I'd like to have a pool of threads working out of that buffer.
I had thought to use a fixed thread pool to do this. To do so, do I need to create the pool, then submit enough runnables for execution to fill it up? I had hoped for a way to say "this is the thread/runnable that I want you to execute, this is how many I want running, GO!". Is there such a method of doing this? Is something other than a fixed thread pool better suited?
I have an ArrayList containing Objects that have a date value.
Now I want to manage to create a new ArrayList for each year that contains all the Objects from the main ArrayList that have the same year in their date Value.
So all Objects from 2010 go in one List, all from 1999 in another.
i want to list files starting with a name like "Report" from a folder.
i found this in google to list all files but i don't how to list file starting with a name.
Thank you
File directory = new File("C:\\Users\\kiki\\Downloads");
File[] files = directory.listFiles();
for (int index = 0; index < files.length; index++)
{
//Print out the name of files in the directory
System.out.println(files[index].toString());
}