java - error: class, interface, or enum expected -


i have following code trying working project in class in. include requirements

design , implement class day implements day of week in program. class day should store day, such sun sunday. program should able perform following operations on object of type day:

a. set day.

b. print day.

c. return day.

d. return next day.

e. return previous day.

f. calculate , return day adding days current day. example, if current day monday , add 4 days, day returned friday. similarly, if today tuesday , add 13 days, day returned monday.

g. add appropriate constructors.

h. write definitions of methods implement operations class day, defined in through g.

i. write program test various operations on class day.

the code follows:

`import java.util.*;   class day { private int dayvalue; private static final int invalid = -1; public day() { this.dayvalue = invalid; } public day(string day) { setday(day); } public day(int day) { this.dayvalue = (day<0 || day>6) ? invalid : day; } public void setday(string day) {     if(day.equals("sunday") || day.equals("sun")) {         this.dayvalue = 0;     } else if(day.equal("monday") || day.equals("mon")) {         this.dayvalue = 1;     } else if(day.equals("tuesday") || day.equals("tues")) {         this.dayvalue = 2;     } else if(day.equal("wednesday") || day.equals("wed")) {         this.dayvalue = 3;     } else if(day.equals("thursday") || day.equals("thurs")) {         this.dayvalue = 4;     } else if(day.equal("friday") || day.equals("fri")) {         this.dayvalue = 5;     } else if(day.equal("saturday") || day.equals("sat")) {         this.dayvalue = 6;     } else {         this.dayvalue = invalid;     } }  public string getday() {     if (dayvalue==0) { return "sunday"; }     if (dayvalue==1) { return "monday"; }     if (dayvalue==2) { return "tuesday"; }     if (dayvalue==3) { return "wednesday"; }     if (dayvalue==4) { return "thursday"; }     if (dayvalue==5) { return "friday"; }     if (dayvalue==6) { return "saturday"; }      return "\"i don't know day is!\"";  } public void printday() {     system.out.println("when printing, day " + getday()); //displays day @ time of printing. } // next day public string getnextday()   {      // compareto() method allows set saturday sat, sunday sun, etc      if((day.compareto("sunday") == 0) || (day.compareto("sun") == 0))         return ("monday");      else if((day.compareto("monday") == 0) || (day.compareto("mon") == 0))         return ("tuesday");      else if((day.compareto("tuesday") == 0) || (day.compareto("tue") == 0))         return ("wednesday");      else if((day.compareto("wednesday") == 0) || (day.compareto("wed") == 0))         return ("thursday");      else if((day.compareto("thursday") == 0) || (day.compareto("thu") == 0))         return ("friday");      else if((day.compareto("friday") == 0) || (day.compareto("fri") == 0))         return ("saturday");      else if((day.compareto("saturday") == 0) || (day.compareto("sat") == 0))         return ("sunday");      else         return ("\"i don't know day is!\"");   }   // previous day   public string getpreday()   {      if((day.compareto("sunday") == 0) || (day.compareto("sun") == 0))         return ("saturday");      else if((day.compareto("saturday") == 0) || (day.compareto("sat") == 0))         return ("friday");      else if((day.compareto("friday") == 0) || (day.compareto("fri") == 0))         return ("thursday");      else if((day.compareto("thursday") == 0) || (day.compareto("thu") == 0))         return ("wednesday");      else if((day.compareto("wednesday") == 0) || (day.compareto("wed") == 0))         return ("tuesday");      else if((day.compareto("tuesday") == 0) || (day.compareto("tue") == 0))         return ("monday");      else if((day.compareto("monday") == 0) || (day.compareto("mon") == 0))         return ("sunday");         return ("\"i don't know day is!\"");   } public day calcday(int offset) { /* code here */ } // printin public string tostring() { return getday(); } }     // main execution point  public static void main (string args[]) {   {    // 1 of weakness case sensitive: sun, sunday, sunday, sunday...    // need more codes avoid case sensitiveness...     // instantiate testday object of type myday class      // "sun" initial value....      day testday = new day("sun");      // prompt user set his/her day      system.out.print("enter day set day: ");      // read , store user's day      string storeday = readinput.nextline().tolowercase(); //changes input lowercase deal variations      // invoke setday() method set day defined user      testday.setday(storeday);      // invoke getday() method day      system.out.println("your day " + testday.getday());      // test printing invoking printday() method      testday.printday();      // invoke getpreday() method previous day      system.out.println("your previous day " + testday.getpreday());      // invoke getnextday() method next day      system.out.println("your next day " + testday.getnextday());       system.out.println("how many days add? " + testday.calcnextday());   }   }` 

i receiving following error:

day.java:92: error: class, interface, or enum expected      public static void main () {                     ^ day.java:101: error: class, interface, or enum expected          system.out.print("enter day set day: ");           ^ day.java:103: error: class, interface, or enum expected          string storeday = readinput.nextline().tolowercase(); //changes input lowercase deal variations          ^ day.java:105: error: class, interface, or enum expected          testday.setday(storeday);          ^ day.java:107: error: class, interface, or enum expected          system.out.println("your day " + testday.getday());          ^ day.java:109: error: class, interface, or enum expected          testday.printday();          ^ day.java:111: error: class, interface, or enum expected          system.out.println("your previous day " + testday.getpreday());          ^ day.java:113: error: class, interface, or enum expected          system.out.println("your next day " + testday.getnextday());          ^ day.java:115: error: class, interface, or enum expected          system.out.println("how many days add? " + testday.calcnextday());          ^ day.java:116: error: class, interface, or enum expected       }       ^ 10 errors 

originally code looked

public class day { static scanner readinput = new scanner(system.in);
string day; public day(string day) { day = "sunday"; } // set day public void setday(string theday) { day = theday; } public string getday() { return day; } public void printday() { system.out.println("when printing, day " + day); }

why not use arrays?

with array, handle things in more concise , simple way.

string[] dayofweekshortnames = new string[] {      "sun", "mon", "tue", "wed", "thu", "fri", "sat"  };  string[] dayofweeklongnames = new string[] {     "sunday", "monday", "wednesday", "thursday", "friday", "saturday" }; 

then, validate whether value passed in parameter correct.

public void setday(string dayofweek) {     if (dayofweek == null || (0 < dayofweek.length() && dayofweek.trim().length() == 0))         throw new illegalargumentexception("dayofweek cannot null or white space.");      (int = 0; < dayofweek.length(); i++)         if (dayofweek.charat(i).isdigit())              throw new illegalargumentexception("dayofweek cannot numeric.");      if (dayofweek.length() < 3) // short names         throw new illegalargumentexception("dayofweek must @ least 3 characters long.");      (int = 0; < dayofweekshortnames.length && < dayofweeklongnames.length; i++)         if (dayofweekshortnames[i].tolowercase() == dayofweek.tolowercase()             || dayofweeklongnames[i].tolowercase() == dayofweek.tolowercase()) {             dayvalue = i;             return;         }              throw new illegalargumentexception(         "day of week: " + dayofweek + " not found."); } 

this simple example off top of head.

why not use program class?

instead of writing main() method out of can lead multiple compile-time errors, perhaps 1 great deal locate main method inside program class member of class, , compiler shall no longer complaint. furthermore, advantage of doing shall make obvious, @ least @ extent, program entry point there. program class shall contain nothing more main() method.

public class program {     public static void main(string args[]) {         // code here...     } } 

as user inputs, perhaps using console class... here's example on how use it.

disclaimer

i have not tested code , provided as-is example purposes only. not java expert, , did best off top of head help.

edit

i did not use array because wanted store value string , not index.

in fact, code sample in question stores day of week in integer value per line:

private int dayvalue; 

hence assignments when set day follows:

public void setday(string day) {     if (day == "sunday" || day == "sun")         dayvalue = 0;     else if (day == "monday" || day == "mon")         dayvalue = 1;     ... } 

the above stores day of week in integer format shall satisfy of systems going use days of week. plus, cannot guarantee exact syntax of string input user if either compare day == "sunday" or day == "sun", have adjust casing according string you're expecting in setday() function, in opinion doesn't make sense.

the common practice such behaviour playing around days of week, etc. use arrays proper expected casing, compare input values either uppercase or lowercase values both, input , value array (only can guarantee perfect match), can store index in dayvalue private member. then, when retrieving day of week through getday() function, can write single line of code can assure work flawlessly, since captured potential errors while setting input through setday() method.

public string getday() { return dayofweeklongnames[dayvalue]; } 

and shall name of day of week set, instead of having write if statements on , on again.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -