4 Replies - 262 Views - Last Post: 01 February 2013 - 09:17 AM Rate Topic: -----

#1 Eggy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 15-November 12

How to pass variable(s)?

Posted 01 February 2013 - 08:08 AM

private static void colReader(String s) throws IOException 
{
	 FileReader readhandle = new FileReader("C:\\****\\****");
	    BufferedReader br = new BufferedReader(readhandle);
	    String line = null;
	    while((line = br.readLine()) != null)
	    {
	      colProduce(line, 0); 
	    }

	    br.close();
	    readhandle.close();
}

private static void colScanner(String s)
{
	char letter = 0;
	
	String fullWord;
	Scanner kb = new Scanner(System.in);
	
	System.out.print("Enter your word ");
	
	fullWord = kb.nextLine();
	System.out.println(fullWord);
	colProduce(fullWord, letter);
}

private static void colProduce(String text, int j)
{
	
for (int i = 0; i < text.length(); i++)
{
	j = text.charAt(i);
	switch(Character.toUpperCase(j))
	{
		case 'A':
		{
		   Blue()
		}
		break;
        }
}
}



Hey guys, I'm trying to pass the variables line (from colRead method), and the fullWord variable (from colScanner method) to the colProduce method.

I'm kind of stuck because, in the code I've produced ONLY the colReader method works, when I attempt to run the colScanner method from my menu, nothing happens! It just refreshes back to the menu.

I've been stuck on this for a bit now and it's driving me crazy!

Any help please?

This post has been edited by Eggy: 01 February 2013 - 08:11 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to pass variable(s)?

#2 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: How to pass variable(s)?

Posted 01 February 2013 - 08:22 AM

Having one method rely on 2 other methods like this should really be designed in advanced. You are just gonna give yourself a headache over it. :nottalkingtoyou:

There are solutions, but it can usually make your code buggy and hard to follow. (such as using global variables) Your code is already kinda hard to follow, and I'm not quite sure what your methods are trying to accomplish.

Perhaps add comments on what your code is doing, and what the overall method is trying to accomplish then it would be easier to give some advice on how to design around this.
Was This Post Helpful? 0
  • +
  • -

#3 Eggy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 15-November 12

Re: How to pass variable(s)?

Posted 01 February 2013 - 08:43 AM

View PostKinaces, on 01 February 2013 - 08:22 AM, said:

Perhaps add comments on what your code is doing, and what the overall method is trying to accomplish then it would be easier to give some advice on how to design around this.


Hi Kinaces, thank you for your time!

Basically, this is homework, and I've been allocated with a robot which has led lighting. For one of our homework's, we have to make the robot produce colours by taking in a word user input (from keyboard), and to be able to read off a text file. The colScanner method is suppose to read off the keyboard input and produce the colours of the letters, whilst the colReader method is suppose to read off of a text file (the word), and output the colours of the word. The methods colScanner and colReader are selected via a menu option, and they're then suppose to carry pass their variable(s) to the colProduce method which produces the colours of the letters.

Thank you.
Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 851
  • View blog
  • Posts: 2,599
  • Joined: 29-July 11

Re: How to pass variable(s)?

Posted 01 February 2013 - 08:57 AM

So colProduce() is supposed to read the first char of a string and return a color you defined for it, like A = Blue(), C = Green(), etc?

OK, that seems to work.

colScanner() seems as if it would work but I don't see why you've defined that String as an argument (String s). You never do anything with it.

If your code isn't too complicated, why don't you post it all so we can see what's going on?
Was This Post Helpful? 0
  • +
  • -

#5 Eggy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 15-November 12

Re: How to pass variable(s)?

Posted 01 February 2013 - 09:17 AM

View Postfarrell2k, on 01 February 2013 - 08:57 AM, said:

If your code isn't too complicated, why don't you post it all so we can see what's going on?


I don't want to upload all my code online because I'm scared of plagiarism! but the
   String s



is the interactive menu calling the method from the user i.e.
if (s.equals ("Colour reader")) colReader(s);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1