Keeps saying "incompatible types String[] cannot be converted to string"
do I have something wrong?
public class TimeCard {
private int employeeNum;
private String[] clockInTimes = new String[14];
private String[] clockOutTimes = new String[14];
private float[] decimalClockIn = new float[14];
private float[] decimalClockOut = new float[14];
private float[] timeElapsed = new float[14];
public String getClockInTimes()
{ //im getting the error here
return clockInTimes;
}
public void setClockInTimes(String[] value)
{ //getting the error here
clockInTimes = value;
}
}
Update now it wont let me enter a string within the array.
I'm trying to do it like this:
public class TestTimeCard {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
TimeCard josue = new TimeCard();
System.out.println("Enter Monday Clock In Times:");
josue.setClockInTimes(reader.next());
}
}
You are returning array but return type is defined as just string.
public String[] getClockInTimes()
{ //im getting the error here
return clockInTimes;
}
Related
I'm trying to print contents of an array stored inside an ArrayList .
My constructor parameters are (String,String,String,String[], String, String).
When creating add. function to the arrayList the contents are stored in the String[] parameter.
But when using a getter method to return the String[], it launches an error: "Type mismatch: cannot convert from String to String[]".
Eclipse solution is to change getter method to String, but then the add. function doesn't work because the parameter should be String[].
Also all the .toString, .clone, etc, returns memory location not array contents.
Desperate for help!!
Here is part of my code:
public class NewRegandLogin {
private String alumniFirstName;
private String alumniLastName;
private String alumniId;
private Scanner scanner = new Scanner(System.in);
private String linkedInPage;
static ArrayList<NewRegandLogin> loginInformation = new ArrayList<>();
private String alumniIdImput;
private String passwordImput;
private String permanentPasword;
private String[] coursesList;
public NewRegandLogin(String alumniFirstName, String alumniLastName,String alumniId, String[] coursesList, String linkedInPage, String permanentPasword) {
this.alumniFirstName=alumniFirstName;
this.alumniLastName = alumniLastName;
this.alumniId = alumniId;
this.coursesList = coursesList;
this.linkedInPage = linkedInPage;
this.permanentPasword = permanentPasword;
}
public void setAlumniCourses() {
coursesList = new String[10];
for (int i=0; i < coursesList.length; i++) {
if(coursesList[i]==null) {
System.out.println("Completed Course Name: ");
coursesList[i]=scanner.next();
}
if(coursesList[i].equals("s") || coursesList[i].equals("S")) {
break;
}
}
}
public String[] getCourses() {
return Arrays.toString(coursesList);
}
main
public class Main {
static NewRegandLogin newRegAndLogin = new NewRegandLogin(null, null, null, null, null, null);
public static void main(String[] args) {
System.out.println("Please make a list of completed Courses: (Enter S to stop adding courses) ");
newRegAndLogin.setAlumniCourses();
loginInformation.add(newRegAndLogin);
printAlumniProfile();
}
public static void printAlumniProfile() {
for (int i = 0; i<NewRegandLogin.loginInformation.size();i++) {
System.out.println(((i+1)+"-"+ NewRegandLogin.loginInformation.get(i)));
System.out.println();
}
}
}
Output:
1-Alumni Name: Geri Glazer
Alumni ID: geri.glazer.she-codes
Courses completed: [Ljava.lang.String;#3dd3bcd
public String[] getCourses() {
return Arrays.toString(coursesList);
}
Arrays.toString returns a String, not a String[]. If you want to return a String, change the return type to String.
public String getCourses() {
return Arrays.toString(coursesList);
}
If you want to return a single-element array, containing the string representation of coursesList, you can wrap the String in an array:
public String[] getCourses() {
return new String[]{Arrays.toString(coursesList)};
}
If you want to return the array, just return the array:
public String[] getCourses() {
return coursesList;
}
Or, more safely, return a defensive copy of the array (to prevent the caller changing the internal array):
public String[] getCourses() {
return Arrays.copyOf(coursesList, coursesList.length);
}
I'm trying to link the trainee object with the training session object, but this error shows up: TrainingSession cannot be converted to TrainingSession[] (it's in the last line).
I can't use an array list or anything similar because I have to follow the UML diagram in the assignment. I have used the following variables sessionName, traineeNo, and sessionNo to know which trainee and training session I'm dealing with.
public class Trainee extends Person {
private TrainingSession [] ST;
public TrainingSession [] getST() {
return ST;
}
public void setST(TrainingSession [] ST) {
this.ST = ST;
}
}
public class TrainingSession {
private int trainingSessionID;
public int getTrainingSessionID() {
return trainingSessionID;
}
public void setTrainingSessionID(int trainingSessionID) {
this.trainingSessionID = trainingSessionID;
}
}
public class TMS2 {
public static void main(String[] args)
throws FileNotFoundException, ParseException {
File file = new File("input.txt");
Scanner read = new Scanner(file);
Trainee [] trainee = new Trainee[15];
int traineeID = read.nextInt();
int trainingSessionID = read.nextInt();
String sessionName = TrainingSession(trainingSession, trainingSessionID);
int traineeNo = TraineeNo( trainee, traineeID);
int sessionNo = SessionNo( trainingSession, sessionName);
trainee[traineeNo].setST(trainingSession[sessionNo]);
}
}
trainingSession[sessionNo] is a TrainingSession object, not an array.
Perhaps you're looking for something like...
trainee[traineeNo].setST(new TrainingSession[] { trainingSession[sessionNo]});
I am trying to get multiple inputs in a single code of line..
for example in c++, we could have it like -
int a,b,c;
cin>>a>>b>>c;
is it possible in java also??
You can use an array for this purpose, like:
public static void main(String[] args) {
int[] values = new int[3];
Scanner in = new Scanner(System.in);
for(int i = 0; i < values.length; i++) {
values[i] = in.nextInt();
}
System.out.println(Arrays.toString(values));
}
UPDATE 2
In java 8 the above solution can have a shorter version:
Scanner in = new Scanner(System.in);
Integer[] inputs = Stream.generate(in::nextInt).limit(3).toArray(Integer[]::new);
UPDATE 1
There is another way, which is closer to cin:
public class ChainScanner {
private Scanner scanner;
public ChainScanner(Scanner scanner) {
this.scanner = scanner;
}
public ChainScanner readIntTo(Consumer<Integer> consumer) {
consumer.accept(scanner.nextInt());
return this;
}
public ChainScanner readStringTo(Consumer<String> consumer) {
consumer.accept(scanner.next());
return this;
}
}
public class Wrapper {
private int a;
private int b;
private String c;
public void setA(int a) {
this.a = a;
} /* ... */
}
public static void main(String[] args) {
ChainScanner cs = new ChainScanner(new Scanner(System.in));
Wrapper wrapper = new Wrapper();
cs.readIntTo(wrapper::setA).readIntTo(wrapper::setB).readStringTo(wrapper::setC);
System.out.println(wrapper);
}
Hello So I have a entire class called tractor with different data's stored in it but now I'm suppose to create an object call tractor with a zero parameter constructor but This is the code I have so far and its giving em errors
First off this my Tractor Class which is in a different file:
import java.util.Scanner;
class Tractor
{
private int RentalRate;
private int RentalDays;
private int VehicleID;
private int RentalProfit;
public void setRentalRate(int r)
{
Scanner input = new Scanner(System.in);
System.out.println("What's the Rental Rate?");
int num = input.nextInt();
num = r;
if(r<0 || r >1000)
RentalRate = r;
RentalRate= 1;
}
public int getRentalRate()
{
return RentalRate;
}
public void setVehicleID(int v)
{
Scanner input = new Scanner(System.in);
System.out.println("What's the vehicleID?");
int num1 = input.nextInt();
num1 = v;
if(v<0)
VehicleID = v;
VehicleID = 1;
}
public int getVehicleID()
{
return VehicleID;
}
public void setRentalDays(int d)
{
Scanner input = new Scanner(System.in);
System.out.println("How many rental days?");
int num2 = input.nextInt();
num2 = d;
if(d<0)
RentalDays = d;
RentalDays = 1;
}
public int getRentalDays()
{
return RentalDays;
}
public String toString()
{
String str;
str = "RentalDays:" + RentalDays +"\nRenalRate:" + RentalRate + "\nVehicleID " + VehicleID;
return str;
}
public void RentalProfit(int RentalRate, int RentalDays)
{
RentalProfit = RentalRate * RentalDays;
}
}
import java.util.Scanner;
public class testTractor
{
public static void main(String[] args)
{
public tractor()
{
this.RentalDays = d;
this.RentalRate = r;
this.VehicleID = v;
}
}
}
The error is :
testTractor.java:7: error: illegal start of expression
public tractor()
^
testTractor.java:7: error: ';' expected
public tractor()
^
2 errors
You have compilation errors. You need to first declare the Tractor class then add the constructor inside it. One way to do is declare in a separate file. Also in Java unless you had defined d you couldnt have assigned it. Maybe you wanted to assign the day as a String look in the examples I provide below.
You need to to first create a file call Tractor.java and then define variables there. For example contents of Tractor.java:
public class Tractor {
String rentaldays,someOtherValue;
public Tractor(){
rentaldays ="monday";
someOtherValue="value";
}
//or
public Tractor(String rentalDays){
this.rentaldays = rentalDays;
someOtherValue = "asf";
}
}
Then in your main method You can do Tractor trac = new Tractor(); or Tractor trac = new Tractor("tuesday"); also after that you can print the rentaldays of trac using System.out.println(trac.rentaldays);
From the looks of it you will probably be making a tractor rental system. In that case, rentalDays may be an array of Strings. And then you would have an array of Tractor objects to store in the rental system. You can look at these terms and keywords to point you in the right direction.
You are defining it wrong, define your methods inside class then call them in main() method.
class Test{
public void greeting(){
System.out.print("hello to JAVA..");
}
public static void main(String[] args){
Test testObj = new Test();
testObj.greeting();
}
}
you use an illegal of java syntax, if you already have class tractor in your project. for calling it to in other class, try below code
public class TestTractor(){
Tractor objTractor;
public static void main(String[] args){
//create new tractor object with no parameter
objTractor = new Tractor();
//create new tractor object with parameter
objTractor = new Tractor(parameter here);
//do some action of object here
...........
}
}
//This is just a sample
in your tractor class add below code
public tractor()
{
this.RentalDays = d;
this.RentalRate = r;
this.VehicleID = v;
}
And keep your TestTractor class as
public class TestTractor(){
public static void main(String[] args){
Tractor objTractor = new Tractor();
// objTractor.yourMethodName
}
}
so i'm trying to pass an initialized object which is "p" from one file to method add() in another file. Null exception occurs at storage[count]=p
public class PlayerList {
private static final int INITIAL_SIZE = 2;
private Player[] storage;
private int count;
public PlayerList() {
// Initialize the storage array and set count to 0.
Player[] storage = new Player[2];
storage[0] = new Player("default name1");
storage[1] = new Player("default name2");
count = 0;
}
public void add(Player p) {
storage[count] = p;
count++;
}
//In another file:
public static void PlayerListTestOne() {
System.out.println("PlayerList testing: constructor, size, add, get(0), find");
PlayerList l = new PlayerList();
Player p = new Player("Derek Jeter");
displayResults(l.size() == 0);
l.add(p);
displayResults(l.size() == 1);
displayResults(l.get(0).equals(p));
displayResults(l.find(p) != -1);
}