# Solution using a state machine

• Solution with state machine

``````public class Solution {
public boolean isNumber(String s) {
s = s.trim();
if (s.isEmpty())
{
return false;
}

int i = 0;
int state = 0;
while (i < s.length())
{
char cur = s.charAt(i++);
switch (state)
{
case 0:
if (cur >= '0' && cur <= '9')
{
state = 1;
}
else if (cur == '.')
{
state = 2;
}
else if (cur == '-' || cur == '+')
{
state = 3;
}
else
{
return false;
}
break;
case 1:
if (cur >= '0' && cur <= '9')
{
state = 1;
}
else if (cur == '.')
{
state = 4;
}
else if (cur == 'e')
{
state = 6;
}
else
{
return false;
}
break;
case 2:
if (cur >= '0' && cur <= '9')
{
state = 5;
}
else
{
return false;
}
break;
case 3:
if (cur >= '0' && cur <= '9')
{
state = 1;
}
else if (cur == '.')
{
state = 2;
}
else
{
return false;
}
break;
case 4:
if (cur >= '0' && cur <= '9')
{
state = 5;
}
else if (cur == 'e')
{
state = 6;
}
else
{
return false;
}
break;
case 5:
if (cur >= '0' && cur <= '9')
{
state = 5;
}
else if (cur == 'e')
{
state = 6;
}
else
{
return false;
}
break;
case 6:
if (cur >= '0' && cur <= '9')
{
state = 8;
}
else if (cur == '-' || cur == '+')
{
state = 7;
}
else
{
return false;
}
break;
case 7:
if (cur >= '0' && cur <= '9')
{
state = 8;
}
else
{
return false;
}
break;
case 8:
if (cur >= '0' && cur <= '9')
{
state = 8;
}
else
{
return false;
}
break;
default:
return false;

}
}

if (state == 1 || state == 5 || state == 8 || state == 4)
{
return true;
}
return false;
}
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.