Jump to content

Working with matrices

- - - - -

  • Please log in to reply
20 replies to this topic

#1
qutazs

qutazs

    Newbie

  • Members
  • PipPip
  • 23 posts
Lets change the question..
I have a HashMap with following settings:

HashMap hm = new HashMap<String, Matrix>();


Observe the next part:

public void init() {

    hm.put("a", new Matrix(3, new int[][] {{1,2,3},{4,5,6},{7,8,9}}));

}


And the Matrix.java file looks like this:

import java.util.*;


/**

 * @author Mark Lonquist

 * @version 1.0

 */


public class Matrix {

    

    public Matrix(int matrixSize, int[][] numbers)

    {

        matrix = new int[matrixSize][matrixSize];

        matrixNumbers = numbers;

        matrix = matrixNumbers;

    }

    

    public int[][] matrixNumbers(Matrix matrix) 

    {

        return (matrixNumbers);

    }

    

    public int size() 

    {

        return matrix.length;

    }

    

    public int sizeInside() 

    {

        return matrix[0].length;

    }

    

    private int matrix[][];

    private int matrixNumbers[][];

       

}


It gives me the following error when running this "main":

public static void main(String[] args) 

    {

        Program program = new Program();

        program.init();

    }



run:

Exception in thread "main" java.lang.NullPointerException

	at Program.init(Program.java:109)

	at Program.main(Program.java:116)

Java Result: 1

BUILD SUCCESSFUL (total time: 0 seconds)


Why is this??

Edited by qutazs, 05 December 2011 - 09:44 AM.


#2
qutazs

qutazs

    Newbie

  • Members
  • PipPip
  • 23 posts
How do I make a method which can rotate this:
{5,0,0}
3 times (cause the length is 3), so I get this back:
{{5,0,0},{0,5,0},{0,0,5}}


---------- Post added at 08:52 PM ---------- Previous post was at 08:51 PM ----------

Been trying to do it for hours :P

#3
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
I did not get any errors. Google Permutation.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#4
qutazs

qutazs

    Newbie

  • Members
  • PipPip
  • 23 posts
if I want to take a String input as follows:
1 2 3

How would I take each number in the string and convert it to int, and then place it in a {}?
Also, if matrixsize has been set to lets say 3,
it would ask for this:
Please enter row 1:
> 1 2 3
Please enter row 2:
> 2 3 4
Please enter row 3:
> 3 4 5

I then want to take those numbers and put them into a list of lists like follows:
{{1,2,3},{2,3,4},{3,4,5}}

I just cant my head around it.. Once I know how to do this, I dont think I will have any trouble with rotating the numbers for the diagonal making.

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
  • Split the String using .split(..). It takes a parameter which you use to tell on which character ( regex ) to split on, just a space in this case: " ".
    It will return a String array.
  • Use int number = Integer.parseInt("3"); to parse to an integer


#6
qutazs

qutazs

    Newbie

  • Members
  • PipPip
  • 23 posts
Use int number = Integer.parseInt("3"); to parse to an integer
I guess I would parse the int from the array using String[0] (example)? right?

It can be done with a for() loop I guess. When I got all 3 rows, how would I then add them to a list list? {{row1},{row2},{row3}}? It needs to be able to take more than 3 rows, and also less.

#7
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
You can do something like:
List<String> rows = new ArrayList<String>();

//loop
rows.add(scanner.nextLine() );
//end loop

int[][] matrix = new int[rows.size()][];
for(int i=0 ; i<rows.size(); i++){
   String[] entries = rows.get(i).split(" ");
   matrix[i] = new int[entries.length];
//Loop trough entries, parse and put in matrix.
}


#8
qutazs

qutazs

    Newbie

  • Members
  • PipPip
  • 23 posts
Thanks alot :) Ill give it a go, might need some tuning, but its a great start!

---------- Post added at 01:58 PM ---------- Previous post was at 01:08 PM ----------

hmm.. Im having a problem..
When I run my code and reach this part:
public void rowValues() {

        List<String> rows = new ArrayList<String>();

        for(int i=1;i<=matrixSize;i++) {

            System.out.println("Please enter row " + i + ": ");

            System.out.print("> ");

            rows.add(reader.nextLine());

        }

        System.out.println("Done.");

        startProgram();

    }

It for some reason prints out:
Please enter row 1: 

> 

Please enter row 2: 

> 

Not sure what is causing it, but as you can see, it jumps directly to row 2, and sets input for row 1 to null <,<

#9
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Is "reader" a BufferedReader or a Scanner?

#10
qutazs

qutazs

    Newbie

  • Members
  • PipPip
  • 23 posts
Wierd!!

I changed a another methods way of handling the input from nextInt to nextLine and now it works 0o

---------- Post added at 02:01 PM ---------- Previous post was at 02:00 PM ----------

Scanner :) thats what we've learned so far.

#11
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Aye, that's normal scanner behaviour ;)
If you type this in for example:

Quote

24 [ENTER]
nextInt() will read the "24" but not the "[ENTER]", that "[ENTER]" is still waiting in the buffer to be read.
So as soon as you do nextLine(), the "[Enter]" gets read, nextLine() completes, without waiting for actual new user input and returns.
And as you may have assumed, nextLine() DOES get rid of any "\n", "\r" left behind.

You may think that's stupid behaviour of that Scanner, but the Scanner is not used only for user input. Your row lines for example can be read by teh Scanner:
String row = "1 2 3 4";
Scanner scanner = new Scanner(row).useDelimiter(" ");
for(int i=0 ; scanner.hasNextInt() ; i++){
   matrix[0][i] = scanner.nextInt();
}
...This actually looks cooler than the Integer.parseInt :D Should've come up with that before.

(Didn't really write the code in an IDE or ran it, I guess it's correct tho)

#12
qutazs

qutazs

    Newbie

  • Members
  • PipPip
  • 23 posts
lol.. I'm wishing the day will come where I can do that off the bat.. I will try it out :) after i've gone and bought some energy drinks :P

---------- Post added at 03:15 PM ---------- Previous post was at 02:48 PM ----------

    public void rowValues() {

        for(int i=0; i<=matrixSize; i++) {

            System.out.println("Please enter row " + (i+1) + ": ");

            System.out.print("> ");

            String row = reader.nextLine();

            Scanner scanner = new Scanner(row).useDelimiter(" ");

            for(int j=0 ; scanner.hasNextInt() ; j++){

                matrixRows[0][j] = scanner.nextInt();

            }

        }

returns:
Please enter row 1: 

> 1 2 3

Exception in thread "main" java.lang.NullPointerException

	at Program.rowValues(Program.java:84)

	at Program.manual(Program.java:59)

	at Program.define(Program.java:45)

	at Program.define(Program.java:51)

	at Program.startProgram(Program.java:26)

	at Program.main(Program.java:145)

Java Result: 1

Can you see the problem?? Did I mess something up? I didn't even know you could do what you wrote :P (so might be me who did something :D

---------- Post added at 03:18 PM ---------- Previous post was at 03:15 PM ----------

Might have fixed it now.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users