I am attempting to create an array of objects, Trees (like a forest) 12x12. They are all trees at first. The user inputs the number of fires to start and the probability of the fires spreading to one of the surrounding objects. I have it working when I use an array[][] but when trying to make it work with objects I cannot get the objects to check their surrounding objects then decide to catch fire or not. I am new to the site so I am unsure exactly what you need to see.
public static void fireCheck(Tree[][] trees1){
int i = 0;
int c = 0;
for(i = 0; i <= 11; i ++) {
for(c = 0; c <= 11; c ++) {
switch(trees1[i][c].getStatus()){
case '^': break;
case '.': break;
case '*': trees1[i][c].burned(); break;
default: break;
}
if(trees1[i][c].getStatus() == '^'){
if(i <= 10){
switch(trees1[i + 1][c].getStatus()){
case '^': break;
case '.': break;
case '*': trees1[i+1][c].probability(); break;
default: break;
}
if(c <= 10){
switch(trees1[i][c + 1].getStatus()){
case '^': break;
case '.': break;
case '*': trees1[i][c+1].probability(); break;
default: break;
}
switch(trees1[i + 1][c + 1].getStatus()){
case '^': break;
case '.': break;
case '*': trees1[i+1][c+1].probability(); break;
default: break;
}
}
}
if(i >= 1){
switch(trees1[i - 1][c].getStatus()){
case '^': break;
case '.': break;
case '*': trees1[i-1][c].probability(); break;
default: break;
}
if(c >= 1){
switch(trees1[i][c - 1].getStatus()){
case '^': break;
case '.': break;
case '*': trees1[i][c-1].probability(); break;
default: break;
}
switch(trees1[i - 1][c - 1].getStatus()){
case '^': break;
case '.': break;
case '*': trees1[i-1][c-1].probability(); break;
default: break;
}
}
}
if(i <= 10){
if(c >= 1){
switch(trees1[i+1][c-1].getStatus()){
case '^': break;
case '.': break;
case '*': trees1[i+1][c-1].probability(); break;
default: break;
}
}
}
}
}
}
}
I suppose you want to make this program more object oriented.
Create a class Tree, which has some position and a way of identifying if another tree is neighbor.
interface OurTree{
public int getPosition();
public boolean isNeighbour(OurTree tree);
public boolean isBurning();
}
Implementing above interface will help you.
Create a set of objects of the above implemented interface (assume this as forest). Loop through the set, and identify if any tree is on fire. if yes, burn its neighbours.
Alternatively you can define a method to return neighbours of given tree, and burn them all at once.
Related
I have been stuck at this for sometime and was wondering if anyone would be able to guide me.
I am trying to let the user enter in a character and if it does not match one of the switch cases, then it should continue to ask the user. My code does that part fine. The issue comes up when the user tries to enter a correct character after and my code continues to output the default instead of the specific case.
Any help would be appreciated!
Thank you
while(!flag){
switch (endChar) {
case 'T':
case 't':
modFlags[0] = true;
flag = true;
break;
case 'P':
case 'p':
modFlags[1] = true;
flag = true;
break;
case 'W':
case 'w':
modFlags[2] = true;
flag = true;
break;
case 'L':
case 'l':
modFlags[3] = true;
flag = true;
break;
case 'I':
System.out.print("sd");
fileName = updateFileName(sc, fileName);
flag = true;
break;
case 'i':
fileName = updateFileName(sc, fileName);
flag = true;
break;
case 'O':
case 'o':
break;
case 'D':
case 'd':
break;
case 'M':
case 'm':
modeBoth = true;
flag = true;
break;
case 'H':
case 'h':
showMenu = false;
flag = true;
break;
default:
System.out.println("Unknown Option.");
System.out.print("Enter action: ");
sc.next();
break;
// flag = false;
}
}
Change
sc.next();
near the bottom of the switch to
endChar = sc.next().charAt(0);
because all you're currently doing is retrieving a String from the Scanner and not doing anything with it. You need to assign a character of that String to endChar if you want the value of endChar to be different for the next iteration of the loop.
Based on what you provided, I suggest moving the scanning a value portion to the very beginning of your while loop and in the default case of your switch statement using just a continue; to bring you back to the beginning of the while loop. That way it will read in another value from input without having to code it again.
while(!flag){
System.out.print("Enter action: ");
endChar = sc.next().charAt(0);
switch (endChar) {
case 'T':
case 't':
modFlags[0] = true;
flag = true;
break;
case 'P':
case 'p':
modFlags[1] = true;
flag = true;
break;
case 'W':
case 'w':
modFlags[2] = true;
flag = true;
break;
case 'L':
case 'l':
modFlags[3] = true;
flag = true;
break;
case 'I':
System.out.print("sd");
fileName = updateFileName(sc, fileName);
flag = true;
break;
case 'i':
fileName = updateFileName(sc, fileName);
flag = true;
break;
case 'O':
case 'o':
break;
case 'D':
case 'd':
break;
case 'M':
case 'm':
modeBoth = true;
flag = true;
break;
case 'H':
case 'h':
showMenu = false;
flag = true;
break;
default:
continue;
}
}
I have been working on a utility to parse text files in the format used by Paradox Interactive in their grand strategy games to be used with a visual-based modding tool I am also developing. I have a mostly-implemented, crude, early version of the parser written out and it is mostly working as intended. This is my second attempt at writing a text parser (the first, which ended up working just fine, parsed a subset of XML).
I speed-wrote my parser on the 9th and have spent all weekend trying to debug it, but all my efforts have failed. I have tracked the issue down to the 3rd line of nextChar(). It was throwing an ArrayIndexOutOfBounds error with a crazy small number (in the -2 millions). After I added a bounds check the program just... continues. It reads all the information as needed, it just doesn't ever exit the parse loop.
The format is basically this:
car = {
model_year = 1966
model_name = "Chevy"
components = {
"engine", "frame", "muffler"
}
}
though I have yet to add support for nested lists like I plan, so my test string is:
car = {
model_year = 1966
model_name = "Chevy"
}
For both my understanding and anybody who would see my code, I tried to generously comment my code where I thought it might be necessary, though if any clarification is needed I would be happy to provide it.
My code:
/**
* Parses text files in the format used by Paradox Interactive in their computer games EUIV, CK2, and Stellaris.
*
* #author DJMethaneMan
* #date 12/9/2016
*/
public class Parser
{
private int pos, line, len, depth;
public String text;
private char[] script; //TODO: Initialize in the parse method
public Parser()
{
pos = 0;
line = 1;
len = 0;
depth = 0;
text = "car = {\n" +
" model_year = 1966 \n" +
" model_name = \"Chevy\"\n" +
"}\u0003";
//text = "Hello World";
//Car c = new Car();
//parse(text, c);
}
public static void main()
{
Car c = new Car();
Parser p = new Parser();
p.parse(p.text, c);
System.out.println("The model name is " + c.model_name);
System.out.println("The model year is " + c.model_year);
}
//TODO: Work
public void parse(String text, Parseable parsed)
{
char[] script = text.toCharArray();
this.script = script;
boolean next_char = false;
PARSE_LOOP:while(true)
{
char c;
if(next_char)
{
c = nextChar();
}
else
{
c = script[0];
next_char = true;
}
switch(c)
{
case 'A':
case 'a':
case 'B':
case 'b':
case 'C':
case 'c':
case 'D':
case 'd':
case 'E':
case 'e':
case 'F':
case 'f':
case 'G':
case 'g':
case 'H':
case 'h':
case 'I':
case 'i':
case 'J':
case 'j':
case 'K':
case 'k':
case 'L':
case 'l':
case 'M':
case 'm':
case 'N':
case 'n':
case 'O':
case 'o':
case 'P':
case 'p':
case 'Q':
case 'q':
case 'R':
case 'r':
case 'S':
case 's':
case 'T':
case 't':
case 'U':
case 'u':
case 'V':
case 'v':
case 'W':
case 'w':
case 'X':
case 'x':
case 'Y':
case 'y':
case 'Z':
case 'z':
case '_'://TODO: HERE
if(depth > 0) //
{
parsed.parseRead(buildWordToken(true), this);//Let the class decide how to handle this information. Best solution since I do not know how to implement automatic deserialization.
}
continueUntilChar('=', false); //A value must be assigned because it is basically a key value pair with {} or a string or number as the value
skipWhitespace();//Skip any trailing whitespace straight to the next token.
break;
case '{':
depth++;
break;
case '}':
depth--;
break;
case '\n':
line++;
break;
case ' ':
case '\t':
skipWhitespace();
break;
case '\u0003': //End of Text Character... Not sure if it will work in a file...
break PARSE_LOOP;
}
}
}
//Returns a string from the next valid token
public String parseString()
{
String retval = "";
continueUntilChar('=', false);
continueUntilChar('"', false);
retval = buildWordToken(false);
continueUntilChar('"', false); //Don't rewind because we want to skip over the quotation and not append it.
return retval;
}
//Returns a double from the next valid token
public double parseNumber()
{
double retval = 0;
continueUntilChar('=', false); //False because we don't want to include the = in any parsing...
skipWhitespace(); //In case we encounter whitespace.
try
{
retval = Double.parseDouble(buildNumberToken(false));
}
catch(Exception e)
{
System.out.println("A token at line " + line + " is not a valid number but is being passed as such.");
}
return retval;
}
/**********************************Utility Methods for Parsing****************************************/
protected void continueUntilChar(char target, boolean rewind)
{
while(true)
{
char c = nextChar();
if(c == target)
{
break;
}
}
if(rewind)
{
pos--;
}
}
protected void skipWhitespace()
{
while(true)
{
char c = nextChar();
if(!Character.isWhitespace(c))
{
break;
}
}
pos--;//Rewind because by default parse increments pos by 1 one when fetching nextChar each iteration.
}
protected String buildNumberToken(boolean rewind)
{
StringBuilder token = new StringBuilder();
String retval = "INVALID_NUMBER";
char token_start = script[pos];
System.out.println(token_start + " is a valid char for a word token."); //Print it.
token.append(token_start);
while(true)
{
char c = nextChar();
if(Character.isDigit(c) || (c == '.' && (Character.isDigit(peek(1)) || Character.isDigit(rewind(1))))) //Makes sure things like 1... and ...1234 don't get parsed as numbers.
{
token.append(c);
System.out.println(c + " is a valid char for a word token."); //Print it for debugging
}
else
{
break;
}
}
return retval;
}
protected String buildWordToken(boolean rewind)
{
StringBuilder token = new StringBuilder(); //Used to build the token
char token_start = script[pos]; //The char the parser first found would make this a valid token
token.append(token_start); //Add said char since it is part of the token
System.out.println(token_start + " is a valid char for a word token."); //Print it.
while(true)
{
char c = nextChar();
if(Character.isAlphabetic(c) || Character.isDigit(c) || c == '_')//Make sure it is a valid token for a word
{
System.out.println(c + " is a valid char for a word token."); //Print it for debugging
token.append(c); //Add it to the token since its valid
}
else
{
if(rewind)//If leaving the method will make this skip over a valid token set this to true.
{
//Rewind by 1 because the main loop in parse() will still check pos++ and we want to check the pos of the next char after the end of the token.
pos--;
break; //Leave the loop and return the token.
}
else //Otherwise
{
break; //Just leave the loop and return the token.
}
}
}
return token.toString(); //Get the string value of the token and return it.
}
//Returns the next char in the script by amount but does not increment pos.
protected char peek(int amount)
{
int lookahead = pos + amount; //pos + 1;
char retval = '\u0003'; //End of text character
if(lookahead < script.length)//Make sure lookahead is in bounds.
{
retval = script[lookahead]; //Return the char at the lookahead.
}
return retval; //Return it.
}
//Returns the previous char in the script by amount but does not decrement pos.
//Basically see peek only this is the exact opposite.
protected char rewind(int amount)
{
int lookbehind = pos - amount; //pos + 1;
char retval = '\u0003';
if(lookbehind > 0)
{
retval = script[lookbehind];
}
return retval;
}
//Returns the next character in the script.
protected char nextChar()
{
char retval = '\u0003';
pos++;
if(pos < script.length && !(pos < 0))
{
retval = script[pos]; //It says this is causing an ArrayIndexOutOfBoundsException with the following message. Shows a very large (small?) negative number.
}
return retval;
}
}
//TODO: Extend
interface Parseable
{
public void parseRead(String token, Parser p);
public void parseWrite(ParseWriter writer);
}
//TODO: Work on
class ParseWriter
{
}
class Car implements Parseable
{
public String model_name;
public int model_year;
#Override
public void parseRead(String token, Parser p)
{
if(token.equals("model_year"))
{
model_year = (int)p.parseNumber();
}
else if(token.equals("model_name"))
{
model_name = p.parseString();
}
}
#Override
public void parseWrite(ParseWriter writer)
{
//TODO: Implement along with the ParseWriter
}
}
Use of the labeled break statement break PARSE_LOOP; is generally considered bad practice. You are essentially writing a "goto" statement: whenever the break PARSE_LOOP; condition is hit, it jumps back to the beginning of the while loop (because that's where you wrote PARSE_LOOP:). This is probably the reason for your infinite loop. I also don't understand why you would restart a while loop that is already infinite (while true).
Change your code to:
public void parse(String text, Parseable parsed)
{
char[] script = text.toCharArray();
this.script = script;
boolean next_char = false;
boolean parsing = true;
while(parsing)
{
char c;
if(next_char)
{
c = nextChar();
}
else
{
c = script[0];
next_char = true;
}
switch(c)
{
case 'A':
case 'a':
case 'B':
case 'b':
case 'C':
case 'c':
case 'D':
case 'd':
case 'E':
case 'e':
case 'F':
case 'f':
case 'G':
case 'g':
case 'H':
case 'h':
case 'I':
case 'i':
case 'J':
case 'j':
case 'K':
case 'k':
case 'L':
case 'l':
case 'M':
case 'm':
case 'N':
case 'n':
case 'O':
case 'o':
case 'P':
case 'p':
case 'Q':
case 'q':
case 'R':
case 'r':
case 'S':
case 's':
case 'T':
case 't':
case 'U':
case 'u':
case 'V':
case 'v':
case 'W':
case 'w':
case 'X':
case 'x':
case 'Y':
case 'y':
case 'Z':
case 'z':
case '_'://TODO: HERE
if(depth > 0) //
{
parsed.parseRead(buildWordToken(true), this);//Let the class decide how to handle this information. Best solution since I do not know how to implement automatic deserialization.
}
continueUntilChar('=', false); //A value must be assigned because it is basically a key value pair with {} or a string or number as the value
skipWhitespace();//Skip any trailing whitespace straight to the next token.
break;
case '{':
depth++;
break;
case '}':
depth--;
break;
case '\n':
line++;
break;
case ' ':
case '\t':
skipWhitespace();
break;
case '\u0003': //End of Text Character... Not sure if it will work in a file...
parsing = false;
break;
}
}
}
Put a debug statement in to prove that it's hitting your break, I'm guessing it's not (Although it could be the break label--I haven't had reason to look into that construct since I first learned java a couple decades ago). I have a couple suggestions though...
I'd use isAlpha instead of that part of the switch. Cleaner, shorter, probably about as efficient and language-agnostic.
Instead of using the break label (Which is very uncommon), You might want to use boolean parsing=true;while(parsing)... instead. It's not really wrong to use the break label, but... Anything that causes the next guy to spend a minute or two scratching his head is a few minutes wasted.
Am trying to create a method to encrypt a word with Caesar cipher
here is the code:
public static void main (String [] args)
{
String s = "ahmed";
int k = 2;
char[] c = new char[5];
int[] a = new int[s.length()];
for (int i = 0; i<s.length();i++){
a[i] = Secret_Code_Library.getDigit(s.charAt(i));
a[i] = i + k ;
c[i] = Secret_Code_Library.getLetter(a[i]);
}
String ss = String.valueOf(c);
}
Secret_Code_Library is a class that contains the two methods
(getDigit(char): returns the number of the character a =0, b= 1...etc, and
getLetter(int): returns the letter corresponding to the number).
my problem is when ever i try to encrypt a word it encrypts only
the first letter correctly and save it in to the array a.
P.S: I have to use those two methods.
the output of the above code is: cdefg
while it should be: cjogf
this is the class :
public class Secret_Code_Library {
public static final char [] LETTERS={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' };
public static char getLetter(int digit){
return LETTERS[digit]; }
public static int getDigit(char ch){
int digit=0;
switch (ch){
case 'A': digit=0; break; case 'B': digit=1; break; case 'C': digit=2; break;
case 'D': digit=3; break; case 'E': digit=4; break; case 'F': digit=5; break;
case 'G': digit=6; break; case 'H': digit=7; break; case 'I': digit=8; break;
case 'J': digit=9; break; case 'K': digit=10; break; case 'L': digit=11; break;
case 'M': digit=12; break; case 'N': digit=13; break; case 'O': digit=14; break;
case 'P': digit=15; break; case 'Q': digit=16; break; case 'R': digit=17; break;
case 'S': digit=18; break; case 'T': digit=19; break; case 'U': digit=20; break;
case 'V': digit=21; break; case 'W': digit=22; break; case 'X': digit=23; break;
case 'Y': digit=24; break; case 'Z': digit=25; break;
}// switch
return digit;
}}
a[i]= i + k ;
should be
a[i] = a[i] + k;
because you're not adding the key to the position of the current encrypted character, but the integer value of the character itself.
But that is not enough. What happens when you're encrypting 'z' with k = 2? You should wrap around in the alphabet, if the Secret_Code_Library.getLetter(a[i]); doesn't already do this for you.
If your alphabet is 26 characters long, then this should do it:
a[i] = (a[i] + k) % 26;
I have a problem in java. It can not find the symbol in my switch function.
I think there is something wrong with char inputChar(i) but I can not figure out the problem.
Here is the code:
import eip.*;
public class IntDigitAutomat
{
public static boolean digitCheck(char inputChar)
{
boolean isdigit = true;
int q = 1;
for (int i=0; i<input.length(); i++)
{
if(isdigit == false)
break;
switch(q)
{
case 1:
switch(inputChar(i))
{
case '+':
case '-':
isdigit = true;
q=2;
break;
case ' ':
isdigit = true;
q=1;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
isdigit = true;
q=3;
break;
default:
isdigit = false;
}
case 2:
switch(inputChar(i))
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
isdigit = true;
q=3;
break;
default:
isdigit = false;
}
case 3:
switch(inputChar(i))
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
isdigit = true;
break;
default:
isdigit = false;
}
}
return isdigit;
}
}
}
I needed more text. I needed more text. I needed more text. I needed more text.
you have to pass single character to your switch statement, change switch(inputChar(i)) to switch(inputChar)
for (int i=0; i<input.length(); i++)
This is where you are getting a problem where is the string or literal that you are supposed to be searching? where is the String input?
If you are trying to create some kind of math expression parser you are going about it the wrong way.
You should look on google or even other questions here about math parsing.
Also look at the other answer about your inputChar(i) which is also going to solve another error you are getting.
your code is not proper if you are using inputchar array then in method attribute take it as array if its not array then loop condition is wrong as well as switch condition is wrong.
I have a double split up into an int array, however the problem I am facing, is that I need it to fill in from the back, like this:
int[] arrayA = new int[5];
int[] arrayB = new int[5];
x = 23456.08;
//code here
//what im left with:
arrayA [0,2,3,4,5,6];
arrayB [0,8];
Heres how im cutting up the double:
public static void main(System args[])
{
Scanner input = new Scanner(System.in);
String answer = "";
int count = 0;
int thatone = 0;
int[] splitD = new int[5]; //main num
int[] splitDec = new int[1]; //decimal
//Enter the Number
System.out.print("Enter a number to convert: ");
double num = input.nextDouble();
// convert number to String
String convert = num + "";
// split the number
String[] split = convert.split("\\.");
String firstPart = split[0];
char[] charArray1 = firstPart.toCharArray();
// recreate the array with size equals firstPart length
splitD = new int[charArray1.length];
for (int i = 0; i < charArray1.length; i++)
{
// convert char to int
splitD[i] = Character.getNumericValue(charArray1[i]);
count++;
}
// the decimal part
if (split.length > 1)
{
String secondPart = split[1];
char[] charArray2 = secondPart.toCharArray();
splitDec = new int[charArray2.length];
for (int i = 0; i < charArray2.length; i++)
{
// convert char to int
splitDec[i] = Character.getNumericValue(charArray2[i]);
}
}
for(int i =0; i<count;i++)
{
if(i ==0) // x00000.00 or 000x00.00
{
if(splitD[0] != "0")
{
switch (splitD[i])
{
case 9: answer+="Nine"; break;
case 8: answer+="Eight"; break;
case 7: answer+="Seven"; break;
case 6: answer+="Six"; break;
case 5: answer+="Five"; break;
case 4: answer+="Four"; break;
case 3: answer+="Three"; break;
case 2: answer+="Two"; break;
case 1: answer+="One"; break;
default: answer+=""; break;
}
answer+= " Hundred ";
}
else
{
answer+= "";
}
}
else if(i ==1)//this goes with i =2 //0x0000
{
if(splitD[i] == 1)
{
switch (splitD[i+1])
{
case 9: answer+="Nineteen"; break;
case 8: answer+="Eighteen"; break;
case 7: answer+="Seventeen"; break;
case 6: answer+="Sixteen"; break;
case 5: answer+="Fifteen"; break;
case 4: answer+="Fourteen"; break;
case 3: answer+="Thirteen"; break;
case 2: answer+="Twelve"; break;
case 1: answer+="ten"; break;
default: answer+=""; break;
}
answer+= " Thousand ";
thatone = 1;
}
else
{
switch (splitD[i])
{
case 9: answer+="Ninety"; break;
case 8: answer+="Eighty"; break;
case 7: answer+="Seventy"; break;
case 6: answer+="Sixty"; break;
case 5: answer+="Fifty"; break;
case 4: answer+="Fourty"; break;
case 3: answer+="Thirty"; break;
case 2: answer+="Twenty"; break;
case 1: answer+=""; break;
default: answer+=""; break;
}
}
}
else if(i == 2) //00x000
{
if(thatone ==0)
{
switch (splitD[i])
{
case 9: answer+=" Nine"; break;
case 8: answer+=" Eight"; break;
case 7: answer+=" Seven"; break;
case 6: answer+=" Six"; break;
case 5: answer+=" Five"; break;
case 4: answer+=" Four"; break;
case 3: answer+=" Three"; break;
case 2: answer+=" Two"; break;
case 1: answer+=" One"; break;
default: answer+=""; break;
}
answer+= " Thousand ";
}
else
{
}
}
else if(i ==3)
{
switch (splitD[i])
{
case 9: answer+="Nine"; break;
case 8: answer+="Eight"; break;
case 7: answer+="Seven"; break;
case 6: answer+="Six"; break;
case 5: answer+="Five"; break;
case 4: answer+="Four"; break;
case 3: answer+="Three"; break;
case 2: answer+="Two"; break;
case 1: answer+="One"; break;
default: answer+=""; break;
}
answer+= " Hundred ";
}
else if(i ==4) //0000x0
{
switch (splitD[i])
{
case 9: answer+="Ninety"; break;
case 8: answer+="Eighty"; break;
case 7: answer+="Seventy"; break;
case 6: answer+="Sixty"; break;
case 5: answer+="Fifty"; break;
case 4: answer+="Fourty"; break;
case 3: answer+="Thirdy"; break;
case 2: answer+="Twenty"; break;
case 1: answer+=""; break;
default: answer+=""; break;
}
answer+= " ";
}
else if(i ==5) //00000x
{
switch (splitD[i])
{
case 9: answer+="Nine"; break;
case 8: answer+="Eight"; break;
case 7: answer+="Seven"; break;
case 6: answer+="Six"; break;
case 5: answer+="Five"; break;
case 4: answer+="Four"; break;
case 3: answer+="Three"; break;
case 2: answer+="Two"; break;
case 1: answer+="One"; break;
default: answer+=""; break;
}
}
}
if(splitDec[0] == 0)
{
answer += " and 00/100 Dollars";
}
else if(splitDec[1] == 0)
{
answer += " and " +splitDec[0] + "0/100 Dollars";
}
else
{
answer += " and " +splitDec[0] +splitDec[1] +" /100 Dollars";
}
System.out.println(answer);
}
}
What should I do to make the array add 0 in the appropriate places?
such as if I typed in 54.00 I would get:
int[] SplitD = {0,0,0,0,5,4};
Thanks!
Instead of re-initializing the array:
// recreate the array with size equals firstPart length
splitD = new int[charArray1.length];
for (int i = 0; i < charArray1.length; i++)
{
// convert char to int
splitD[i] = Character.getNumericValue(charArray1[i]);
count++;
}
Use the existing (with length = 5), iterating from the back:
if(charArray1.length > 5) // heavy hard-code, would prefer something better
throw new IllegalArgumentException("Maximum 5 digits, on both decimal sides.");
// watch out of ArrayIndexOutOfBoundsException
for (int i = charArray1.length - 1, j = splitD.length -1 ;
i >=0 && j >=0; i--, j--)
{
// convert char to int
splitD[j] = Character.getNumericValue(charArray1[i]);
count++;
}
Recall, int arrays are filled with zeros, at initialization.
Think creating an array of String, one String for each character (including the decimal point), would be a better approach:
String[] chars = String.valueOf(num).split("(?<=.)");
Job done.
Edit:
To use a switch, don't even split:
for (byte b : String.valueOf(num).getBytes()) {
switch(b) {
case '.':
case '1':
}
}