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.

tux4life 2,072 Postaholic

I hope everyone is aware that StringBuilder is superior to StringBuffer.

For those who aren't, the reason is that StringBuffer synchronizes on every call, while StringBuilder doesn't, therefore StringBuffer is thread-safe, and StringBuilder isn't.
Truth is that most of the time you don't need the thread-safety offered by StringBuffer, so it makes sense to use StringBuilder.
As an extra benefit StringBuilder will likely be faster than StringBuilder due to the lack of synchronizing.

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

This class provides an API compatible with StringBuffer, but with no guarantee of synchronization.
This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case).
Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.
(StringBuilder - Java API)

Personally I think that the real-world use cases of a StringBuffer are pretty limited. Imagine two threads operating on a single shared StringBuffer.
Threads can switch back and forth and there are no guarantees about the order in which the threads will access the shared StringBuffer.

jalpesh_007 commented: thanks +4
tux4life 2,072 Postaholic

If you decide to follow the approach explained in stultuske's post then there's no need to add instanceof checks since the identity comparisons (== on reference types) will guarantee you that the source is a particular object, and you'll know what you can safely cast it to.

tux4life 2,072 Postaholic

Here's a link to the API documentation for it:
http://docs.oracle.com/javase/6/docs/api/java/util/EventObject.html#getSource()

As you'll notice its return type is Object, so you'll need to cast to whatever the type of your event source is.
Before doing so it is always a good idea to use the instanceof operator to check if casting is possible.

Example:

Object source = e.getSource();

if (source instanceof JButton) {
    JButton button = (JButton) source;
    button.setText("Changed");
}
tux4life 2,072 Postaholic

My bad, the following part of my post is incorrect:

You probably tried fixing it by adding explicit casts in a way like:
Console.WriteLine("{0}{1}{2}", (string) ReturnPattern(n-1), n, (string) ReturnPattern(n-1));

Which results in a compiler error:
can't implicitly convert void to string/int

(explicit casts don't result in an implicit conversion)

This one gives me "Cannot explicitly convert void to string".

The error is in line 20:
return Console.WriteLine("{0}{1}{2}", ReturnPattern(n-1),n,ReturnPattern(n-1));

Console.WriteLine has return type void meaning that there is no value.
But you are trying to use it in a return statement of a method that you have defined to have a return type of string.

tux4life 2,072 Postaholic

I tried to convert it to both int and string but then I got the error "can't implicitly convert void to string/int". What to do?

My post tries to clarify on previously given information.

You defined your ReturnPattern method as follows:
static void ReturnPattern(int n).

Then you try to use Console.WriteLine as follows:
Console.WriteLine("{0}{1}{2}", ReturnPattern(n-1), n, ReturnPattern(n-1));

You probably tried fixing it by adding explicit casts in a way like:
Console.WriteLine("{0}{1}{2}", (string) ReturnPattern(n-1), n, (string) ReturnPattern(n-1));

Which results in a compiler error:
can't implicitly convert void to string/int

void means "no value", conversions apply to values, if there's no value, then how can it be converted? Method parameters take values.
You can't pass in something of type void where a value is expected, and void really means that there's no value.
If you want to use your ReturnPattern method inside a Console.WriteLine then you must make sure that a call to ReturnPattern produces a return value, which in turn can be passed as argument into parameters of another method such as Console.WriteLine.

tux4life 2,072 Postaholic

Look at the API documentation for getSystemResource:
Returns: A URL object for reading the resource, or null if the resource could not be found.

tux4life 2,072 Postaholic

The method returns an int, you can simply use System.out.println().
Here's a simple demo program:

public class Test
{
    public static void main(String[] args)
    {
        int[] a = {1, 2, 3, 4, 5};
        System.out.println(findMaxPosition(a));
    }

    // put your findMaxPosition method here
}
tux4life 2,072 Postaholic

The method signature of your checkMoney method looks like this:

public void checkMoney(int dollars, int cents)

On lines 79 (x.checkMoney();) and 84 (y.checkMoney();) you didn't pass in arguments.
Hence you'll get a compilation error actual and formal argument lists differ in length.
The actual argument list in this context is an empty one, since you didn't pass in anything.
The formal argument list is specified in the method signature. In this context it has length 2.
However I should note that merely having matching lengths is not sufficient, the types in the argument lists should be of a correct type too.

There's another error on lines 88-90 you refer to dollars and cents, you declared them as private on line 3:

private int dollars, cents;

Lines 88-90 should therefore use the getters getDollars() and getCents().

tux4life 2,072 Postaholic

I rewrote your code somewhat, and added comments so that you can see what is going on:
I did not test this code, but it should give you a basic idea.

private List<Student> createStudents(String filename) {
    /*
     * Read the file contents, putting each line into an array element.
     *
     * Assuming a valid file format, the array will contain:
     * [0] -> courseNum
     * [1] -> courseName
     * [2] -> courseSemester
     * [3]..[N] -> line representing student
     */
    String[] data = FileUtils.readIntoArray(filename);

    /* Create an ArrayList to which all students will be added as the array is processed. */
    List<Student> students = new ArrayList<Student>();

    /*
     * Create a Course object representing the unique course.
     * A reference to the (currently empty) array of Students is passed.
     * Note that this object is not used for anything further in this code.
     * See the question at the end of my post.
     */
    Course c = new Course(data[0], data[1], data[2], students);

    /*
     * Create a Student for each remaining line in the file.
     * A line that represents a Student has the following format:
     * 
     * studentID,studentProgram,studentGpa
     *
     * Note that the for needs to start at index 3 because the
     * indexes zero to two (included) hold information about the course.
     */
    for (int i = 3; i < data.length; i++) {
        /*
         * Split up the line representing a Student using comma as a delimiter.
         *
         * Assuming a valid file format, the array will contain:
         * [0] …
tux4life 2,072 Postaholic

I have a couple of questions:

  1. You defined the input file structure as follows:
    courseNum
    courseName
    courseSemester
    studentID,studentProgram,studentGpa
    studentID,studentProgram,studentGpa

    Can that whole block be repeated multiple times throughout the file?

  2. FileUtils.readIntoArray(fileName); what result does it produce?
    Does it produce an array containing the lines of the file, where each array element holds one line?
    If you don't know and have the implementation, could you post it?
tux4life 2,072 Postaholic

Also my print format doesnot display the $ and the headers.

Take a look at the DecimalFormat class.

I need the year 1 to be the initial value without doing the percent first

Print out the balance before adding the interest to it.

Are you required to use loops?

tux4life 2,072 Postaholic

so i would import the Math class

No need to import it, it's in the java.lang package, meaning that you can use all the classes in that package without having to import them explicitly.

and use Math.PI where i want to use the value of pi

Yes.

how exactly would I go about implementing this to find the ballCircumference? i assume finding the ballCircumference would be the same as finding the circumference of a circle

That should work.

tux4life 2,072 Postaholic

to return an ArrayList you just have to have your return type as arraylist

Not necessarily, you can specify a superinterface or superclass as the method's return type and return a reference to an implementation of it.
ArrayList is an implementation of the List interface so it is guaranteed to have the methods specified by that interface.

tux4life 2,072 Postaholic

I want to know how can I return ArrayList

You are actually doing this already.
In the code that you posted on line 4 you let res be a reference to a List, and set it to refer to an ArrayList.
On line 20 you have return res; which will return a reference to that ArrayList.

I'm still not sure if my code are correct

Change this:

List<Student> res = new ArrayList();
List<Course> a = new ArrayList();

to:

List<Student> res = new ArrayList<Student>();
List<Course> a = new ArrayList<Course>();

or (as of Java 7) you can also let the compiler infer the type parameter using diamond syntax:

List<Student> res = new ArrayList<>();
List<Course> a = new ArrayList<>();

in order to fix the compiler warning that will come up if you actually try to compile.

The following:

List<Student> s = (List<Student>) new Student(id, program, gpa);
List<Course> c =  (List<Course>) new Course(num, name, semester, s);

will fail at runtime due to a ClassCastException (unless Student and Course would be List implementations, which is highly unlikely, and plain wrong)

Also:

res [i] = c;

will fail at compile time because res is not an array reference.

Let me requote you:

I'm still not sure if my code are correct

I hope we can now both see that your code is incorrect and that you should've compiled and tried to fix it before posting it …

tux4life 2,072 Postaholic

You can never obtain the exact value of PI, you are always bound to the limitations of memory and computation time.
Also a perfectly correct answer is not always needed in all circumstances.
Therefore it makes sense to determine how correct you want the answer to be and use an appropriate approximation for PI that achieves the level of correctness you want.
For simple programs like this one the PI constant defined in the Math class should be sufficient.

Use like this: Math.PI

Also from an OO-perspective myBalls extends Penis doesn't make sense, because what you are really doing is defining an IS A-relationship.
Concrete to this example it means that you defined that myBalls IS A Penis, which doesn't make sense.

tux4life 2,072 Postaholic

i dont know how to create tables in sql but i have make account hope u will come to help me i send you email ... :)

Congratulations, you just limited your chances of getting good advices.
In this context email is far less productive to get answers than a forum is.
On a forum people will correct each other when one gets it wrong, you'll be confronted with different point of views, different opinions about a subject.
Moreover on a forum sharing of knowledge is encouraged, everything that gets written here may potentially be useful to other people in the future.
People here are not clairvoyant and cannot read emails sent between you and someone else, they can't see which things have been suggested already, and which haven't, what worked, and what didn't.
I hope you take that into consideration next time you decide to take such an action.

tux4life 2,072 Postaholic

Or change line 8 to:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

Although i have made corrections in your code but even then it is running for infinite

Yes, that happens when a value > 2 is entered for n, the while loop will keep looping as long as n > 2, and since the value of n never changes during loop execution, that will in theory be forever.

adikimicky commented: Yes. ,it is more better. +0
tux4life 2,072 Postaholic

I remember this snippet from Dave Sinkula.

tux4life 2,072 Postaholic

In addition: don't forget to close the file.

Lucaci Andrew commented: Good catch. Perhaps I rushed the code. :) +6
tux4life 2,072 Postaholic

I know how to read file from data and split each line using array

Then I fail to see what the problem is.

Pretty much the same question as your other thread.
This post should get you started.

tux4life 2,072 Postaholic

So basically, having
String something = "Something"
will be replaced by the compiler with
String something = new String("Something")

I'm not sure whether you can consider these as equivalent. Following that rule the following would create two distinct String instances, however that is not the case:

String something = "Bla";
String somethingElse = "Bla";

The follwing is stated in the JLS 3.10.5:

Each string literal is a reference (§4.3) to an instance (§4.3.1, §12.5) of class String (§4.3.3). String objects have a constant value. String literals-or, more generally, strings that are the values of constant expressions (§15.28)-are "interned" so as to share unique instances, using the method String.intern.

tux4life 2,072 Postaholic

In Java you have primitive and reference types.
Primitives are boolean, char, byte, short, int, long, float, double.
Anything else is a reference type.

tux4life 2,072 Postaholic

It all depends on how you read your number: if you read it in as int, use the modulo 10 divide by 10 approach. On the other hand if you read your number as a String you can either loop through all characters in the String and convert each character to a digit, or you parse the number into an int and use the modulo 10 divide by 10 approach on the parsed result.

Suppose you read it in as an int, then here's an example on how to extract the digits.
All divisions are integer divisions.

Input: 12345

12345 % 10 = 5
12345 / 10 = 1234
1234 % 10 = 4
1234 / 10 = 123
123 % 10 = 3
123 / 10 = 12
12 % 10 = 2
12 / 10 = 1
1 % 10 = 1
1 / 10 = 0 <-- Here's where we stop

Philippe.Lahaie commented: this is also what i would choose to do for this +7
tux4life 2,072 Postaholic

You probably didn't set your PATH variable correctly.

tux4life 2,072 Postaholic

but then how would I write the get method?

Have you seriously spent time using a search engine?
Under 1 minute you could've answered your own question by just Googling.
(Notice that is faster than waiting for this reply)

Anyway, here is an example:

public int get(int row, int column)
{
    return matrix[row][column];
}

Look here and here for more info.

tux4life 2,072 Postaholic

it won't be an 2d array anymore I can't go something like 'matrix[i][j]')? Anybody have any ideas, or am I approaching this completely wrong?

While in other languages such as C# or C++ you have operator overloading, there's no such feature in Java.
Your best bet is creating a method for it, e.g. get(int row, int column).
Also take a look on the Internet, Google a bit, there are no doubt examples of Matrix classes in Java.

tux4life 2,072 Postaholic

@tux4life, it is a good suggestion. I would go on that route too but the OP wants regex for a line, so I am not going to stop him/her.

Perhaps you misunderstood the point I was trying to make.

radhakrishna.p said:

is it nessasary to use regular expression concept to get your actual output?

From what follows it seems like implying that split() doesn't make use of regular expressions - which it does - that's the point I wanted to make.

tux4life 2,072 Postaholic

is it nessasary to use regular expression concept to get your actual output?
may i suggest any alternate solution to your requirement?
isn't it good to use split() for this requirement?

What do you think split() uses?

Here's its signature:
public String[] split(String regex)

API:
Splits this string around matches of the given regular expression.

tux4life 2,072 Postaholic

It is basically the same as counting in base 4.

tux4life 2,072 Postaholic

It is System.in, not System.in().