0

I have a orpaned case error and im not sure where i am missing anything. Probably just needs another set of eyes to find it. thank you in advance

Here is the error code :
C:\Users\winte497\Documents\FSMValue.java:109: orphaned case
case 6: if ((ch >= '0') && (ch <= '9'))

Here is my program:

import java.util.*;
public class FSMValue {

  public static void main(String[] args) {

	int x;
	String s;
	char ch;
	int state;
	double value;
	double whole_part, dece_part;
	int sign,power;
	Scanner input = new Scanner ( System.in );

	System.out.print("Enter Number or enter CTRL z to exit =====> ");
	while (input.hasNext())
	{
 		s = input.nextLine();
 		s = s + ' ';
 		x = 0;
 		state =1;
 		value =0;
 		whole_part =0;
 		dece_part = 0;
 		sign =1;
 		power =10;
		while ((state != 7) & (state != 6))
		{
			ch=s.charAt(x++);
			System.out.print(ch);
			switch (state)
			{
			case 1:	if ((ch >= '0') && (ch <= '9'))
					{
					state = 3;
					whole_part = whole_part + (int)ch-(int)'0';
					}
				else
					if ((ch == '+') || (ch == '-'))
						{
						state =2;
						if (ch == '-')
							sign =-1;
						}
					else
						{
							state = 7;
							System.out.println(" <== Loooky what we got here. Digit, +, or - expected ");
						}
				break;

			case 2:	if ((ch >= '0') && (ch <= '9'))
					{
					state = 3;
					whole_part = whole_part+ (int)ch-(int)'0';
					}
				else
					{
						state = 7;
						System.out.println(" <== Loooky what we got here. Digit expected ");
					}
				break;

			case 3:	if ((ch >= '0') && (ch <= '9'))
					{
					state = 3;
					whole_part = whole_part*10 + (int)ch-(int)('0');
					}
				else
					if (ch == '.')
						state = 4;
					else
						if (ch == ' ')
							state =6;
						else
						{
							state = 7;
							System.out.println(" <== Loooky what we got here. Digit, Blank or Period expected ");
						}
				break;

			case 4:	if ((ch >= '0') && (ch <= '9'))
					{
					state = 5;
					dece_part += (int)ch-(int)('0');
					}
				else
					{
						state = 7;
						System.out.println(" <== Loooky what we got here. Digit expected ");
					}
				break;

			case 5:	if ((ch >= '0') && (ch <= '9'))
					{
					state = 5;
					dece_part = dece_part*10 + (int)ch-(int)('0');
					power = power *10;
					}
				else
					if (ch == ' ')
							state =6;
					else
						{
							state = 7;
							System.out.println("  <== Loooky what we got here. Digit or Blank expected ");

			case 6:	if ((ch >= '0') && (ch <= '9'))
					{
					state = 3;
					whole_part = whole_part + (int)ch-(int)'0';
					}
					else
						if ((ch == 'e') || (ch == 'E'))
					{
							state =2;
						if (ch == 'e')

					}
					else
							state = 7;
							System.out.println(" <== Loooky what we got here. e or E expected ");
					}
					break;

					}
			case 7:	if ((ch >= '0') && (ch <= '9'))
					{
					state = 5;
					dece_part += (int)ch-(int)('0');
					}
					else
					{
						state = 7;
						System.out.println(" <== Loooky what we got here. Digit expected ");
					}
				break;

			} //end switch
		} // end while state

		if (state ==6)
			{
			System.out.println(" <=== This Parsing was Successful");
			System.out.printf("%s%6.2f", " The value of the number is = ",sign *(whole_part + dece_part/power));
			}
		System.out.print("\n-----------------------------------\n");
		System.out.print("\nEnter Number or enter CTRL z to exit =====> ");
	} // end while end of file

} // end of main method
} // end of class

Edited by masijade: added code tags

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by chriswinter
1

well ... no offence, but you've made a mess out of your usage of
{'s and }'s.

starting from line 108.

not to mention the fact that you're using variables you didn't declare or initialize.
you may want to spend some minutes cleaning your code.

0

You don't close the else block of case 5.

Always use braces when using if and you won't forget this sort of thing.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.