Saturday, 15 June 2013

input - Nooby Java programmer confused at output (Constructors) -



input - Nooby Java programmer confused at output (Constructors) -

i new java programmer , next online tutorial improve skills. found next code illustration on tutorial looks should run getting number of errors when run code in eclipse.

this first programme using cases pretty sure have right syntax maybe can point out error? surprised compiler complains line "system.out.println ("are sure (y - yes, n - no)?");"

i getting errors on lines 3,4,7,8,10,11,15. please can tell me why programme not run?

class certainty { system.out.println ("are sure (y - yes, n - no)?"); int ch = system.in.read (); switch (ch) { case 'y': case 'y': system.out.println ("i sure."); break; case 'n': case 'n': system.out.println ("i not sure."); break; default : system.out.println ("incorrect choice."); } }

/* give thanks of helpful responses i'm starting head around java , i'm enjoying , loving how question answered guys awsome.**/

this first programme using cases pretty sure have right syntax maybe can point out error?

most of actual syntax around cases correct, except it's default: not default: (capitalization matters).

but class has step-by-step code within class. can't that. has within initializers or (more commonly) constructors and/or methods.

separately, system.in.read() may throw ioexception, have either declare method throws or catch. in illustration below, grab , happened. you'd more useful that, it's fine kind of quick test.

import java.io.ioexception; // <=== tell compiler we're going utilize class below class certainty { public static final void main(string[] args) { seek { // `try` starts block of code we'll handle (some) exceptions system.out.println ("are sure (y - yes, n - no)?"); int ch = system.in.read (); switch (ch) { case 'y': case 'y': system.out.println ("i sure."); break; case 'n': case 'n': system.out.println ("i not sure."); break; default : system.out.println ("incorrect choice."); } } grab (ioexception e) { // <== `catch` says exceptions we'll handle system.out.println("an exception occurred."); } } }

there, i've moved code standard main method used command-line java apps, , fixed default thing.

java input compiler-construction io case

No comments:

Post a Comment