tux4life 2,072 Postaholic

I'm writing this post for everyone who - like me - is trying to send requests with: Content-Type: application/json.

I figured it out the hard way (by capturing the packet sent from the PHP code sample) that the Content-Type needs to be multipart/form-data.

tux4life 2,072 Postaholic

Because, I will just use Integer.parseInt for the input.
Then, I'll use if-else statement to justify the user's choice.
Plus, I don't know if it would work under if-else statement..

Sounds workable to me. Alternatively you could use a switch statement.
Try it and when you encounter problems post what you tried so that we can take a look and suggest a way to fix it.

tux4life 2,072 Postaholic

But I still don't understand some parts.. like "private".

Take a look here.
Basically the idea is that the employees array reference should not be exposed.
Compare the following two approaches:

1) Marking it public or default:

// Marking it public
public class EmpList
{
    public String[] employees; // accessible from everywhere

    // ...
}

// Marking it default
public class EmpList
{
    // default access is what you get by "default" when you don't
    // specify 'public', 'private' or 'protected'
    String[] employees; // not accessible from everywhere, but still
                        // accessible from everywhere in the package of
                        // your EmpList class.

    // ...
}

Somewhere else in your code (outside the EmpList class) it is now possible to write this:

EmpList list = new EmpList();
employees.add("John");
employees.add("Tom");
employees.add("Abbey");
list.employees = null; // this would be VERY BAD, after executing,
                       // the elements in EmpList are "gone".

2) Marking it private:

public class EmpList
{
    private String[] employees; // only accessible from inside EmpList

    // ...
}

Somewhere else in your code (outside the EmpList class) you might try writing this:

EmpList list = new EmpList();
employees.add("John");
employees.add("Tom");
employees.add("Abbey");
list.employees = null; // will cause a compiler error because
                       // the employees field is marked as 'private',
                       // none of the employees is gone.

Because you've marked the employees field as private this is not possible - which is a good thing.
Imagine the bugs …

tux4life 2,072 Postaholic

Do you need to write your own simple list implementation as an assignment?

If so, then you'll need to work on your EmpList class, because as it stands now it is no better than using an array directly.
You should encapsulate your implementation and mark your employees array reference in EmpList as private.
Furthermore you should provide a public interface (don't confuse this with a Java interface) that contains methods for:

  • querying the size,
  • adding an element,
  • removeing an element,
  • getting an element by index

Think of something you could use like this:

// Create an empty list
EmpList employees = new EmpList();

// Adding elements
employees.add("John");
employees.add("Tom");
employees.add("Abbey");

// Removing elements
employees.remove("Tom");

// Iterating
for (int i = 0; i < employees.size(); i++)
    System.out.println("Employee #" + i + " = " + employees.get(i));

// Expected output:

// Employee #0 = John
// Employee #1 = Abbey

our instructor told us to use j2sdk 1.4.2_14

Why would you stick with something old like that?

I figured out that some of the package files like.. java.util.Scanner were not included in it.

That is because the Scanner class was new in Java 5.

tux4life 2,072 Postaholic

Use the escape sequence: '\\'.

Example:

char c = '\\'; // store a backslash
jalpesh_007 commented: good solution.. +4
tux4life 2,072 Postaholic

Cisco

tux4life 2,072 Postaholic

Don't hardcode the length of your array:

// DON'T do this
for(int k = 0; k < 5; k++)
    System.out.println(employees[k]);

instead use the array's length field:

// DO this instead
for(int k = 0; k < employees.length; k++)
    System.out.println(employees[k]);
tux4life 2,072 Postaholic

[line 18]: // I got a problem with this here. It's supposed to be a command that reads a char input. But I don't know how to do it too.

You can pick-off the first character in the String. readLine() returns a String, and the String class has a method charAt().
So you can do: inp.readLine().charAt(0).

The line System.out.println(employees[]); has invalid syntax: the brackets aren't allowed there unless you specify an index.
From context I'd say that you probably intended: employees[k].

Another option is to make use of the Arrays utility class to get a String representation of your array.
Here's an example of how you can do that: Arrays.toString(employees), as you can see: you don't have to write a loop.

Do you want/have to write your own list class that is backed by an array and provide insert and delete operations for it? If not, then use ArrayList.

tux4life 2,072 Postaholic

SAP

tux4life 2,072 Postaholic

What you refer to as "ternary expression" is actually called the conditional operator in Java.
Terminology aside, you can use the conditional operator as a shorthand for the following:

int a;

if (condition)
    a = expression1;
else
    a = expression2;

Using the conditional operator, the above can be rewritten as follows:

int a = condition ? expression1 : expression2;

For more precise info about what condition, expression1, and expression2 can be, I refer to the Java Language Specification section 15.25 Conditional Operator ? :.

tux4life 2,072 Postaholic

I don't think I can use charAt(i) to compare the three consecutive 1's, but I don't know what I can use to test if there are three consecutive 1's in the string.

You could use charAt() but it is tedious to do so compared to using contains().
Note that the test input.charAt(i) == '111' is not valid since '111' is not a valid character literal.
To test if your bit String contains at least three consecutive ones somewhere in the String, you can use input.contains("111").

tux4life 2,072 Postaholic

OCZ

tux4life 2,072 Postaholic

Epson

tux4life 2,072 Postaholic

Dell

tux4life 2,072 Postaholic

ESET

tux4life 2,072 Postaholic

I mean the seed is where the random generator is supposed to start generating numbers from?

The class uses a 48-bit seed, which is modified using a linear congruential formula.

(Source: Java API - java.util.Random)

This is a linear congruential pseudorandom number generator,
as defined by D. H. Lehmer and described by Donald E. Knuth in The Art of Computer Programming, Volume 3: Seminumerical Algorithms, section 3.2.1.

(Source: Java API - java.util.Random.next(int))

For an introductory read on Linear Congruential Generators, take a look at Julienne Walker's Eternally Confuzzled page about Random Numbers.

which number should I use as seed?

For general use the easiest thing you can do is calling the no argument constructor of Random.
It will seed the random number generator automagically to a value that is very likely not going to be the seed of any other invocation of that constructor.

Here's how it is stated in the Java API:

Creates a new random number generator.
This constructor sets the seed of the random number generator to a value very likely to be distinct from any other invocation of this constructor.

(Source: Java API - java.util.Random: Random())

tux4life 2,072 Postaholic

The reason is that there's still data in the input stream after the exception has occurred.
You can fix it by reading a line when an exception occurs, using keyboard.nextLine().
Also it seems like your noException flag is redundant.

Play a bit with the following piece of code:

while (num != 0)
{
    System.out.print("Enter a number or 0 to exit: ");

    try
    {
        num = keyboard.nextInt();
        // If an exception is thrown on the line above, then all the code that
        // follows in this try block will not be executed.

        System.out.println("Input correct: " + num);

        //System.out.println("Data left in input stream = " + keyboard.nextLine());

        // Process num here
        // ...
    }
    catch (InputMismatchException e)
    {
        // Print data left in input stream
        System.out.println("\nNot a valid integer: '" + keyboard.nextLine() + "'");
    }
}
tux4life 2,072 Postaholic

Your code is missing an ending brace on line 60.

tux4life 2,072 Postaholic

How to reproduce:

  1. Open up two tabs, load the member activity stream in the first, in the second load another random Daniweb page.
  2. Log out from the second tab.
  3. Switch back to the first tab and notice that the stream keeps streaming.

I'm using Google Chrome version 24.0.1312.57 m.
Clearing the browser cache after logging out stops the stream.

The reason why I'm posting this is that if I'd reload the member stream page I'd not get access to it after having logged out.
So it doesn't seem consistent to me that using the way I described above I can still see the stream after having logged out.

Any thoughts?

tux4life 2,072 Postaholic

Google

tux4life 2,072 Postaholic

C# 4.0 in a Nutshell (4th Edition) / C# 5.0 in a Nutshell (5th Edition)

tux4life 2,072 Postaholic

Lexmark

tux4life 2,072 Postaholic

Is this correct...

Apart from not following the standard and being unportable I'd say no.
Did you even try it out? Compile, run and answer your own question.
If it doesn't compile, fix the compiler error, if it runs but doesn't produce the correct output, fix the bug and compile again.

tux4life 2,072 Postaholic

What is the output you are getting now, and what is your desired output?

tux4life 2,072 Postaholic
tux4life 2,072 Postaholic

Try the following: Double.parseDouble("0x0.DAB789Bp0").
The p0 means that the part on its left side is multiplied by ( 2 ^ 0 ) = 1.

Sources:

  • Double.valueOf (Java API - referred to from the API documentation of Double.parseDouble)
  • Hexadecimal Floating-Point Literals (Blog - Joseph D. Darcy)
  • Floating-Point Literals (JLS - 3.10.2):

    A floating-point literal has the following parts: a whole-number part, a decimal or hexadecimal point (represented by an ASCII period character), a fractional part, an exponent, and a type suffix.
    A floating point number may be written either as a decimal value or as a hexadecimal value.
    For decimal literals, the exponent, if present, is indicated by the ASCII letter e or E followed by an optionally signed integer.
    For hexadecimal literals, the exponent is always required and is indicated by the ASCII letter p or P followed by an optionally signed integer.

tux4life 2,072 Postaholic

NVIDIA

tux4life 2,072 Postaholic

Should wallet extend coin?

Whenever in doubt apply the IS A-test: Wallet IS A Coin? (or rephrased: IS Wallet A Coin?) If the answer to that question is no, then you shouldn't.

tux4life 2,072 Postaholic

Recently I was looking for books about C#. Since this thread doesn't have alot of book suggestions I'll add the results of my search to it in the hope that it will be useful to others as well.

kvprajapati commented: I love these books :) +14
tux4life 2,072 Postaholic

Have you started already? If not, why so? What in particular are you having difficulties with?
As a hint: modulo 10, division by 10, multiplication by 10; you can pretty much solve your task using these operations.