Do/While loop confusion

Can anyone see why my do/while loop does not work?

Page 1 of 1

5 Replies - 2326 Views - Last Post: 15 April 2008 - 07:39 PM Rate Topic: -----

#1 mittens  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-September 07

Do/While loop confusion

Post icon  Posted 15 April 2008 - 02:14 AM

Hi all,

I am VERY VERY new to Java (started Monday last)... and also have the dubious pleasure (ha ha) of being an external student...

My problem is this: in the attached I can't understand why the do/while loop wont work.

I get the error message:
m7.java:95: variable response might not have been initialized
}while(response != 'q');
^

The error points to the 'r' in response.

If I take out the do/ while , the program works but not correctly( it is sposed to loop!)

Can anyone help me??

Also, apologies if I didn't post correctly, still getting used to all this.

thank you
mittens

PS Never study this stuff externally!!!

import B102.*;

class m7
{
 static void printMenu()
 {
  Screen.out.println("You have the following options:");
  Screen.out.println("Enter A to print name.");
  Screen.out.println("Enter B to print Tutorial.");
  Screen.out.println("Enter C for number display.");
  Screen.out.println("Enter D for a random number between 1 and 10 to be displayed.");
  Screen.out.println("Enter Q to quit."); 
 }

 static void printName()
 {
  Screen.out.println("mittens.");
  Screen.out.println("");
 }

 static void printTutorial()
 {
  Screen.out.println("No tutorial time. External student.");
  Screen.out.println("");
 }

 static void NumCount()
 {
  int num, i;

  Screen.out.print("Enter an integer number between 1 and 50:");
  num = Keybd.in.readInt();

  if((num <= 0) || (num >= 51))
   Screen.out.print("Invalid Range. It must be between 1 and 50!");

  else

   for(i = 0; i <= num; i++)
   Screen.out.print(" " + i);
   Screen.out.println(" ");
   Screen.out.println(" ");
 }

 static void RandomNumber()
 {
  int guess;
  final int GUESS_MAX = 10;

  guess = (int)(Math.random() * GUESS_MAX) + 1;
  Screen.out.println("The random number is " + guess);
  Screen.out.println(" ");  
 }

 static void SortResponse()
 {
  char response;

  response = Keybd.in.readChar();

  switch(response)
  {
   case 'a':
	 printName();
	 return;

   case 'b':
	 printTutorial();
	 return;

   case 'c':
	 NumCount();
	 return;

   case 'd':
	 RandomNumber();
	 return;

   case 'q':
	  Screen.out.println("OK. Bye!");
	  break;

   default:
	 Screen.out.println("You haven't selected a valid option. Try again.");
	 Screen.out.println(" ");
	 return;
  }
 }

 public static void main(String[]args)
 {
   char response;

   do
   {
   printMenu();
   SortResponse();
   }while(response != 'q');

   if(response == 'q')
	Screen.out.println("OK. Bye!");
 }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Do/While loop confusion

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3124
  • View blog
  • Posts: 19,168
  • Joined: 14-September 07

Re: Do/While loop confusion

Posted 15 April 2008 - 04:18 AM

That's happening because char response is not initialized to any value inside main()

There are several ways to correct this. You could take keyboard input inside of main and assign it to response. You could also pass parameters from your other functions into main so you can use char response.
Was This Post Helpful? 0
  • +
  • -

#3 mittens  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-September 07

Re: Do/While loop confusion

Posted 15 April 2008 - 04:32 AM

Hi !

Thanks for replying so quick.

I have tried getting user input inside main, but get the same error.
Also tried passing parameters from my SortResponse and other ways(a method for exitProgram).... but all ended up with the same error.

Read through some stuff here, and even tried to get some to work.. :(

Any other ideas?

mittens
Was This Post Helpful? 0
  • +
  • -

#4 kr gandharva  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-April 08

Re: Do/While loop confusion

Posted 15 April 2008 - 05:08 AM

View Postmittens, on 15 Apr, 2008 - 04:32 AM, said:

Hi !

Thanks for replying so quick.

I have tried getting user input inside main, but get the same error.
Also tried passing parameters from my SortResponse and other ways(a method for exitProgram).... but all ended up with the same error.

Read through some stuff here, and even tried to get some to work.. :(

Any other ideas?

mittens



I think the complete problem lies here

public static void main(String[]args)
{
char response;

do
{
printMenu();
SortResponse();
}while(response != 'q');

if(response == 'q')
Screen.out.println("OK. Bye!");
}
}

response here is the variable of your main block and in no way the response is getting any short of data value (character) as input in any way...

So try to look into that..

or best part would be that you take the keyboard input in the main block before calling SortResponse()
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Do/While loop confusion

Posted 15 April 2008 - 07:45 AM

There are better ways of doing that but...

I would not doing that way but this is a quick way to fix your problem (please do not put my name in your comments as the guy having programmed that)


 static char SortResponse()
 {
  char response;

  response = Keybd.in.readChar();

  switch(response)
  {
   case 'a':
	 printName();
	 break;

   case 'b':
	 printTutorial();
	 break;

   case 'c':
	 NumCount();
	 break;

   case 'd':
	 RandomNumber();
	 break;

   case 'q':
	  Screen.out.println("OK. Bye!");
	  break;

   default:
	 Screen.out.println("You haven't selected a valid option. Try again.");
	 Screen.out.println(" ");
  }
  return response;
 }

 public static void main(String[]args)
 {
   char response;

   do
   {
   printMenu();
   response = SortResponse();
   }while(response != 'q');

 
	Screen.out.println("OK. Bye!");
 }
}


Was This Post Helpful? 0
  • +
  • -

#6 mittens  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-September 07

Re: Do/While loop confusion

Posted 15 April 2008 - 07:39 PM

Thank you!! :) Your help is fantastic. Works like a charm.

Being external has its disadvantages, (no-one to really bounce ideas or mistakes off) but I am slowly getting there.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1