4 Replies - 673 Views - Last Post: 29 November 2008 - 08:37 AM Rate Topic: -----

#1 mcweaver  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-November 08

Please help! Error message whilst compiling (; expected)

Posted 29 November 2008 - 07:16 AM

Basically when I compile this I keep getting the same errors:

Cwk2Problem1.java:19: ';' expected
int createlists(String[] args)
^
Cwk2Problem1.java:19: ';' expected
int createlists(String[] args)
^
What my code is doing is reading in a .gr file (graph) and building an adjacency list representation of the corresponding graph.

Please help! Thank you


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

class Cwk2Problem1
{
	public static void main(String[] args)
	{
		//check only give one argument(filename), check its a .gr file (check last 3 letters of string are .gr), if thats fine you do this:..... or else error. return number nodes/edges
		Cwk2Problem1 program = new Cwk2Problem1();
		int result = program.createlists(args);
		System.out.println("result = %d", result);
	   //calculates the length of the input string given
		int len = args[0].length();
		//takes the substring of the given string
		//takes the last 3 chars of the string
		String ext = args[0].substring(len-3, len);
		if ((args.length == 1) & (ext.equals(".gr")))
		{
			int createlists(String[] args)
			{
				int nodes = 0;
				int edges = 0;
				DataInputStream dis = null;
				try
				{
					String record;
					File f = new File(args[0]);
					FileInputStream fis = new FileInputStream(f);
					BufferedInputStream bis = new BufferedInputStream(fis);
					dis = new DataInputStream(bis);
					while ((record = dis.readLine()) != null)
					{
						String[] data = record.split(" ");
						if ( data[0].equals("p"))
						{
							nodes = Integer.parseInt(data[2])+1;
							edges = Integer.parseInt(data[3]);
							break;
						}
					}
					ArrayList[] gertrude = new ArrayList[nodes];  
					dis.close();
					f = new File(args[0]);
					fis = new FileInputStream(f);
					bis = new BufferedInputStream(fis);
					dis = new DataInputStream(bis);
					while ((record = dis.readLine()) != null)
					{
						String[] data = record.split(" ");
						if (data[0].equals("a"))
						{
							int position = Integer.parseInt(data[1]);
							int a = Integer.parseInt(data[2]);
							int b = Integer.parseInt(data[3]);
							Apples pineapple = new Apples(a, b);
							gertrude[position].add(pineapple);
						}	 
					}
				}
				catch (IOException e)
				{
					System.out.println("IOException error!");
				}
				return (int)(nodes/edges);
			}
		}
		else
		{
			System.err.println("Wrong file");
		}
	}
}

class Apples
{
	int a, b;
	Apples(int c, int d)
	{
		a = c;
		b = d;
	}
	int geta()
	{
		return a;
	}
	int getb()
	{
		return b;
	}
	void seta(int orange)
	{
		a=orange;
	}
	void setb(int banana)
	{
		b=banana;
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Please help! Error message whilst compiling (; expected)

#2 windra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-November 08

Re: Please help! Error message whilst compiling (; expected)

Posted 29 November 2008 - 07:58 AM

you cannot declare a function inside an if statement which is your problem.

you need to move the function int createlists(String[] args){....} outside of the if statement and main and call it from your if statement like createlists(yourstringhere);
Was This Post Helpful? 0
  • +
  • -

#3 mcweaver  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-November 08

Re: Please help! Error message whilst compiling (; expected)

Posted 29 November 2008 - 08:09 AM

View Postwindra, on 29 Nov, 2008 - 06:58 AM, said:

you cannot declare a function inside an if statement which is your problem.

you need to move the function int createlists(String[] args){....} outside of the if statement and main and call it from your if statement like createlists(yourstringhere);


where you put createlists(yourstringhere) I'm not sure what to put instead of yourstringhere.... I'm not that experienced with java! thank you for your help
Was This Post Helpful? 0
  • +
  • -

#4 windra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-November 08

Re: Please help! Error message whilst compiling (; expected)

Posted 29 November 2008 - 08:31 AM

Here you go....
I moved the whole creatlists function outside of the main, it should work now. I also changed your println so that it compiled as it threw an error.

Basically when you do result = createlists(args) you are calling the createlists function,giving it the string args and result equals whatever createlists() returns. You had put the function INSIDE the main, when it needs to go outside which was your problem.

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

class Cwk2Problem1
{
	public static void main(String[] args)
	{
		//check only give one argument(filename), check its a .gr file (check last 3 letters of string are .gr), if thats fine you do this:..... or else error. return number nodes/edges
		Cwk2Problem1 program = new Cwk2Problem1();
		int result = program.createlists(args);
		System.out.println("result = %d" + result); //+ concatinates the string with result which is an int so it can print it as a string
	}
	int createlists(String[] args) {
	//calculates the length of the input string given
	int len = args[0].length();
	//takes the substring of the given string
	//takes the last 3 chars of the string
	String ext = args[0].substring(len-3, len);
	if ((args.length == 1) & (ext.equals(".gr")))
	{
		int nodes = 0;
		int edges = 0;
		DataInputStream dis = null;
		try
		{
			String record;
			File f = new File(args[0]);
			FileInputStream fis = new FileInputStream(f);
			BufferedInputStream bis = new BufferedInputStream(fis);
			dis = new DataInputStream(bis);
			while ((record = dis.readLine()) != null)
			{
		   		 String[] data = record.split(" ");
				 if ( data[0].equals("p"))
				 {
					 nodes = Integer.parseInt(data[2])+1;
					 edges = Integer.parseInt(data[3]);
					 break;
				 }
			}
			ArrayList[] gertrude = new ArrayList[nodes];  
			dis.close();
			f = new File(args[0]);
			fis = new FileInputStream(f);
			bis = new BufferedInputStream(fis);
			dis = new DataInputStream(bis);
			while ((record = dis.readLine()) != null)
			{
				String[] data = record.split(" ");
				if (data[0].equals("a"))
				{
					int position = Integer.parseInt(data[1]);
					int a = Integer.parseInt(data[2]);
					int b = Integer.parseInt(data[3]);
					Apples pineapple = new Apples(a, b);
					gertrude[position].add(pineapple);
				}	 
			}
	 }
	 catch (IOException e)
	 {
	 	System.out.println("IOException error!");
	 }
		return (int)(nodes/edges);
  }
		else
		{
			System.err.println("Wrong file");
			return 0;  //MUST have a return here or the function never returns to the main
		}
	}
}
	

class Apples
{
	int a, b;
	Apples(int c, int d)
	{
		a = c;
		b = d;
	}
	int geta()
	{
		return a;
	}
	int getb()
	{
		return b;
	}
	void seta(int orange)
	{
		a=orange;
	}
	void setb(int banana)
	{
		b=banana;
	}

  }


This post has been edited by windra: 29 November 2008 - 08:35 AM

Was This Post Helpful? 0
  • +
  • -

#5 mcweaver  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-November 08

Re: Please help! Error message whilst compiling (; expected)

Posted 29 November 2008 - 08:37 AM

thank you so much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1