メイン メソッドの実行時に StringIndexOutOfBoundsError を取得しています。
コマンドラインでの私のプログラムの出力は次のとおりです。
"Please enter the shift, 1 for day, 2 for night"
1
"you entered a number for the shift"
"Please enter the hourly pay Rate"
2
"you entered a number for the pay Rate"
"Please enter the employees name"
brenda
"cat6b"
"your value you entered is correct 0-9 or a - z"
"Please enter the employee number"
100e
"cat41"
"your value you entered is correct 0-9 or a - z"
"Please enter current date in XXYYZZZZ format, X is day, Y is month, Z is year"
10203933
"cat81 "
"your value you entered is correct 0-9 or a - z"
90 1
valye of array is 1
81 0
value of array is 0
82 2
value of array is 2
83 0
value of array is 0
84 3
value of array is 3
85 9
value of array is 9
86 3
value of array is 3
87 3
value of array is 3
"Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of
range: 8
at java.lang.String.charAt(String.java:658)
at ProductionWorker<init>(ProductionWorker.java:66)
at labBookFiftyFour.main(labBookFiftyFour.java:58)"
"Press any key to continue . . ."
コード内の猫の部分は無視してください。println ステートメントを使用してコードをテストしていました。後でプログラムで配列を使用したかったので、配列出力の値も無視します。
これが私の主な方法です。
import java.math.*;
import java.text.DecimalFormat;
import java.io.*;
import java.util.*;
public class labBookFiftyFour
{
public static void main(String[] args)
{
Scanner myInput = new Scanner(System.in);
int shift = -1;
double pRate = -2;
String name = " ";
String number = " ";
String date = " ";
while(shift < 0 || pRate < 0 )
{
System.out.println("Please enter the shift, 1 for day, 2 for night");
if(myInput.hasNextInt()){
System.out.println("you entered a number for the shift");
shift = myInput.nextInt();
}
System.out.println("Please enter the hourly pay Rate");
if(myInput.hasNextDouble()){
System.out.println("you entered a number for the pay Rate");
pRate = myInput.nextDouble();
}
else if (myInput.hasNext()) {
System.out.println("Please enter a proper value");
myInput.next();
} else {
System.err.println("No more input");
System.exit(1);
}
}
myInput.nextLine(); //consume newLine
System.out.println("Please enter the employees name");
name = myInput.nextLine(); //use your isValid method
if(isValid(name))
{
System.out.println("your value you entered is correct 0-9 or a - z ");
}
System.out.println("Please enter the employee number");
number = myInput.nextLine(); //use your isValid method
if(isValid(number))
{
System.out.println("your value you entered is correct 0-9 or a - z ");
}
System.out.println("Please enter current date in XXYYZZZZ format, X is day, Y is month, Z is year");
date = myInput.nextLine(); //use your isValid method
if(isValid(date))
{
System.out.println("your value you entered is correct 0-9 or a - z ");
}
ProductionWorker myWorker = new ProductionWorker(shift, pRate, name, number, date); //int day and night , double payRate
System.out.println("THis is the shift " + myWorker.getShift() + " This is the pay Rate " + myWorker.getPRate() + " " + myWorker.getName()
+ " " + myWorker.getNumber() + " " + myWorker.getDate());
}
//Made this method for testing String input for 0-9 or a - z values , put AFTER main method, but before end of class
public static boolean isValid(String stringName) //This method has to be static, for some reason?
{
System.out.println("cat" + stringName.length() + stringName.charAt(0));
boolean flag = true;
int index = 0;
while(index < stringName.length())
{
if(Character.isLetterOrDigit(stringName.charAt(index)))
{
flag = true;
}
else
{
flag = false;
}
++index;
}
return flag;
}
}
これが私のemployeeOneです。java スーパークラス
public class employeeOne
{
private String name;
private String number;
private String date;
public employeeOne(String name, String number, String date)
{
this.name = name;
this.number = number;
this.date = date;
}
public String getName()
{
return name;
}
public String getNumber()
{
return number;
}
public String getDate()
{
return date;
}
}
これが私の ProductionWorker.java サブクラスで、employeeOne を拡張します。
public class ProductionWorker extends employeeOne
{
private int shift; //shift represents day or night, day = 1, night = 2
private double pRate; //hourly pay rate
public ProductionWorker(int shift, double pRate, String name, String number, String date)
{
super(name, number, date);
this.shift = shift;
if(this.shift >= 3 || this.shift <= 0)
{
System.out.println("You entered an out of bounds shift date, enter 1 for day or 2 for night, else shift will be day");
this.shift = 1;
}
this.pRate = pRate;
boolean goodSoFar = true;
int indexNum = 0;
int indexDate = 0;
if(name.length() <= 10 && number.length() <= 4 && date.length() < 9 )
{
goodSoFar = true;
}
else
{
goodSoFar = false;
}
while(goodSoFar && indexNum < 3) //XXXL XXX digits 1-9, L is a letter A -M
{
if(Character.isDigit(number.charAt(indexNum)))
{
goodSoFar = true;
}
else
{
goodSoFar = false;
}
++indexNum;
}
while(goodSoFar && indexNum < 4)
{
if(Character.isLetter(number.charAt(indexNum)))
{
goodSoFar = true;
}
else if(Character.isDigit(number.charAt(indexNum)))
{
goodSoFar = false;
}
else if(Character.isDigit(number.charAt(indexNum)) == false && Character.isLetter(number.charAt(indexNum)) == false)
{
goodSoFar = false;
}
++indexNum;
}
int[] dateValues = new int[date.length()];
while(goodSoFar && indexDate <= date.length()) //XXYYZZZZ
{
System.out.println("" + date.length() + indexDate + " " + date.charAt(indexDate));
if(Character.isDigit(date.charAt(indexDate)))
{
dateValues[indexDate] = Character.getNumericValue(date.charAt(indexDate));
System.out.println("value of array is " + dateValues[indexDate]);
++indexDate;
}
else
{
goodSoFar = false;
}
}
if(goodSoFar)
{
System.out.println("your input is good so far");
}
else
{
System.out.println("your input is wrong for name or number or date");
}
}
public int getShift()
{
return shift;
}
public double getPRate()
{
return pRate;
}
}