I have a problem!!!Please help me!!!
Well, I have to write a program for my exams...
But there is a problem.
See the code I have made:
import java.io.*;
publicclass Project{
publicstaticvoid main(String args[])throws IOException{//scanf arxi
Project h=new Project();
out:
System.out.println("\t\t\t\tKalos irthes!\n\t\t\t Dose arithmo apo 1 mexri 5");
System.out.println("1.Anoigma Arxeiou\n2.Provoli Arxeiou\n3.Eyresi Alpharithmitikou\n4.Sigkrisi Arxeion\n5.Exodos apo to oraio mas programma!!!");
int a,b,c,exit=0;
String sig;
String line;
InputStreamReader r =new InputStreamReader(System.in);
BufferedReader ReadThis =new BufferedReader(r);
line = ReadThis.readLine ();
a = Integer.parseInt(line);//scanf telos
if (a<=0 || a>5){
System.out.println("LATHOS");
}
else{
switch (a){
case 1: System.out.println(" 1");
break;
case 2: System.out.println(" 2");
break;
case 3: System.out.println(" 3");
break;
case 4: System.out.println(" 4");
break;
case 5: System.out.println("Eisai sigouros? An nai pata 1!");
InputStreamReader p =new InputStreamReader(System.in);
BufferedReader ReadThat =new BufferedReader(p);
sig = ReadThat.readLine ();
c = Integer.parseInt(sig);
if (c==1){
break;}
else{
break out ;}
}
}
}
}
Please don't bother which is inside println... It is greek with english characters :)
Well the problem it that i have made a label. The label out!
I made it in order when user submit 5 at the swich the program to close. But after submiting 5 I must ask him again if he want to leave. If the user press 1 the program will close. But when press anything else to return to the start!
This will be very very easy in C. I have made it with goto command... But java have not goto. Have somthing similar, labels. I used the label out.
But when i compile it i get this error:undentidied label: out
Where is my error?
Please tell me the error or another idea to return the user to start...
Thanks a lot.
[3797 byte] By [
crash21a] at [2007-10-2 8:41:11]

I don't think you need a label and don't think you need "ReadThat". It's your design and it's all part of learning so this is what i can offer you to work from.
public class Project{
public static void main(String args[]) throws IOException{ //scanf arxi
Project h=new Project();
out: {
System.out.println("\t\t\t\tKalos irthes!\n\t\t\t Dose arithmo apo 1 mexri 5");
System.out.println("1.Anoigma Arxeiou\n2.Provoli Arxeiou\n3.Eyresi Alpharithmitikou\n4.Sigkrisi Arxeion\n5.Exodos apo to oraio mas programma!!!");
int a,b,c,exit=0;
String line;
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader ReadThis = new BufferedReader(r);
while((line = ReadThis.readLine())!=null){
a = Integer.parseInt(line);//scanf telos
if (a<=0 || a>5){
System.out.println("LATHOS");
}
else{
switch (a) {
case 1: System.out.println(" 1");
break;
case 2: System.out.println(" 2");
break;
case 3: System.out.println(" 3");
break;
case 4: System.out.println(" 4");
break;
case 5: System.out.println("Eisai sigouros? An nai pata 1!");
line = ReadThis.readLine();
c = Integer.parseInt(line);
if (c==1){
break out;
}
System.out.println("\t\t\t\tKalos irthes!\n\t\t\t Dose arithmo apo 1 mexri 5");
System.out.println("1.Anoigma Arxeiou\n2.Provoli Arxeiou\n3.Eyresi Alpharithmitikou\n4.Sigkrisi Arxeion\n5.Exodos apo to oraio mas programma!!!");
break;
}
}
}
}
}
}
it's work!!!! this i want!!!but i don't understand the changes you make... I am a vit confused about the while you use and the last 3 lines of code after last if....Anyway! thanks a lot :)
> it's work!!!! this i want!!!
> but i don't understand the changes you make... I am a
> vit confused about the while you use and the last 3
> lines of code after last if....
> Anyway! thanks a lot :)
well, it was a minimal hack on existing code and you probably shouldn't do it that way.
there are lots of ways to do things, you needed a while loop rather than a label in order to repeat the processing.
see if this makes more sense. i removed extra variables that weren't used and since it seemed to be all static i removed the line with "new Project()"
public class Project{
public static void main(String args[]) throws IOException{
int a,c=0;
String line;
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader ReadThis = new BufferedReader(r);
while(c!=1){
System.out.println("\t\t\t\tKalos irthes!\n\t\t\t Dose arithmo apo 1 mexri 5");
System.out.println("1.Anoigma Arxeiou\n2.Provoli Arxeiou\n3.Eyresi Alpharithmitikou\n4.Sigkrisi Arxeion\n5.Exodos apo to oraio mas programma!!!");
line = ReadThis.readLine();
a = Integer.parseInt(line);//scanf telos
if (a<=0 || a>5){
System.out.println("LATHOS");
}
else{
switch (a) {
case 1: System.out.println(" 1");
break;
case 2: System.out.println(" 2");
break;
case 3: System.out.println(" 3");
break;
case 4: System.out.println(" 4");
break;
case 5: System.out.println("Eisai sigouros? An nai pata 1!");
line = ReadThis.readLine();
c = Integer.parseInt(line);
break;
}
}
}
}
}
I have done this for now...
import java.io.*;
public class Project{
public static void main(String args[]) throws IOException{ //scanf arxi
Project h=new Project();
System.out.println("\t\t\t\tKalos irthes!\n\t\t\t Dose arithmo apo 1 mexri 5");
System.out.println("1.Anoigma Arxeiou\n2.Provoli Arxeiou\n3.Eyresi Alpharithmitikou\n4.Sigkrisi Arxeion\n5.Exodos apo to oraio mas programma!!!");
int a,b,c;
String line;
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader ReadThis = new BufferedReader(r);
while((line = ReadThis.readLine())!=null){
try{
a = Integer.parseInt(line);//scanf telos
}
catch (NumberFormatException e)
{ System.out.println("\t\t\tTo \""+line+"\" den einai apo 1 mexri 5");
System.out.println("\t\t\tDokimase xana, SOSTA ayti ti fora!!!");
continue;
}
switch (a) {
case 1:
System.out.println("\n\n\n\n\n\t\t\t Dose arithmo apo 1 mexri 5");
System.out.println("1.Anoigma Arxeiou\n2.Provoli Arxeiou\n3.Eyresi Alpharithmitikou\n4.Sigkrisi Arxeion\n5.Exodos apo to oraio mas programma!!!");
break;
case 2:
System.out.println("\n\n\n\n\n\t\t\t Dose arithmo apo 1 mexri 5");
System.out.println("1.Anoigma Arxeiou\n2.Provoli Arxeiou\n3.Eyresi Alpharithmitikou\n4.Sigkrisi Arxeion\n5.Exodos apo to oraio mas programma!!!");
break;
case 3:
System.out.println("\n\n\n\n\n\t\t\t Dose arithmo apo 1 mexri 5");
System.out.println("1.Anoigma Arxeiou\n2.Provoli Arxeiou\n3.Eyresi Alpharithmitikou\n4.Sigkrisi Arxeion\n5.Exodos apo to oraio mas programma!!!");
break;
case 4: System.out.println(" 4");
System.out.println("\n\n\n\n\n\t\t\t Dose arithmo apo 1 mexri 5");
System.out.println("1.Anoigma Arxeiou\n2.Provoli Arxeiou\n3.Eyresi Alpharithmitikou\n4.Sigkrisi Arxeion\n5.Exodos apo to oraio mas programma!!!");
break;
case 5: System.out.println("Eisai sigouros? An nai pata 1!");
line = ReadThis.readLine();
c = Integer.parseInt(line);
if (c==1){
return;}
System.out.println("\t\t\t Dose arithmo apo 1 mexri 5");
System.out.println("1.Anoigma Arxeiou\n2.Provoli Arxeiou\n3.Eyresi Alpharithmitikou\n4.Sigkrisi Arxeion\n5.Exodos apo to oraio mas programma!!!");
break;
default: System.out.println("Lathos");
System.out.println("\t\t\t Dose arithmo apo 1 mexri 5 LEME");
System.out.println("1.Anoigma Arxeiou\n2.Provoli Arxeiou\n3.Eyresi Alpharithmitikou\n4.Sigkrisi Arxeion\n5.Exodos apo to oraio mas programma!!!");
break;
}
}
}
}
I have tested the code and is ok!!!
I wish to finalize it as I am only at the start of the project!!!
currently, your code does not use this
Project h=new Project();
or the variable "b" in this declaration
int a,b,c;
These lines are repeated in all cases where the loop is intended to continue
System.out.println("\n\n\n\n\n\t\t\t Dose arithmo apo 1 mexri 5");
System.out.println("1.Anoigma Arxeiou\n2.Provoli Arxeiou\n3.Eyresi Alpharithmitikou\n4.Sigkrisi Arxeion\n5.Exodos apo to oraio mas programma!!!");
you could define a variable for this string and if you put it at the top of the loop just once, then it would make more sense.
this is nice
catch (NumberFormatException e)
how about the parseInt call in case 5?
i noticed you removed the if condition and added a default to the switch.
what about using
while(c!=1)
i guess if it works then you're doing okay.
just thought i would make a few comments for the sake of discussion.
they aren't intended as criticism of any sort and may not apply with the current changes that you made.
kind regards
walken
no no!!! i want your opinion and of course not have problem :)
Well i have put Project h=new Project(); because i think i will use next for another thing... The main project work is to "play" with input output....
Well i have added while (c!=1) in my program but unfortunatelly the compiler sends two SAME errors :
variable line might not have been initialized (line 13)
variable line might not have been initialized (line 16)
why?I have understand that you use it in order program terminate after user press 1 in case 5... But it not works and i wonder why...
I think if change and this the program it will be excellent for now!
Thanks a lot...
the answer is sort of in reply #3.local variables aren't automatically initialized so you could take careof that error by setting the value of c equal to 0 likeint a,c=0;kind regards walken
i have done it.... the same again...
> i have done it.... the same again...
oops, sorry, i misread the error.
The error mentions the variable line
so line is what you need to initialize (also)
i refer to post #3
while(c!=1){
line = ReadThis.readLine();//maybe you left this out?
anyway, you can do it either way that works.
kind regards
walken
yes now it's ok!!!!
i have made the code more easy to read...
import java.io.*;
public class Project{
public static void main(String args[]) throws IOException{ //scanf arxi
Project h=new Project();
int a,c=0;
String line,arxi=("\n\n\n\n\n\t\t\t Dose arithmo apo 1 mexri 5\n1.Anoigma Arxeiou\n2.Provoli Arxeiou\n3.Eyresi Alpharithmitikou\n4.Sigkrisi Arxeion\n5.Exodos apo to oraio mas programma!!!");
System.out.println(arxi);
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader ReadThis = new BufferedReader(r);
while(c!=1){
line = ReadThis.readLine();
try{
a = Integer.parseInt(line);//scanf telos
}
catch (NumberFormatException e)
{ System.out.println("\t\t\tTo \""+line+"\" den einai apo 1 mexri 5");
System.out.println("\t\t\tDokimase xana, SOSTA ayti ti fora!!!");
continue;
}
switch (a) {
case 1:
System.out.println(arxi);
break;
case 2:
System.out.println(arxi);
break;
case 3:
System.out.println(arxi);
break;
case 4: System.out.println(" 4");
System.out.println(arxi);
break;
case 5: System.out.println("Eisai sigouros? An nai pata 1!");
line = ReadThis.readLine();
try{c = Integer.parseInt(line);}
catch (NumberFormatException e){
System.out.println(arxi);
continue;}
break;
default: System.out.println("\t\t\tTo \""+a+"\" den einai apo 1 mexri 5");
break;
}
}
}
}
Well for now the program is excellent, i have tested and tested and tested... no bugs at all!
Now i must sleep!
To tomorrow i will continue with the next things i must do!
Thanks for all!!!
And a happy new year!!!
sorry for more easy.... i mean easier.... anyway sorry for my bad english....
> sorry for more easy.... i mean easier.... anyway> sorry for my bad english....you're doing fine and your english is not bad.good luckwalken
Tip: Try to use more specific subject lines. The more meaningful your subject, the more meaningful your replies.
> Tip: Try to use more specific subject lines.
> The more meaningful your subject, the more meaningful
> your replies.
yeah, that's probably why this thread only has replies from one person.
I try to pick threads like this so I don't have to worry about my replies being meaningful, it sort of makes it easier for me.
kind regards
walken