Store X and Y coordinates

I welcome you to this site and I need help with the program that I am working on. im problem is that I cannot save a string and two integers (as coordinates). I looked at another code, but can't see how the values ​​are stored. Below is the ive code. the code seems fine, but when you try to save values ​​that I cannot put, multiply the integers. thank you for your time

import java.util.HashMap; public class map { class Coords { int x; int y; public boolean equals(Object o) { Coords c = (Coords) o; return cx == x && cy == y; } public Coords(int x, int y) { super(); this.x = x; this.y = y; } public int hashCode() { return new Integer(x + "0" + y); } } public static void main(String args[]) { HashMap<Coords, Character> map = new HashMap<Coords, Character>(); map.put(new coords(65, 72), "Dan"); } } 
+4
source share
5 answers

There seem to be a few problems:

  • "Dan" is a String , not a Character
  • In Java, the situation is important ( new coords(65,72) should be new coords(65,72) )
  • Encoders must be static in order to be instantiated without reference to an instance of the surrounding card class.

This should work:

 static class Coords { ... } Map<Coords, String> map = new HashMap<Coords, String>(); map.put(new Coords(65, 72), "Dan"); 

ps: although you are allowed to specify a local map variable inside the class map , it is not a good idea for such a name clash. In Java, classes usually start in uppercase, so you can rename your class map. But it happens that Map is a standard Java class. So call your Main or Test class or something else .; -)

+6
source

In java, the Point Point class is called.

http://docs.oracle.com/javase/7/docs/api/java/awt/Point.html

A point representing a location in the coordinate space (x, y) specified with complete accuracy.

You can see an example at this link: http://www.java2s.com/Tutorial/Java/0261__2D-Graphics/Pointclass.htm

 import java.awt.Point; class PointSetter { public static void main(String[] arguments) { Point location = new Point(4, 13); System.out.println("Starting location:"); System.out.println("X equals " + location.x); System.out.println("Y equals " + location.y); System.out.println("\nMoving to (7, 6)"); location.x = 7; location.y = 6; System.out.println("\nEnding location:"); System.out.println("X equals " + location.x); System.out.println("Y equals " + location.y); } } 

Hope this helps you!

+9
source

Adding to @assylias

The code:

 public class XYTest { static class Coords { int x; int y; public boolean equals(Object o) { Coords c = (Coords) o; return cx == x && cy == y; } public Coords(int x, int y) { super(); this.x = x; this.y = y; } public int hashCode() { return new Integer(x + "0" + y); } } public static void main(String args[]) { HashMap<Coords, String> map = new HashMap<Coords, String>(); map.put(new Coords(65, 72), "Dan"); map.put(new Coords(68, 78), "Amn"); map.put(new Coords(675, 89), "Ann"); System.out.println(map.size()); } } 
+1
source
 package Lecture3; import java.util.Scanner; public class lecture9 { private int nInleste; public lecture9() {/* * tabell/ // T/*chapter 6 in the books. **/ } public static void main(String[] args) { Scanner inn = new Scanner(System.in); int nInleste = 3; double[] tall = new double[nInleste]; double sum = 0; for (int i = 0; i < nInleste; i++) { System.out.println("Leste en tall!"); tall[i] = inn.nextDouble(); sum += tall[i]; } System.out.println(sum); double snitt = nInleste / nInleste; System.out.println("Gjennomsnittsverdien:" + snitt); for (int i = 0; i < nInleste; i++) { double aavik = tall[i] - snitt; int avvivk = 0; System.out.println(i + 1 + " Tal sitt avvik fra gjennomsnittet " + avvivk); } }/* end of the main methods */ } 
+1
source

if you have problems with the code, you can try this, simple code to store the string and two int values ​​in map

 class MyCoord{ private int X; private int Y; public MyCoord() { this(0,0); } public MyCoord(int X, int Y) { this.X = X; this.Y = Y; } public int getX() { return X; } public int getY() { return Y; } public void setX(int X) { this.X = X; } public void setY(int Y) { this.Y = Y; } } 

// the main class begins

 public class PointDemo{ public static void main(String[] args) { Map <String,MyCoord> multiplePoints=new HashMap<String, MyCoord>(); multiplePoints.put("point1", new MyCoord(10, 20)); multiplePoints.put("point2", new MyCoord(100, 2000)); MyCoord coord=multiplePoints.get("point1"); System.out.println(coord.getX() +" : "+coord.getY()); } } 
0
source

All Articles