beginner - issues with System.out.println()

creating a class for a directed graph

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1986 Views - Last Post: 09 January 2010 - 02:35 PM Rate Topic: -----

#1 brucezepplin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 11-November 09

beginner - issues with System.out.println()

Posted 07 January 2010 - 08:20 AM

Hi, just a quick question - I have a code which creates an event with three parameters ( start time, finish time, duration ). It compiles fine, but I can't get it to print out the desired information. Please take a look:

public class Event {

	public int start = 0;
	public int finish = 0;
	public int duration = 0;
	
	void show (int s,int f, int d) {
		
		start = s;
		finish= f;
		duration = d;
	
	}

public static void main(String[] args) {

	Event A = new Event();
	int a = Integer.parseInt(args[0]);
	int b = Integer.parseInt(args[1]);
	int c = Integer.parseInt(args[2]);
	A.show(a,b,c);
	System.out.println(A);
		
	
}
}


And here is the compiler's message:

[email protected]:~/University$ javac Event.java
[email protected]:~/University$ java Event
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Event.main(Event.java:19)
[email protected]:~/University$ java Event 1 2 3
[email protected]


I don't know why it simply isn't returning 1 2 3.

Any help would be much appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: beginner - issues with System.out.println()

#2 japanir   User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: beginner - issues with System.out.println()

Posted 07 January 2010 - 08:36 AM

one problem i noticed is the way you print your object.
A is a new class designed by you.
what is the way of representing its object using the Suystem.out methods?
for that you must override the toString() method lang.Object.
This is the reason it prints: [email protected]!

this is just a note.. however this is not what causing the error.
what is the way you insert the arguments?
make sure you dont add any chars except for 123!
Was This Post Helpful? 0
  • +
  • -

#3 anonymouscodder   User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: beginner - issues with System.out.println()

Posted 07 January 2010 - 10:11 AM

Quote

[email protected]:~/University$ java Event
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Event.main(Event.java:19)

If you pass no argument, when the execution reaches int a = Integer.parseInt(args[0]); you will get a runtime exception (ArrayIndexOutOfBoundsException).

By calling System.out.println(A);, you will calling PrintStream.println(A), which will call PrintStream.print(A), which will call String.valueOf(A), which will call A.toString().

And as you can see here: http://java.sun.com/...html#toString(), the output it's gonna be getClass().getName() + '@' + Integer.toHexString(hashCode()).

So when you called [email protected]:~/University$ java Event 1 2 3, the output was [email protected].
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: beginner - issues with System.out.println()

Posted 07 January 2010 - 01:02 PM

Unless you override the toString method in your code.
class MyClass {
	....
	@Override
	public String toString() {
		String returnString = whatever your string is, assemble it here.
		return returnString;
	}
}


Was This Post Helpful? 0
  • +
  • -

#5 brucezepplin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 11-November 09

Re: beginner - issues with System.out.println()

Posted 07 January 2010 - 01:24 PM

Hmm I see, so when I create my own class and implement it to output numbers using System.out.println, the compiler just gives me the has value equivalent of the integers put in. Is there a way to get round this though?
Was This Post Helpful? 0
  • +
  • -

#6 brucezepplin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 11-November 09

Re: beginner - issues with System.out.println()

Posted 07 January 2010 - 01:34 PM

it's ok guys - problem solved! I was being a bit of a div and should of done " System.out.println(A.start) " etc etc.....

Thanks for the help!
Was This Post Helpful? 0
  • +
  • -

#7 brucezepplin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 11-November 09

Re: beginner - issues with System.out.println()

Posted 09 January 2010 - 08:31 AM

Ok, I have improved my code and modified it to allow the user to enter information about events, but I now want to store these events into either an ArrayList, or an AdjacencyMatrix. The code compiles, but I'm still getting this nasty hash code business instead of a nice list.....

Event event = new Event();
	BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
	int a = Integer.parseInt(br.readLine());
	int b = Integer.parseInt(br.readLine());
	int c = Integer.parseInt(br.readLine());
	int d = c - b;
	event.show(a,b,c,d);
	
	ArrayList<Integer> matrix = new ArrayList<Integer>();
	
	matrix.add(event.name);
	matrix.add(event.start);
	matrix.add(event.finish);
	matrix.add(event.duration);
	
	System.out.println(matrix.toArray());


The compiler throws back:

[email protected]:~/University$ java Event
This program allows you to contruct a directed graph by inputting events
To add a new event, enter the event name, start time and end time
1
9
44
[Ljava.lang.Object;@1a8c4e7


I realize this has been discussed already, and I managed to sort the problem out when using simply Integer.parseInt, but how do I store those numbers in a list or matrix?

Thanks again!
Was This Post Helpful? 0
  • +
  • -

#8 erik.price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: beginner - issues with System.out.println()

Posted 09 January 2010 - 08:38 AM

If your trying to print out all the elements of the array, you would need to use a for or a foreach loop to iterate through it and print each element.

What you're doing now is trying to print out the entire array at once, which doesn't work how you would expect it to.
Was This Post Helpful? 1
  • +
  • -

#9 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: beginner - issues with System.out.println()

Posted 09 January 2010 - 08:42 AM

Yep. This about summarizes what's happening and why: http://java.sun.com/...html#toString()

Like Erik.Price said, just loop through it and print each one.
Was This Post Helpful? 0
  • +
  • -

#10 brucezepplin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 11-November 09

Re: beginner - issues with System.out.println()

Posted 09 January 2010 - 09:33 AM

ok so I now have the following :

public class Event {
	
	public int name = 0;
	public int start = 0;
	public int finish = 0;
	public int duration = 0;
	public static ArrayList<Integer> matrix = new ArrayList<Integer>();
	
	void show (int n,int s,int f, int d) {
		
		name = n;
		start = s;
		finish= f;
		duration = d;
		
		}
	
public static void fillMatrix() {
	   for (int i = 0; i < 10; i++) {
		 matrix.add(event.name);
		 matrix.add(event.start);
		 matrix.add(event.finish);
		 matrix.add(event.duration);
	   }
	 }

public static void outputMatrix() {
	   for (int i = 0; i < matrix.size(); i++) {
		 System.out.printf("%d squared is %d.\n",
		   i, matrix.get(i));
	   }
	 }

public static void main(String[] args) throws IOException {

System.out.println(" Thank you, your summary of events are as follows: ");
	
	fillMatrix();
	outputMatrix();

}
}





and I now get the following error:

Event.java:24: cannot find symbol
symbol : variable event
location: class Event
matrix.add(event.name);
^
Event.java:25: cannot find symbol
symbol : variable event
location: class Event
matrix.add(event.start);
^
Event.java:26: cannot find symbol
symbol : variable event
location: class Event
matrix.add(event.finish);
^
Event.java:27: cannot find symbol
symbol : variable event
location: class Event
matrix.add(event.duration);

why does it not pick up event?

awesome help so far by the way, I am enjoying the java learning curve!
Was This Post Helpful? 0
  • +
  • -

#11 erik.price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: beginner - issues with System.out.println()

Posted 09 January 2010 - 09:35 AM

You never declare a variable, event in your Event class.

I think you would be looking for self.finish, self.start, self.name, etc. anyway :)
Was This Post Helpful? 1
  • +
  • -

#12 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: beginner - issues with System.out.println()

Posted 09 January 2010 - 09:39 AM

View Posterik.price, on 9 Jan, 2010 - 10:35 AM, said:

You never declare a variable, event in your Event class.

I think you would be looking for self.finish, self.start, self.name, etc. anyway :)


lol stuck in Ruby?

By the way, you might want to use this.start()

		 matrix.add(this.name);
		 matrix.add(this.start);
		 matrix.add(this.finish);
		 matrix.add(this.duration);



But I'm not even sure that that is even what you want to do...
Was This Post Helpful? 1
  • +
  • -

#13 erik.price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: beginner - issues with System.out.println()

Posted 09 January 2010 - 09:41 AM

View PostDogstopper, on 9 Jan, 2010 - 11:39 AM, said:

lol stuck in Ruby?

By the way, you might want to use this.start()


POOF! There goes my expert badge.

:P thanks, you're right
Was This Post Helpful? 0
  • +
  • -

#14 brucezepplin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 11-November 09

Re: beginner - issues with System.out.println()

Posted 09 January 2010 - 11:50 AM

Well I allow the user to input integers in the main method using BufferedReader and then allow them to put in an event name (integer ) and a start and end time. I want to store all of this info into an ArrayList or AdjacencyMatrix and allow dependencies between events ( a directed graph ).

So I have altered to:

public static void fillMatrix() {

	   for (int i = 0; i < 10; i++) {
		 matrix.add(this.name);
		 matrix.add(this.start);
		 matrix.add(this.finish);
		 matrix.add(this.duration);
	   }
	 }


And now I get:

[email protected]:~/University$ javac Event.java
Event.java:24: non-static variable name cannot be referenced from a static context
matrix.add(Event.name);
^
Event.java:25: non-static variable start cannot be referenced from a static context
matrix.add(Event.start);
^
Event.java:26: non-static variable finish cannot be referenced from a static context
matrix.add(Event.finish);
^
Event.java:27: non-static variable duration cannot be referenced from a static context
matrix.add(Event.duration);

doh!
Was This Post Helpful? 0
  • +
  • -

#15 brucezepplin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 11-November 09

Re: beginner - issues with System.out.println()

Posted 09 January 2010 - 12:20 PM

Its' probably best to give you my whole code to give you a better idea:

import java.io.*;
import java.util.ArrayList;


public class Event {
	
	public static int name = 0;
	public static int start = 0;
	public static int finish = 0;
	public static int duration = 0;
	
	public static ArrayList<Integer> matrix = new ArrayList<Integer>();
	
	void show (int n,int s,int f, int d) {
		
		this.name = name;
		this.start = start;
		this.finish= finish;
		this.duration = duration;
		
		}
	
public static void fillMatrix() {

	   for (int i = 0; i < 10; i++) {
		 matrix.add(this.name);
		 matrix.add(this.start);
		 matrix.add(this.finish);
		 matrix.add(this.duration);
	   }
	 }

public static void outputMatrix() {
	   for (int i = 0; i < matrix.size(); i++) {
		 System.out.printf("%d squared is %d.\n",
		   i, matrix.get(i));
	   }
	 }
	
	
	
public static void main(String[] args) throws IOException {
	
	System.out.println( " This program allows you to contruct a directed graph by inputting events " );
	System.out.println( " To add a new event, enter the event name, start time and end time " );
	
	while (true) {
	
	Event event = new Event();
	BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
	int a = Integer.parseInt(br.readLine());
	int b = Integer.parseInt(br.readLine());
	int c = Integer.parseInt(br.readLine());
	int d = c - b;
	event.show(a,b,c,d);
	
	System.out.println(" Event " + event.name + " (start,finish,duration) : (" + event.start + "," + event.finish + "," + event.duration + ")" );
	
	System.out.println("Enter 1 to add another event, or 0 to stop adding events");
	
	int decision = Integer.parseInt(br.readLine());
	
	if (decision == 0) {
	
	System.out.println(" Thank you, your summary of events are as follows: ");
	
	fillMatrix();
	outputMatrix();
	
	break;
	
	} 
	
	}// end while
	
	}// end main
	
} // end class Event

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2