apines 116 Practically a Master Poster Featured Poster

In a loop you can use the keyword continue to stop the current loop iteration and proceed to the next one, and the keyword break that simply exits the loop.

apines 116 Practically a Master Poster Featured Poster

I think at line 17 my drawing from before confused you - you cannot assume that head is the node that you need to change. You need to change the element that is before the first element that you are trying to swap. As kramerd said - if node1 is the 49th and node2 is the 50th, you will have to change the (node 48th).next to point at node2, and you will not touch head during the entire swap process. I suggest that you draw the drawing again, using different places for the nodes - at the head, tail and in the middle, to be sure that you are not stuck on only one scenario.

apines 116 Practically a Master Poster Featured Poster

apines, in order for this approach of setting a boolean to work, you need to put a break statement just after setting the boolean to true inside the loop. Otherwise it can just get set to false again in a different loop iteration and you won't have the value you want at the end.

Also, in your second code snippet, you have a void method that returns a boolean value.

Well, you are right about the void method returning a boolean, but I don't need a break since the boolean is set to false at the beginning and set to true if there is an empty sit. Can we settle on just half my bad and not a full one? :) I didn't put the break statement because it can confuse and this is not the point of the explanation :)
The method should be public static boolean makeFirstReservation(int[][] totalSeats, Scanner input)

apines 116 Practically a Master Poster Featured Poster

Let's take a look at makeFirstReservation method:

public static void makeFirstReservation(int[][] totalSeats, Scanner input)
{
   for (int row = 0; row < 3; row++)
   {
      for (int col = 0; col < 4; col++)
      {
         row = input.nextInt();
         col = input.nextInt();
         System.out.println(totalSeats[row][col]);
      }
  }
}

Every iteration of the inner loop you are asking the user again for row and col, and then print the number inside that specific 2d array cell - not exactly what you wanted. You want the user to decide where he wants to sit, and then you want to check if you have any sits left. Let's assume that the user chose first class. You want to go over the matrix cells until you encounter an open sit (the first cell with 0) - if there is such a cell, then success, the user have a boarding pass and you need to change that cell to 1, otherwise the user will be back to the menu to choose another class.
So in the makeFirstReservation method we basically want to check if there is a cell with value 0 in the range of the first 3 rows, all four columns. We will use a boolean variable to tell us whether there is an empty sit or not - we will scan the matrix, and when (and if) we will find an empty sit, then the variable will be equal to true:

public static void makeFirstReservation(int[][] totalSeats, Scanner input)
{
   boolean emptySit = false; …
apines 116 Practically a Master Poster Featured Poster
a=a+b;
b=a-b;
a=a-b;

Will indeed swap to variables without a temp. Notice that you have to make sure that a+b does not exceed the maximum allowed integer. There is another way of swapping the variables without these constraints:

void swap (int *x, int *y) 
{
   if (x != y) 
   {
      *x ^= *y;
      *y ^= *x;
      *x ^= *y;
   }
}
apines 116 Practically a Master Poster Featured Poster

Try to do one swap as showed in the diagram and as kramerd elaborated, and than think about special cases that need your attention, such as what if the length of the linked list is 1, or 2?

minimi commented: Helpful tip! +1
apines 116 Practically a Master Poster Featured Poster

Your method:

static int random(int die1, int die2)
{
   die1 = (int)(Math.random()*6) + 1;
   die2 = (int)(Math.random()*6) + 1;
}

From the header static int random(int die1, int die2 ) we know that the method takes two int parameters, die1 and die2, and returns an int. This is not exactly what you want, right? You want the method to simply return a number that will represent the sum of the two die throws. Meaning you want the method to return an int, but you don't need any parameters sent to it. We should write:

static int random()
{
   int die1 = (int)(Math.random()*6) + 1;
   int die2 = (int)(Math.random()*6) + 1;
}

Now, this method computes two int random values, but doesn't do nothing with them - we need to explicitly tell the method what we want to return. Since you stated the int in "static int random", the compiler will shout at you if you won't return an int in the end of your method. We need to use the return keyword in order to return a value from the method. We said that we want to return the sum of the two die throws, so we can write:

static int random()
{
   int die1 = (int)(Math.random()*6) + 1;
   int die2 = (int)(Math.random()*6) + 1;
   int dieSum = die1 + die2; //calculating the sum
   return dieSum;  //returning the sum;
}

Or, in the same way, we can simply write:

static int random()
{
   int die1 = …
apines 116 Practically a Master Poster Featured Poster

When do you increment? if you need only the number of rows of the matrix, you need to count only when you finish with a row, and not every cell (which will increment every column and every row, more than you need).

apines 116 Practically a Master Poster Featured Poster

The method is PlayerWins(diceSum) - but you are calling PlyerWins(diceSum) .

FYI - the Java naming conventions encourage methods and variables to start with a lowercase letter, and classes with an uppercase letter.

apines 116 Practically a Master Poster Featured Poster

My guess is that your compiler errors are from not recognizing Console.readString() - I am not familiar with that method in the Console class. Second thing is that your method is called PlayerWins but you are invoking on line 37 PlyerWins.

Are there any other compiler errors?

apines 116 Practically a Master Poster Featured Poster

Take a look at the equals method - public boolean equals(Object obj) . This method only gives true or false depending on whether the two objects are equal to one another. In this case - if they are the same object. You want to check the age variable in both nodes, and not just if they are equal - if they are bigger, equal or smaller - I think the compareTo method is better for that purpose :)

apines 116 Practically a Master Poster Featured Poster
  1. When you insert the values to the arrays, keep a counter - this way you will know what was the last cell that you have written to - at the end instead of RMSD_Final = R2 / coordinatesRotZ.length; just do RMSD_Final = R2 / counter;
  2. The loop in line 164 accumulates all the R1 into R2, just be sure that R2 is 0 when you start.
apines 116 Practically a Master Poster Featured Poster

Well, if your code works, there is no reason to post it again I guess.
Glad I could inspire :) Please mark the thread as solved.

apines 116 Practically a Master Poster Featured Poster

After you finish the inner loop, you need to erase the information inside the String Builder, or to adjust the loops to append just the needed string. Right now you are appending "Bark" at the first line, then in the second line you append to the same string "Bark Meow" and so on - it will create a long string.

apines 116 Practically a Master Poster Featured Poster

When you do PersonNode x = current.getAge() you are trying to insert an int into a variable that can store PersonNode. The correct syntax should be PersonNode = current - that states that x, which is a PersonNode, points to the same place in the heap that current is pointing to. After that, doing x.getAge() is the same as current.getAge() , and doing node.next = x is the same as node.x = current .
If I have confused you, or you don't know what is the heap, tell me and I will explain it to you :) There is a big difference between trying to swap primitive types such as int, boolean, char etc' to objects, which are by reference in Java such as your PersonNode.

apines 116 Practically a Master Poster Featured Poster

Can you please include the entire code?

apines 116 Practically a Master Poster Featured Poster

kremerd is right - drawing is very helpful. I will try to help you out with the pictures. We will do together one scenario, you will try and translate into code. The rest of the scenarios will be easier then I believe. Let's have this linked list:

1.png

We want to swap node1 and node2, meaning that the list should go head [TEX]\rightarrow[/TEX] node2[TEX]\rightarrow[/TEX] node1[TEX]\rightarrow[/TEX] the rest of the list as usual. First thing let's change node2.next() so it will point to next1, the next in line in the new order: 2.png

Now, node1.next() should point to where node pointed before, because now this is his new place in the list: 3.png

And eventually we need head, who pointed at node1, to point now at node2: 4.png

Now, note that you need not to "lose" your nodes, for example after changing node2.next() to point to node1, we make the older node2.next() unreachable: 5.png

We can make a temp node that will point to node2.next() before changing it to node1: 6.png

You need to be sure that during the entire swap you don't lost any nodes.

This was one swap scenario - can you think about more scenarios that you need to check and handle as part of the swap?

apines 116 Practically a Master Poster Featured Poster

An ArrayList of String[] is an ArrayList where each cell contains an array of String. This can be looked at as a dynamic 2d matrix. For example:

ArrayList<String[]> arr2d = new ArrayList<String[]>(); //The proper way to declare an ArrayList of String[]
String[] str1 = new String[2];
str1[0] = "hello";
str1[1] = "world";
String[] str2 = new String[3];
str2[0] = "how";
str2[1] = "are";
str2[2] = "you?";
arr2d.add(str1);
arr2d.add(str2);

This will create and ArrayList with 2 cells, the first one contains an array of length 2, and the second one is an array of length 3.

apines 116 Practically a Master Poster Featured Poster

In order to parse the words, you can use the code string Pattern w = Pattern.compile("\\W+"); . The sentences are more problematic, since a sentence can be across lines. After parsing a sentence and all of its lines you have to check whether the last line ends a sentence, and if not you need to extract the next line (or lines, depends on the length of the sentence) and concatenate all of the fragments together into one sentence. A very rough draft should be something of the sort:

  1. Retrieve a line from the file.
  2. Parse all the sentences from it.
  3. Check whether the last part is indeed a complete sentence
  4. If not - hold a variable states that the sentence is not finished yet.
  5. Parse the words, continue to the next line.
  6. When reaching the end of the sentence, check whether you have a sentence from before - if you do, concatenate it to the sentence you are parsing now.
apines 116 Practically a Master Poster Featured Poster

This is the code (in Java, if you don't know it don't worry, it is very similar so you won't have trouble understanding) for creating the wanted output with loops, hopefully this will help your project:

public static void main(String[] args)
{
   String str = null;
   boolean needToBark = true;
   int row = 10;
   for (int i = 0; i < row; ++i)
   {
      str = "";
      for(int j = 0; j < i + 1; ++j)
      {
         if(needToBark)
	 {
	    str += "Bark ";
	 }
	 else
	 {
	    str += "Meow ";
	 }
	 needToBark = !needToBark;
      }
      System.out.println(str);
      needToBark = true;
      str = "";
   }
}
apines 116 Practically a Master Poster Featured Poster

The file name has to be the same name as the class - class Main has to reside in the file Main.java.

apines 116 Practically a Master Poster Featured Poster

Glad to help, please mark the thread as solved.

apines 116 Practically a Master Poster Featured Poster

What is the name of the file containing the class?

apines 116 Practically a Master Poster Featured Poster

The syntax for creating a new instance is

Class1 varName = new Class1();

This will call the default constructor (you haven't defined any other). Now, to access the only thing you have in the class, which is the getAllSubjects() method, you use the dot notation syntax

varName.getAllSubjects()
apines 116 Practically a Master Poster Featured Poster

Your matrix is 1000*1000, and you don't want to print all of the values, and you don't want to change the size of the matrix. My suggestion is to save the values of x and y that you use when inserting the values into the matrix, and use them when printing the values to the user - instead of printing all of the array, print only until the x and y.

apines 116 Practically a Master Poster Featured Poster

The compiler states that he cannot found the class named Main. Can you post your code?

apines 116 Practically a Master Poster Featured Poster

While inserting the elements to the matrix you can count which cells were used, and then print only them to the user.

apines 116 Practically a Master Poster Featured Poster

Perhaps you will find more help in the JSP forum :)

apines 116 Practically a Master Poster Featured Poster

The number of cells in your matrix should be the same as the numbers of elements in the text file - every extra cell has its initial value to be a 0.0. If you take my text file and put the matrix to be 4x4, you will see extra 0.0.

apines 116 Practically a Master Poster Featured Poster

For example, in line 87 in the new code you have the exact same loop as in the older code, and you are not changing the indices, just like you didn't do in the older code. You have changed it in the older code and now it works - and in your new code you didn't. Review the new code, fix the similar problems, and post it again fixed.

Edit: You have posted the requested code as I was writing this post :)

apines 116 Practically a Master Poster Featured Poster

Sure, please post the other code after you have made the changes as we made in this code. You have similar problems there as you had here. Fix them, and if it still doesn't work post the new code again.

apines 116 Practically a Master Poster Featured Poster

Because you ask it to print in the constructor (lines 27, 32) and in the main method (lines 52, 54).

apines 116 Practically a Master Poster Featured Poster

Use the file attached, myDouble to be a 3x3 matrix:

static double[][] myDouble = new double[3][3];

Remember to change the path in your code to the new file. Still the same?

apines 116 Practically a Master Poster Featured Poster

Line 87 you have the same loop, and you are not changing the indexing. Please correct all the stuff that we have talked about in the other code first.

apines 116 Practically a Master Poster Featured Poster

First, notice that you are handling objects, which are by reference. The code

PersonNode temp = current;

Means that temp points to the location in the heap of current - changes that you are making for current will also affect temp! You will have to copy the object using a copy constructor or simply hold the instance members in different variables (hold int age for the age etc').

Also - you never change swap, it is always false, so I don't understand what you are doing with it in the bubble sort.

apines 116 Practically a Master Poster Featured Poster

What can I tell you - I copy pasted your code and it works with a slight index problems, which occur because you didn't reset the columns in each iteration:

while ((line = in.readLine()) != null)
{
   String[] values = line.split(",");
   for (String str : values)
   {
      strDouble = Double.parseDouble(str);
      myDouble[x][y] = strDouble;
      y=y+1;
   }
   x=x+1;
   y=0; // <----- you need to reset the columns index.
}

So I can't see why your code does not work...

apines 116 Practically a Master Poster Featured Poster

Tried it and it works for me, post your code please.

apines 116 Practically a Master Poster Featured Poster

I have answered your same question here...

apines 116 Practically a Master Poster Featured Poster

ok, let's review methods a little bit :) When creating a method, the syntax is:

<visibility> <optional static> <return type> methodName(<optional parameters>)

For example, let's consider the most famous method

public static void main(String[] args)

The visibility is the public keyword, it is static (if you don't know what static is, ask and I will elaborate). Its return type is void, which means it does not return anything, the name of the method is main and it accepts one String array argument named args. Notice that in the method declaration, we put the type of each parameter before its name - we indicate that args is String[].

If we wanted to create a public, not-static method which returns an int, called myMethod and accepts two parameters - one double named doubleVar and a char named charVar, we would have written:

public int myMethod(double doubleVar, char charVar)

Again, we indicate that doubleVar is double and charVar is char.

Now, we want to call this method from another method. The syntax for it is simple:

methodName(<optional arguments>)

Let's say that we want to call myMethod from the main method, the syntax would be:

public static void main(String args[])
{
   double dVar = 4.564;
   char hello = 'c';
   myMethod(dVar, hello);
}

Notice that this time we didn't need to specify the type (we didn't write myMethod(double dvar, char hello)) - the type that needs to be sent to the method is known to the compiler from …

apines 116 Practically a Master Poster Featured Poster

It's printing 1000^2=1,000,000 elements, it won't fit in your console screen, and it will take some time to stop. If you want to be sure, make another, smaller, file and make the matrix 10^10 - then see if it prints all the elements.

apines 116 Practically a Master Poster Featured Poster

The error is because the line

if(PlayerWins)

States that if the variable PlayerWins is true, execute the code block. What we want is

if(PlayerWins(diceSum))

Which means if the method PlayerWins is true when passing to it the argument diceSum. The compiler doesn't know what is the variable PlayerWins only the method, so it returns an error.

I am not sure that I understood your second question, can you explain?

apines 116 Practically a Master Poster Featured Poster

The main method is ArrayTest and not Testing - the main is not inherited, testing is never called and nothing is printed. Before changing the structure take a look at the main method, you forgot to increment x and y when populating myDouble:

while ((line = in.readLine()) != null)
{
   String[] values = line.split(",");
   for (String str : values)
   {
      strDouble = Double.parseDouble(str);
      myDouble[x][y] = strDouble;
      ++y; //increment the columns!
   }
      ++x; //increment the rows, and start over the columns
      y = 0;
}

Now, regarding the structure of your code - one solution is to move your method from main to the constructor, so when an ArrayTest object is constructed, its myDouble will be populated:

public ArrayTest()
{
   try
   {
      BufferedReader in = new BufferedReader(new  FileReader("D:\\Downloads\\Testing2.txt"));
      String line;
      while ((line = in.readLine()) != null)
      {
         String[] values = line.split(",");
         for (String str : values)
         {
             strDouble = Double.parseDouble(str);
             myDouble[x][y] = strDouble;
             ++y; //increment the columns
         }
         ++x; //increment the rows
         y = 0; //start over the columns
       }
       in.close();
   }
   catch(IOException ioException) { }//always good to log this
}

Now, in order to test your method you can create a main method inside TestArray as well, such as:

public static void main(String[] args)
{
   ArrayTest arr = new ArrayTest(); 
   double[][] residuescores = arr.myDouble;
   for(int i = 0; i < myDouble.length; ++i)
   {
	for(int j = 0; j < myDouble[i].length; ++j)
	{
	   System.out.print(ArrayTest.myDouble[i][j] + " ");
	}
	System.out.println(" ");
   }
}

Now you have no usage to …

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

The problem is with the line

if(p1array.equals(p2array))

It basically tries to check if p1array and p2array are the same object (it uses the native Object's equals method). You have to loop over the array and check if each cell is equal to the same cell of the other array. Since you use sort, the two arrays will be equal if and only if each cell contains the same character.

public boolean isAnagram()
{
   p1array = phrase1.toCharArray();
   java.util.Arrays.sort(p1array);
   p2array = phrase2.toCharArray();
   java.util.Arrays.sort(p2array);
   boolean equal = true;  //to check if all the cells are equal

   if(p1array.length != p2array.length)
   {
	return false; 
   }
		
   for(int i = 0; i < p1array.length; ++i)
   {
      if(p1array[i] != p2array[i])
      {
	equal = false; //one letter does not equal to the other, this is not an anagram.
      }
   }
   return equal;
}
apines 116 Practically a Master Poster Featured Poster

Have you fixed your letterchange method as Coil suggested? Strings in Java passed by value to the method. Changing the value of the phrase1 inside the method will not affect it outside of it. You need your method to return the new String and update the variable accordingly:

public String letterchange(String phrase)
{
   return phrase.toLowerCase();
}

And the way to use it:

anag.phrase1 = letterchange(anag.phrase1);
apines 116 Practically a Master Poster Featured Poster

Have you tried png? jpeg does not support transparency.

apines 116 Practically a Master Poster Featured Poster

The number of values that will be in argv is exactly argc. When starting the program argc will be equal to the number of elements in argv, meaning that argv[0] through argv[argc-1] are existing cells.

apines 116 Practically a Master Poster Featured Poster

The main method must be static. One way to go is to create a new Anagram object and use the dot notation to access its methods.

public static void main(String args[])
{
   Anagram anag = new Anagram();
   anag.phrase1 = args[0]; //using dot notation to access the members phrase1 and phrase2
   anag.phrase2 = arg[1];

   letterchange(anag.phrase1);
   letterchange(anag.phrase2);
 
   if (anag.isAnagram()) //using the dot notation also to access the isAnagram() method
   {
      System.out.println(anag.phrase1 + " is an anagram of " + anag.phrase2);
   }
   else
   {
      System.out.println(anag.phrase1 + " is not an anagram of " + anag.phrase2);
   }
}
apines 116 Practically a Master Poster Featured Poster

Is this an assignment that you have or a possible solution to your problem? If it is the latter it's possible that there are better solutions - would you like to post the problem?

apines 116 Practically a Master Poster Featured Poster

This is not the correct usage of a while loop - your code will return true if the dice sum is indeed 2,3,7,11 or 12 but will be stuck in an infinite loop otherwise because you do not change the value of diceSum inside the loop. After reading again the problem, I believe that kramerd's idea is better and will give you the usage of methods that you wanted. Define a method isPlayerWins

private boolean isPlayerWins(int diceSum) {
   return diceSum == 2 || diceSum == 3 || diceSum == 11 || diceSum == 12;
}

This method will return true if the player wins, false otherwise. Now, what you need is

public static void main(String args [])
{
   System.out.println("Would you like to play the game? (y/n) ");
   String responseLine = Console.readString();
   char response = responseLine.charAt(0);   		
   if(response == 'y')
   { 
        int diceSum = random(1, 6) + random(1,6) //you need to complete this method that returns the dice throw.
	for(int i = 0; i <= 10; i ++)
	{   
   	   // using the isPlayerWins(diceSum) method, you need to determine whether the player won or lost.
 	}
   }
}

Try to implement the code using this new information, good luck :)