java - Incorrect output or won't compile -


i having issue couple of programs - 1 debug , other need write.

the first 1 take user inputted planet name, convert uppercase , run through enum. did wrong? import java.util.*;

public class debugnine4 { enum planet {     mercury, venus, earth, mars, jupiter, saturn, uranus, neptune };  public static void main(string[] args) {     planet planet;     string userentry;     int position;     int comparison;     scanner input = new scanner(system.in);     system.out.print("enter planet in our solar system >> ");     planet = input.nextline().touppercase();     planet = planet.valueof(planet);     system.out.println("you entered " + planet);     position = planet.ordinal();     system.out.println(planet + " " + (position + 1)             + " planet(s) sun"); } } 

the 2nd 1 output not going through want it. coming with:

cut shampoo 

and not info need - sortable name of service, cost , time. did go wrong on code?

  1. first need create service object 3 data fields.
  2. next need create array of 6 service objects, populating them data table 9.6, page 420 in book, like:

    service[0] = new service("cut",8.00,15); 
  3. using scanner object, collect response question: "sort services (s)ervice, (p)rice, or (t)ime"

  4. return of information in 3 formatted columns listed in correct order based on input like:

    sorted time:

    trim          $6.00    5 minutes shampoo        $4.00    10 minutes 

code:

public class salonreport {  public static void main(string[] args) {      // services listing time , cost     service[] myservice = new service[6];     myservice[0] = new service("cut", 8.00, 15);     myservice[1] = new service("shampoo", 4.00, 10);     myservice[2] = new service("manicure", 18.00, 30);     myservice[3] = new service("style", 48.00, 55);     myservice[4] = new service("permanent", 18.00, 35);     myservice[5] = new service("trim", 6.00, 5);     sortdescription(myservice, myservice.length);     system.out.println(myservice[0].getservicetype() + " "             + myservice[1].getservicetype()); }  public static void sortdescription(service[] array, int len) {     int a;     int b;     service temp;      (a = 0; < len; ++a)         (b = 0; b < len - 1; ++b) {              if (array[b].getservicetype().compareto(                     array[b + 1].getservicetype()) > 0)                 ;             {                 temp = array[b];                 array[b] = array[b + 1];                 array[b + 1] = temp;              }          }  } }  class service {  // declaring parameters string servdescript; double price; int avgmin;  public service(string s, double p, int m) { // constructor     servdescript = s;     price = p;     avgmin = m;  }  // method returning requested item -  public string getservicetype() {     return servdescript; }  public double getprice() {     return price; }  public int getminutes() {     return avgmin; } } 

first seems be:

planet = planet.valueof(input.nextline().touppercase()); 

remove line, since cant put string enum type variable:

    planet = input.nextline().touppercase(); 

edit:

public class debugnine4 { enum planet {     mercury, venus, earth, mars, jupiter, saturn, uranus, neptune };  public static void main(string[] args) {     scanner input = new scanner(system.in);     system.out.print("enter planet in our solar system >> ");      planet planet = planet.valueof(input.nextline().trim().touppercase());     system.out.println("you entered " + planet);      int position = planet.ordinal();     system.out.println(planet + " " + (position + 1)             + " planet(s) sun"); } } 

above code works, watch out input.

tried combinations like: mars, mars, mars etc.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -