12 Replies - 640 Views - Last Post: 15 April 2011 - 10:01 AM Rate Topic: -----

#1 Borron555  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 02-March 11

switch statement help

Posted 15 April 2011 - 07:27 AM

I'm having problem with a switch statement. I need to have a switch statement take a string, such as a name, I'll use my name.

Matt Piela.

It has to go through the string, and check each individulal letter for the numerology behind it based off this table:

A, J, S = 1
B, K, T = 2
C, L, U = 3
D, M, V = 4
E, N, W = 5
F, O, X = 6
G, P, Y = 7
H, Q, Z = 8
I, R, = 9

I have to have two methods, a getPersonality(), which goes through the string and only takes and adds the numbers of the consonants in the string, so for my name it would take MTT PL, find the corresponding numbers, and add them together in an integer, next i need to have a getSoul() method, that only takes the vowels, finds the numbers, and adds them tegether in an integer. I am utterly lost in how to do this,

this is what i have so far but it gives about 11 errors, i'm not sure if i'm on the right track.

	        int iName;
		switch(sName.toLowerCase())
		case sName.charAt().equals("A","J","S"): iName += 1 break;
		case sName.charAt().equals("B","K","T"): iName += 2 break;
		case sName.charAt().equals("C","L","U"): iName += 3 break;
		case sName.charAt().equals("D","M","V"): iName += 4 break;
		case sName.charAt().equals("E","N","W"): iName += 5 break;
		case sName.charAt().equals("F","O","X"): iName += 6 break;
		case sName.charAt().equals(


This is homework but this is the last thing i need to figure out, i have done everything else. All help will be very very much appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: switch statement help

#2 TFoSSDQ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 123
  • View blog
  • Posts: 253
  • Joined: 09-December 10

Re: switch statement help

Posted 15 April 2011 - 07:32 AM

How a switch statement works is it takes in a variable and then each case checks it against a value. If you input a String then you have to check it against Strings. To check the chars, you would have to put a char in the switch.
Was This Post Helpful? 0
  • +
  • -

#3 Borron555  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 02-March 11

Re: switch statement help

Posted 15 April 2011 - 07:41 AM

View PostTFoSSDQ, on 15 April 2011 - 07:32 AM, said:

How a switch statement works is it takes in a variable and then each case checks it against a value. If you input a String then you have to check it against Strings. To check the chars, you would have to put a char in the switch.



okay, so, how would i be able to check individual parts of the string without using char at? I'm thinking java reads the string as one object and not an array of chars, so if i had cases without a charAt, i would think it would be comparing the string as a whole to the case statement, and not per character.

That is the main question, how would i get the switch to check each individual letter of the string?
Was This Post Helpful? 0
  • +
  • -

#4 sakshamkum  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 19
  • View blog
  • Posts: 232
  • Joined: 09-June 09

Re: switch statement help

Posted 15 April 2011 - 08:02 AM

you can check each character in the String by extracting one character at a time and compare it in the switch
Was This Post Helpful? 0
  • +
  • -

#5 Borron555  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 02-March 11

Re: switch statement help

Posted 15 April 2011 - 08:16 AM

public int getPersonality()
	{
		for(int i = 0; i < sName.length(); i++)
		{
			char cName = sName.charAt(i);

			switch(cName)
			{
			  case cName.equals("A","J","S"): iName += 1; break;
			  case cName.equals("B","K","T"): iName += 2; break;
			  case cName.equals("C","L","U"): iName += 3; break;
			  case cName.equals("D","M","V"): iName += 4; break;
			  case cName.equals("E","N","W"): iName += 5; break;
			  case cName.equals("F","O","X"): iName += 6; break;
		      case cName.equals("G","P","Y"): iName += 7; break;
		      case cName.equals("H","Q","Z"): iName += 8; break;
		      case cName.equals("I","R")    : iName += 9; break;
		      default:                        iName += 0; break;
			}
	    }

		return iName;

	}


Is that close?
Was This Post Helpful? 0
  • +
  • -

#6 TFoSSDQ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 123
  • View blog
  • Posts: 253
  • Joined: 09-December 10

Re: switch statement help

Posted 15 April 2011 - 08:26 AM

cases do not have booleans which it what you're putting there with a condition. You would want something like this:
for(int i = 0; i < sName.length(); i++)
{
    char cName = sName.charAt(i);
    switch(cName)
    {
        case 'A': //do something
        //.
        //.
        //.
        //cycle through all of them which is honestly not the best way to do it as if-else-ifs can handle multiple conditions in one thing but cases can handle only one value
    }
}


Was This Post Helpful? 1
  • +
  • -

#7 Borron555  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 02-March 11

Re: switch statement help

Posted 15 April 2011 - 09:01 AM

Alright i've gotten this so far, it compiles and runs but i get the incorrect number, also the getReduced() method just takes the number, say 22, and adds it together and gets 4, as an example.

I used the example my professor gave, his name, and for this method it should come out to 32, then be reduced to 5, right now, before its even reduced, its coming out to 5, like mine name should come out to 28, then 10, and its coming out to 11 before reduction. What am i missing?

public int getPersonality()
	{
		String sLetter;
		int iName = 0;

		for(int i = 0; i < sName.length(); i++)
		{
			char cName = sName.charAt(i);

			switch(cName)
			{
				case 'J':
				case 'S':
				iName += 1;
				break;

				case 'B':
				case 'K':
				case 'T':
				iName += 2;
				break;

				case 'C':
				case 'L':
				iName += 3;
				break;

				case 'D':
				case 'M':
				case 'V':
				iName += 4;
				break;

				case 'E':
				case 'N':
				case 'W':
				iName += 5;
				break;

				case 'F':
				case 'X':
				iName += 6;
				break;

				case 'G':
				case 'P':
				case 'Y':
				iName += 7;
				break;

				case 'H':
				case 'Q':
				case 'Z':
				iName += 8;
				break;

				case 'R':
				iName += 9;
				break;

				default:
				iName += 0;
				break;
			}
	    }

	    System.out.println(iName);
		return getReduced(iName);
	}

This post has been edited by Borron555: 15 April 2011 - 09:08 AM

Was This Post Helpful? 0
  • +
  • -

#8 sakshamkum  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 19
  • View blog
  • Posts: 232
  • Joined: 09-June 09

Re: switch statement help

Posted 15 April 2011 - 09:08 AM

characters cannot be compared by equals()
use
    for(int i = 0; i < sName.length(); i++)
		{
			char cName = sName.charAt(i);

			switch(cName)
			{
			  case 'A':
                           case 'J':
                           case 'S': iName += 1; break;
			  case 'B':
                           case 'K':
                           case 'T': iName += 2; break;
			  case 'C':
                           case 'L':
                           case 'U': iName += 3; break;
			  case 'D':
                           case 'M':
                           case 'V': iName += 4; break;
			  case 'E':
                           case 'N':
                           case 'W': iName += 5; break;
			  case 'F':
                           case 'O':
                           case 'X': iName += 6; break;
		          case 'G':
                           case 'P':
                           case 'Y': iName += 7; break;
		          case 'H':
                           case 'Q':
                           case 'Z': iName += 8; break;
		          case 'I':
                           case 'R': iName += 9; break;
		          default: iName += 0; break;
			}
	    }

		return iName;
    

Was This Post Helpful? 0
  • +
  • -

#9 Borron555  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 02-March 11

Re: switch statement help

Posted 15 April 2011 - 09:12 AM

Never mind i figured out the problem, its not touppercase or lowercase (which i'm not possitive how to do with a char), but its only taking the capital letters in the string from the case. can you use a || operator in a case?
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2777
  • View blog
  • Posts: 11,750
  • Joined: 20-September 08

Re: switch statement help

Posted 15 April 2011 - 09:17 AM

Use

char cName = sName.charAt(i).toUpperCase();

Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2777
  • View blog
  • Posts: 11,750
  • Joined: 20-September 08

Re: switch statement help

Posted 15 April 2011 - 09:37 AM

Sorry that was wrong. Should have been

char cName = Character.toUpperCase(sName.charAt(i));


but it's easier to do like this:


    public static int getPersonality(String s) {
	int result = 0;
	for(int i = 0;i < s.length();i++) {	
	    char c = Character.toUpperCase(s.charAt(i));
	    result += (c - 'A' + 1) % 9;
	}
	return result;
    }


Was This Post Helpful? 0
  • +
  • -

#12 Borron555  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 02-March 11

Re: switch statement help

Posted 15 April 2011 - 09:40 AM

View Postg00se, on 15 April 2011 - 09:37 AM, said:

Sorry that was wrong. Should have been

char cName = Character.toUpperCase(sName.charAt(i));


but it's easier to do like this:


    public static int getPersonality(String s) {
	int result = 0;
	for(int i = 0;i < s.length();i++) {	
	    char c = Character.toUpperCase(s.charAt(i));
	    result += (c - 'A' + 1) % 9;
	}
	return result;
    }



You can also just do it like this, which i'm not having any problems with:

public int getPersonality()
	{
		int iName = 0;

		for(int i = 0; i < sName.length(); i++)
		{
			char cName = sName.toUpperCase().charAt(i);



because the method is off of the string, its modifying that before initializing it as a char, so no de-referencing occurs. Thanks everyone for all the help, i should have it down now.
Was This Post Helpful? 0
  • +
  • -

#13 Borron555  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 02-March 11

Re: switch statement help

Posted 15 April 2011 - 10:01 AM

Urgh.. i hate when this happens. Okay so now that i have the two getPersonality() and getSoul() methods up and running, i have to have another method, getPowerName(), that adds them together, the problem is, is that the integers in both previous methods are non-class level, but if i try to put them in the parameteres for getPowerName() like this:

public int getPowerName(int iSoul, int iPers) // i changed both iNumbers to reflect their method up top.
	{
		int iPowerName;

		iPowerName = iSoul + iPers;

		return getReduced(iPowerName);
	}


but then the calling class gets the:

getPowerName(int,int) in Numerology cannot be applied to ()
        jtaOutput.append("Power Name: " + tstNum.getPowerName() + "\n");


error. Am i missing something simple?

Yes i was, i just called the method name in the calculations:

iPowerName = getSoul()+getPersonality(); and it worked.

This post has been edited by Borron555: 15 April 2011 - 10:10 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1