10 Replies - 167 Views - Last Post: 07 May 2012 - 09:27 AM Rate Topic: -----

#1 BrendanH  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 159
  • Joined: 05-May 12

variable errors

Posted 07 May 2012 - 07:43 AM

i have 2 errors saying this: vailable vowels might not have been initialized,
vailable consonants might not have been initialized:

im not sure how to solve this as im new in java and have not seen this error message before
here is the program:

 public class Instructor
{
public static void main (String[] args)
{
}

public void congratdulateStudent(char grade)
{

switch(grade) {


case 'a': case 'b': case 'c': case 'd': case 'e':
int vowels = vowels + 1;
break;

default:
int consonants = consonants + 1;

char grade1 = 'e';

switch(grade1) {

case 'a':
System.out.println("Excellent");
break;

case 'b':
System.out.println("Nice job");
break;

case 'c':
System.out.println("Not bad");
break;

case 'd':
System.out.println("almost!" +"See you next semester");
break;

case 'e':
System.out.println("See you next semester");
break;
}
}
}
}


Is This A Good Question/Topic? 0
  • +

Replies To: variable errors

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5578
  • View blog
  • Posts: 9,010
  • Joined: 19-March 11

Re: variable errors

Posted 07 May 2012 - 07:51 AM

       
      int vowels = vowels + 1;




What could this possibly mean?
Was This Post Helpful? 0
  • +
  • -

#3 BrendanH  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 159
  • Joined: 05-May 12

Re: variable errors

Posted 07 May 2012 - 08:47 AM

the number of vowels there are supposed to be?
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5578
  • View blog
  • Posts: 9,010
  • Joined: 19-March 11

Re: variable errors

Posted 07 May 2012 - 08:53 AM

No, I mean that statement.

The variable "vowels" didn't exist until just now, so what could it mean to increment it?

It's difficult to make out what you want this to do. You're talking about vowels and consonants at one point, and grades at another point. Can you explain what you want this method to accomplish?

When I call congratdulate('n'), what am I expecting to happen?

(I'm not going to chide you about your spelling, except to say that you should really be precise with these things, or you make life difficult for yourself. How are you going to remember which misspelling you used?)
Was This Post Helpful? 0
  • +
  • -

#5 BrendanH  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 159
  • Joined: 05-May 12

Re: variable errors

Posted 07 May 2012 - 09:00 AM

i was told to write the following program:
Add a public method called congratulateStudent() that takes in a single parameter of type char.
This character will represent the student’s grade.
4.
Within the congratulateStudent() method, add a switch statement that switches on the student’s grade.
The switch statement should do the following:

If the student received an ‘A’, print out “Excellent”.

If the student received a ‘B’, print out “Nice job”.

For a ‘C’, print out “Not bad”.

For a ‘D’, print out “Almost!” and “See you next semester”

For an ‘F’, print out “See you next semester”

If the character is none of the above, display an error message.
5.
Save and compile the Instructor class.

All i have to go on to complete this program is the book given to me as im learning from home, also this is a 2 part program.
Was This Post Helpful? 0
  • +
  • -

#6 BrendanH  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 159
  • Joined: 05-May 12

Re: variable errors

Posted 07 May 2012 - 09:06 AM

i finaly corrected errors after removing the vowel and consonant variables as that didint make any sence to me either, but i removed them and remove switch (grade1) as i had switch (grade) already and it now compiles fine with no more errors!!

Thanks for the help!
Will try work on my spelling as i know i can be crap at it sometimes! :)
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5578
  • View blog
  • Posts: 9,010
  • Joined: 19-March 11

Re: variable errors

Posted 07 May 2012 - 09:08 AM

Okay, sounds reasonable. So what's with the vowels and consonants?
Was This Post Helpful? 0
  • +
  • -

#8 BrendanH  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 159
  • Joined: 05-May 12

Re: variable errors

Posted 07 May 2012 - 09:11 AM

View Postjon.kiparsky, on 07 May 2012 - 09:08 AM, said:

Okay, sounds reasonable. So what's with the vowels and consonants?

Was This Post Helpful? 0
  • +
  • -

#9 BrendanH  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 159
  • Joined: 05-May 12

Re: variable errors

Posted 07 May 2012 - 09:16 AM

well im just learning about switch statements and we only did one practice example using numbers in test scores, so when they ask me to do this assignment which used letters instead i had no idea what to do so i used an example in the book but clearly i see now it was all wrong!

But at least now i know how to do it if i need to do it again sometime!
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5578
  • View blog
  • Posts: 9,010
  • Joined: 19-March 11

Re: variable errors

Posted 07 May 2012 - 09:17 AM

View PostBrendanH, on 07 May 2012 - 11:06 AM, said:

i finaly corrected errors after removing the vowel and consonant variables as that didint make any sence to me either, but i removed them and remove switch (grade1) as i had switch (grade) already and it now compiles fine with no more errors!!

Thanks for the help!


Glad to help.

Quote

Will try work on my spelling as i know i can be crap at it sometimes! :)


Attention to detail in this area is a habit worth developing.
Was This Post Helpful? 1
  • +
  • -

#11 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5578
  • View blog
  • Posts: 9,010
  • Joined: 19-March 11

Re: variable errors

Posted 07 May 2012 - 09:27 AM

Quote

well im just learning about switch statements and we only did one practice example using numbers in test scores, so when they ask me to do this assignment which used letters instead i had no idea what to do so i used an example in the book but clearly i see now it was all wrong!

But at least now i know how to do it if i need to do it again sometime!



The main problem with your vowels and consonants stuff - apart from it shouldn't have been included in that method - was that you should have initialized the variable outside of the scope in which you used it.

Here's a schematic sort of example

private int countVowels (String s)
{
  //  initialize counter variable here
  int vowels = 0

  // loop over String: 
  for (char c : s.toCharArray()) {  // I prefer the enhanced for, so get the underlying char array
  switch c
    case a:
    case e:
    case i:
    case o:
    case u:     vowels++; break;
    default:    break; // do nothing
  }
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1