I am new to java and I am trying to work with the stringBuilder at the moment. my current project will allow the user to input a "colors.txt" file. I want to verify that the entered string is valid. The information that is entered is: Had to parenthesize the # but it need to be taken out and reentered on the valid output. (#)F3C- valid, ouput(#FF33CCFF) (#)aa4256- valid, output(AA4256FF) (#)ag0933 - is invalid because 'g' is not hexadecimal (#)60CC- valid, output(6600CCFF) 095- valid, output(009955FF) Be0F- valid, output(BBEE00FF) (#)AABB05C- invalid, to many characters (7)
So the output to another file called "-norm" appended to the name of the file before the dot ".". I want to verify if the entered line is a true hexadecimal color. Also the out put has to have the validated line equal 8 by doubling the characters, if the double does not equal 8 then "FF" has to be appended to it.
I was able to input the file however without verification. It will read each line and go through 2 methods(Just learning as well) for verification. I am having a lot of issues with my code. I can visualize and know what I want to do, I am having an issue translating that into code.
Thank you for all your help!
import java.util.*; // for Scanner
import java.io.*; // for File, etc.
import java.lang.*;
//import java.awt.* //to use the color class
public class RGBAColor
{
//Scanner file = new Scanner(new File("colors.txt"));
public static void main(String[] args) //throws IOException
throws FileNotFoundException
{
Scanner console = new Scanner(System.in);
System.out.println("Please make sure you enter the colors.txt ");
//assigning the colors file
String fileName = console.nextLine();
Scanner inputFile = new Scanner(new File(fileName));
//outputing to colors-norm file
int dotLocation;
StringBuilder dot = new StringBuilder(fileName);
dotLocation = dot.indexOf(".");
//PrintWriter FileName =
//new PrintWriter("colors-norm.txt");
while (inputFile.hasNextLine())
{
String currLine = inputFile.nextLine();
int lineLenght = currLine.length();
//System.out.printf("line length is %s \n", currLine);
verification(currLine);
}
inputFile.close();
}
//this will verify the color
public static void verification(String line)
{
StringBuilder newString = new StringBuilder(line);
//will be used to compare the length
int stringLength;
//will remove the # if in string
if (newString.charAt(0) == '#')
{
newString = newString.deleteCharAt(0);
}
//assigns the length
stringLength = newString.length();
//checks the length of the string
//prompt will show if the number of digits is invalid
if (!(stringLength == 3 || stringLength == 4 || stringLength == 6 || stringLength == 8))
{
System.out.println("invalid number # of digits " + stringLength + " in "
+ newString);
}
StringBuilder errorLessString = new StringBuilder("");
//checks number and letter for valid entries for hexadecimal digit
for (int i = 0; i < newString.length(); i++ )
{
char valid = newString.toString().toUpperCase().charAt(i);
if (!(valid >= '0' && valid <= '9' || valid >= 'A' && valid <= 'F'))
{
System.out.println("invalid color '" + newString.charAt(i) +
"' in " + newString );
}
errorLessString.append(valid);
}
System.out.println("this is the length of " + errorLessString + " " + errorLessString.length());
String resultingString = " ";
// validating length only allowing the correct lengths of 3,4,6,and 8
switch (errorLessString.length())
{
case 3: System.out.println("begin case 3");
dbleAppend(newString.toString());
addFF(newString.toString());
System.out.println("end case 3");
break;
case 4: dbleAppend(newString.toString());
break;
case 6: addFF(newString.toString());
break;
case 8:
}
}
//method to have two characters together
public static String dbleAppend(String appd)
{
StringBuilder charDouble = new StringBuilder("");
//pass in append string to double the characters
for (int i = 0; i < appd.length(); i++)
{
charDouble.append(appd.charAt(i));
charDouble.append(appd.charAt(i));
}
return appd;
}
//method will append ff to string
public static String addFF(String putFF)
{
StringBuilder plusFF = new StringBuilder("FF");
plusFF.append(putFF.toString());
System.out.println(plusFF);
return putFF;
}
}