apines 116 Practically a Master Poster Featured Poster

First of all, I think you better use the Random class rather the Math.random(). If you want the game to continue until a certain condition is true, you can use the while loop:

while(expression)
{
  //do something
}

As long as the expression is true, the loop will continue. In your case, you want the expression to be true if the dice are not 2,3,7,11 or 12.

int diceSum = dice1 + dice2; //This will hold the sum of both dice tosses.
while(diceSum != 2 && diceSum != 3 && diceSum != 7 && diceSum !=11 && diceSum != 12)
{
  //do something
}

Try to work with this code, and if you encounter any problems post your new code here and we'll help you out :) (Please use the (code) button when posting code)

apines 116 Practically a Master Poster Featured Poster

As far as I know, you can't pass statically created multidimensional array as an argument to a function. The way you have done it seems like a good way (if not the only one :)). Just be careful and don't do that on a dynamically allocated array, since you cannot assume that all the cells are in the same memory chunk. Then again, if the array is dynamically allocated, you have better ways to do it.

Direwolf007 commented: Good answer! +4
apines 116 Practically a Master Poster Featured Poster

I am using Eclipse, and when using its export features I have always managed to export everything into one jar file.
Also - a quick search came up with this: http://one-jar.sourceforge.net/, might help.

apines 116 Practically a Master Poster Featured Poster

well, it is tedious hand coding them.. but its kinda satisfying, if you know what i mean.. :)

I do :) but if, for example, you have a deadline at work - satisfaction can wait, results are what important :)

apines 116 Practically a Master Poster Featured Poster

I see, then use the getClass() and getName() methods to get the name of the class as a String, for example:

Integer var = new Integer();
var.getClass().getName();

Will return "java.lang.Integer". After you have that, it's a question of comparing Strings.

apines 116 Practically a Master Poster Featured Poster

I have to admit that I am a little bit confused - if you want to check the type of the object, you can use instanceof. If you want to compare two objects of the same type you can override the equals method. I am not sure what you are trying to do...

apines 116 Practically a Master Poster Featured Poster

Glad we could help :)

Please mark the thread as solved.

apines 116 Practically a Master Poster Featured Poster

The problem is in printA, you declare the grade array list, which hides the member variable of the same name. Change this line:

ArrayList<Integer> grade = new ArrayList<Integer>();

to this

grade = new ArrayList<Integer>();

This will still cause the same problem since every time the user attempts to add a new number, the grade's constructor will be called again, erasing previous elements. Since the grade is being initialized in Average's constructor, this line can be removed.

apines 116 Practically a Master Poster Featured Poster

In your printA() method you have the line:

ArrayList<Integer> grade = new ArrayList<Integer>();

Which causes the grade ArrayList to be created again, erasing the older one. Remove this line and it should work fine :)

apines 116 Practically a Master Poster Featured Poster

In Java a booolean is a boolean, and an int is an int. 0 is not false, and 1 is not true. You can change your code to something like:

if (((num1 % 2 == 1) || (num2 % 2==1)) && (num1 % 2 != num2 % 2))
{
   System.out.println ("\n YOU LOSE! ='( " + num1 + " " + num2);
}

And this should work :)

One more comment - you are using Math.random() in order to create integers. It will be much easier to use the java.util.Random class, and using its nextInt(int n) method.

optikali commented: Correct, concise, and illuminating! +2
apines 116 Practically a Master Poster Featured Poster

well, everyone in my class is using netbeans to create GUIs.. for some reason, i hand coded mine.. you get a better understanding of GUIs..

But after you got all the understanding you need you just want to do them fast and not spend time coding them :)

apines 116 Practically a Master Poster Featured Poster

Eclipse has plugins for GUI building, that I know for sure. Never used one to recommend you about a specific one though.

apines 116 Practically a Master Poster Featured Poster

As far as I can tell it is the word wrap in the command line that causes all those phantom lines. The lines with the minus signs are longer than the ones without, which can explain the fact that the new line appears every second line. You can always try a smaller input data file if you really want to :).

apines 116 Practically a Master Poster Featured Poster

Then redirect the output to a file (at the end of you command type "> fileName.txt")

apines 116 Practically a Master Poster Featured Poster

Word wrap means that when a line is full, it will continue automatically to a new line. Assuming that you are using windows, copy all of the output from the program into notepad. Then go to Format and make sure that Word Wrap is not checked - still see those empty lines?

apines 116 Practically a Master Poster Featured Poster

It doesn't show that extra line for me - perhaps your are using word wrap and it simply wraps the line?

apines 116 Practically a Master Poster Featured Poster

It doesn't do it for me, can you please post your code with the changes?

apines 116 Practically a Master Poster Featured Poster

It won't shows in rows and numbers because you are using System.out.println all the time:

for (String str : values)
{
   double str_double = Double.parseDouble(str);
   matrix[x][y]=str_double;
   System.out.print(matrix[x][y] + " ");
}

You need to print the columns without a new line, and each time you finish a line (exit the inner loop), print a new line as well. Notice that I have changed the locations where you increment x and y - inside the appropriate loops.

while ((line = in.readLine()) != null)	//file reading
{
   String[] values = line.split(",");
   for (String str : values)
   {
      double str_double = Double.parseDouble(str);
      matrix[x][y]=str_double;
      System.out.print(matrix[x][y] + " ");
      y=y+1; //you have inserted a value to the former y, need to increment
   }
   x=x+1; // finished the row, need to increment the row number
   System.out.println(""); // print a new row.
}
apines 116 Practically a Master Poster Featured Poster

For the ArrayIndexOutOfBoundsException I think you should have used this method:
scan.hasNextLine() instead of scan.hasNext()
But you should wait for apines comment, since it's his code.

For the other bug, try to add some System.out.println where you read the file and add elements to the list in order to see what is read. If the error still remains, just ignore the first line.

minimi stated that he is not allowed to use arrays, so my code is out - hasNextLine() seems like a good solution though :)

apines 116 Practically a Master Poster Featured Poster

The regex which you are using in the useDelimiter method is "\\s*" - * means 0 or more, when in fact you want 1 or more (since you want at least one space to separate between the name and the age) - try using "\\s+", which means one or more whitespace character.

apines 116 Practically a Master Poster Featured Poster

The split method returns an array. If you are not allowed to use them - use your previous methods, just make sure that they work. Your main problem was what javaAddict and I have stated - your comparison was wrong.

minimi commented: Thank you very much! Great Help! +1
apines 116 Practically a Master Poster Featured Poster

Ok, found your problem. First of all the way that you have parsed your file returned "ERROR: null" when I have tried to run it. I have changed it a little bit so it will work for me:

scan = new Scanner(new File(fileLoc));
if(scan != null) 
{
   String[] line;
   while(scan.hasNext()) 
   {
	   line = scan.nextLine().split("\\s+");
	   name = line[0];
	   age = line[1];
	   list.add(name, age); // add to list
   }
}

Second, in the displayPeopleWithSameAge(String input) method you have the following line:

if (input.equals(current))

Which basically compares the String input with the PersonNode current - if I am not mistaken this is actually comparing the input with PersonNode's toString() method - no way that those two will be equal right?

I have changed it to be

if (input.equals(current.getAge()))

Where getAge() is a getter I have added in order to retrieve the age member from the PersonNode instance.

public String getAge()
{
   return age;
}

EDIT: javaAddict beat me to it by 3 minutes - using the same getter - ++javaAddict :)

apines 116 Practically a Master Poster Featured Poster

I am getting "ERROR: null" when trying to run your program with a file identical to the one you have posted here - are you sure that the Scanner usage is correct?

apines 116 Practically a Master Poster Featured Poster

Not looking to good for me

Actually it seems that you are progressing so don't give up.
Your multiplication code:

for(double i = 0; i <factor2; i++)
{
   product += factor1;
}

This code will work - it states that if you want to calculate product*factor you simply add up product factor amount of times.
The division should work just like the multiplication, meaning that if you get var1 and var2 then var1/var2 is basically the question is how many times var2 can be contained inside var1 (6/3 means how many times 3 is contained inside 6, and the answer is 2).
In your division code you ask for dnum1 and dnum2, and then you do

for( double i = 0;i>dnum1;i++)
{
   quotient=+i;
}

Where did dnum2 go? remember - dnum1/dnum2 simply states how many times can you contain dnum2 inside dnum1. One way to solve this problem is to subtract dnum2 from dnum1 as many times as you can, which is the number of times until dnum1 is smaller than dnum2 and then you can't subtract dnum2 from it anymore. What we got so far:

while(dnum1 >= dnum2)
{
   dnum1 -= dnum2;
   ++result;
}

Now, two more comments about your code if I may - first you should stick to Java's naming conventions. Class names should start with a capital letter and methods should start with lower case letters. Addition() should be addition() and so on. This is not a must but will …

apines 116 Practically a Master Poster Featured Poster

If you are not forced to use charAt() (assignment restrictions, etc') I recommend a different approach - using String's split(String regex) method with the regex "/", you can divide your String into a String array with the day, month and year in its cells. This will be much more convinient code-wise and will be more flexible in terms if how user formats the date (i.e. 2/5/10, 2/5/2010, 2/05/2010 will all work).

intes77 commented: thaaanks +1
apines 116 Practically a Master Poster Featured Poster

No problem :) please mark the thread as solved.

apines 116 Practically a Master Poster Featured Poster

You need to multiply. For each iteration of the outer loop, the middle loop is doing all of it's iterations. For each iteration of the middle loop, the inner loop is doing all of it's iterations. Let's take an easier example:

for(int i = 0; i < 3; ++i)
{
   System.out.println("Started Outer Loop!");
   for(int j = 0; j < 5; ++j)
   {
      System.out.println("Started Inner Loop!");
   }
   System.out.println("Finished Outer Loop!")
}

Now, when the outer loop enters the first iteration, and starting to execute the lines inside its code block, the first thing that will happen then, is that the line "Started Outer Loop!" will be printed. Afterwards the inner loop will start. When entering the second loop, from j=0 to j=4, meaning 5 iterations, the line "Started Inner Loop!" will be printed. Afterwards the line "Finished Outer Loop!" will be printed. After that the first iteration of the outer loop will be finished, and we will start it again, this time with i=1. This procedure will go over and over again until i=3, and the outer loop finishes. This means that for 3 times, from i=0 to i=3, the inner loop did 5 iterations, from j=0 to j=5 - overall 3*5=15. On the screen, at the end, you will have the total number of 3 "Started Outer Loop!", 3 "Finished Outer Loop!" and 15 "Started Inner Loop!" (obviously not in that order :))

apines 116 Practically a Master Poster Featured Poster

Well, for maximum flexibility you can always pass the 2D array as a parameter. If you pass a displayText 2D array as the parameter, you will be able to initialize each objPiece with its own displayText.

apines 116 Practically a Master Poster Featured Poster

You want to know after how many iterations a will go from 1 to n when jumping in steps of a*9. After the first iteration a=9, after the second iteration a=9*9=9^2=81, third iteration a=9*9*9=9^3. After k iterations, you will have a=9^k, Assuming the a = n after x iterations, it means that n=9^x -> x = log9(n).

apines 116 Practically a Master Poster Featured Poster

There was a similar question here. Try to review the example, and if you still encounter any trouble I'll go over it with you.

apines 116 Practically a Master Poster Featured Poster

You can pass the displayText as a parameter

public void fillGrid(String displayText) 
{
   for(int i = 0; i < ROWS; i++) 
   {
      for(int j = 0; j < COLUMNS; j++) 
      {
    	 board[i][j] = new objPiece(displayText);
      }
   }
}
apines 116 Practically a Master Poster Featured Poster

Glad to hear that it's working! Try to work on the palindrome with the techniques you have learned :)

Please mark the thread as solved.

apines 116 Practically a Master Poster Featured Poster

How about the simple old fashioned

i.next = new Node(key);

:)

apines 116 Practically a Master Poster Featured Poster

Your newNode(int n) method only inserts the new node if (head == null), but does nothing afterwards except to iterate all the way to the end.

public void newNode(int n)
{
   key = n;
   Node i ;
   if (head == null)
   {
      head = new Node(key); // Great, we will have a new node here.
   }
   else
   {
      i = head;
      while (i.getNext() != null)
      {
         i = i.getNext();
      }
      // ok, i is the last node here... now you need to insert the node.
   }
}

Your list only has the first node inserted, which can explain your results...

apines 116 Practically a Master Poster Featured Poster

Sure thing! just mark the thread as solved (It is, sort of, solved here, no? :))

apines 116 Practically a Master Poster Featured Poster

Please post the code as text and not as a picture...

apines 116 Practically a Master Poster Featured Poster

This is the Java forum, not JavaScript - you probably want this forum :)

apines 116 Practically a Master Poster Featured Poster

Ok...
First, the method is accepting two integers, a and b - and then you trying to compare the nodes instead of a and b in question. The method should accept a and b and return the higher value of the two.

public int max(int a, int b)
{
   if(a > b)
   {
     return a;
   }
   return b;
}

Can you see the difference between this and what you wrote?
Second, regarding the complication error - you are trying to compare an int (node.getKey()) and a node (node.getNext()).
Perhaps you want to compare it with node.getNext().getKey()

apines 116 Practically a Master Poster Featured Poster

You can't omit the max, since this is the actual line that does the comparison between the two numbers - the number in the current node, and the number returned from the following nodes in the recursion. You can implement a max method of your own, or simply use the max(int a, int b) provided in the Math class.

apines 116 Practically a Master Poster Featured Poster

Glad I could help! Please mark the thread as solved :)

apines 116 Practically a Master Poster Featured Poster

No problem, happy to help :) please mark the thread as solved :)

apines 116 Practically a Master Poster Featured Poster

I agree with Taywin, recursions can be hard at the beginning. Let us try and review our code for a linked list of length 4, containing [22]->[43]->[107]->[45]->[null].
First, we call findMax(), it will call findMax(Node node) where node == head:

//Iteration 1

int findMax(Node node) // node == head, and head.getKey() == 22
{
   if(head.next() == null) //we know that head.next() != null, so we are going to the else clause.
   {
      return node.getKey();
   }
   else
   {
      return max(node.getKey(), findMax(node.next())); //meaning we need to return the max between 22 and the result of findMax(head.next()).
   }
}

The method then needs to call to findMax(head.next()) in order to determine which one is bigger:

//Iteration 2

int findMax(Node node) // node == head.next(), and head.next()getKey() == 43
   if(head.next() == null) //we know that node.next() != null, so we are going to the else clause.
   {
      return node.getKey();
   }
   else
   {
      return max(node.getKey(), findMax(node.next())); //meaning we need to return the max between 43 and the result of findMax(head.next()).
   }
}

And again, the method needs to call to findMax(head.next().next()) in order to determine which one is bigger:

//Iteration 3

int findMax(Node node) // node == head.next().next(), and head.next().next().getKey() == 11
   if(head.next() == null) //we know that node.next() != null, so we are going to the else clause.
   {
      return node.getKey();
   }
   else
   {
      return max(node.getKey(), findMax(node.next())); //meaning we need to return the max between 107 and the result of findMax(head.next()).
   }
}

And yet again, the method has to …

apines 116 Practically a Master Poster Featured Poster

You did not compare temp to the other cells of the array in order to make sure that the number is unique. Please read again what I wrote to you here - you simply skipped that step.

apines 116 Practically a Master Poster Featured Poster

The comparison is done in this line:

return max(node.getKey(), findMax(node.next()));

Where you return the max of the current result and the result of the next recursion call. Try to visualize it... can you see it? :)

apines 116 Practically a Master Poster Featured Poster

the syntax for the following methods is not coorect:

public return getMonthlyInterestRate(annualInterestrate)
public return withdraw(balance, withdrawlAmount)
public return deposit(balance, depositAmount)

The Correct syntax is

public double getMonthlyInterestRate(double annualInterestrate)
public double withdraw(double balance, double withdrawlAmount)
public double deposit(double balance, double depositAmount)

The syntax of Java methods is

<scope> <return type> <method name> (<parameters>)

Where the parameters are optional and if there is no return type it should be written as "void". Since your methods return double, the return type should be double. Also, in the method declaration, you have to define the type of each parameter, therefore you need to define "double balance" and not just "balance" - the compiler needs to know what type of variable is balance.

apines 116 Practically a Master Poster Featured Poster

You have declared your average as an int - so it can only show int values. I see that I have done the same in my explanation. The average can hold real values and not only integers, so let's change it to double.

double average

Now, we indeed want to put inside average sum/10, but beware - in Java, if you divide to integers, the result will be integer, therefore the code

average = sum / 10;

will result in an integer (5 again in our case). The solution is simple - you need to cast one of the variable in the division to a double, and then the result of dividing an integer and a double will be a double. So let's try this:

average = ((double)sum) / 10; //casting sum to a double before dividing

This should give you the result you need. Your sum works fine as it is :)

@Eric Cute - the ++i and i++ will do the same in our case, and the ++i is more efficient since you don't need to hold a temp copy and return it after the incrementation.

apines 116 Practically a Master Poster Featured Poster

The method course points returns a double as a result, but inside your main method you are not assigning is to the variable (line 28), so the variable is not changed. Try this:

//This line should use the coursePoints variable from the method below and display it.
coursePoints = coursePoints(grade,credits); //the method return a double value, assign it to the proper variable.

Your code works great, you just forgot this assignment. Perhaps you should not use the exact same name for variables and methods, it might be confusing.

apines 116 Practically a Master Poster Featured Poster

If you guys are planning to study together to the exams, it's in your interest to share the notes with them - maybe they will understand things that you didn't, and by sharing with them it will help you as well. I am speaking as someone who took notes for most of his B.Sc and shared them with his buddies. It helped me just as I just told you, and in later years there were courses that I couldn't take notes in (wasn't able to, two courses were on the same time etc') and my buddies took the notes and shared them.

apines 116 Practically a Master Poster Featured Poster

jwill, please do not use caps-lock. What code do you have so far? What have you tried?

apines 116 Practically a Master Poster Featured Poster

The solution you have provided for the first question is not recursive. You can have a recursion that will stop when max.next() is null, and otherwise will return the max value between the current node and the next node.

int findMax(Node node)
{
   if(node.next() == null)
   {
      return node.getKey();
   }
   else
   {
      return max(node.getKey(), findMax(node.next()));
   }
}

And you will need a method that will call the recursion with head

int findMax()
{
   if(head == null)
   {
      return null;
   }
   else
   {
      return findMax(head);
   }
}

Regarding the second question - how would you check for a palindrome in a non-recursive manner? Try to do this and maybe the recursion will seem more clear.

Good luck!