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(" ");
}
}
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 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?
I'm using SAX and XML reader to read XML weather info from the web and it works fine if the page exists. But if for instance the user inputs an invalid city, zip etc the XML page that gets read from is empty and the app force closes with nullpointerexception. The area that generates the error is here right at open inputstream. Any suggestions?:
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = null;
try {
sp = spf.newSAXParser();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/* Get the XMLReader of the SAXParser we created. */
XMLReader xr = null;
try {
xr = sp.getXMLReader();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/* Create a new ContentHandler and apply it to the XML-Reader*/
WeatherHandler myExampleHandler = new WeatherHandler();
xr.setContentHandler(myExampleHandler);
/* Parse the xml-data from our URL. */
try {
xr.parse(new InputSource(url.openStream()));
parsedWeatherDataSet =
myExampleHandler.getParsedData();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return parsedWeatherDataSet.toString();
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'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)
}
}
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
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?
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.
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.
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 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 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());
}
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 use setLayout (null) and I'm trying to place the buttons and textfield places I know by x, y
The problem when I run the program no matter what software (Eclipse, bluej)
I need to run on the panel with the mouse until I stand on the position of the button and I can see it.
When I find the textfield, it is small and only when I start writing it became the size I set it
Does anyone know how to solve it?
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)
This is a totally newbie question. I'm running Eclipse on Ubuntu. I created a test project that I want to compile to an executable (whataver the linux equivalent is of a Windows .exe file). Here's the contents of my program:
public class MyTest {
public static void main(String[] args) {
System.out.println("You passed in: " + args[0]);
}
}
I want to know how to compile it and then how to execute it from the command line.
Thanks!
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.
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
Ok, so here is my problem:
I have a list containing interfaces - List<Interface> a - and a list of interfaces that extend that interface: List<SubInterface> b. I want to set a = b. I do not wish to use addAll() or anything that will cost more memory as what I am doing is already very cost-intensive. I literally need to be able to say a = b. I have tried List<? extends Interface> a, but then I cannot add Interfaces to the list a, only the SubInterfaces. Any suggestions?
EDIT
I want to be able to do something like this:
List<SubRecord> records = new ArrayList<SubRecord>();
//add things to records
recordKeeper.myList = records;
The class RecordKeeper is the one that contains the list of Interfaces (NOT subInterfaces)
public class RecordKeeper{
public List<Record> myList;
}