• Post Reply Bookmark Topic Watch Topic
  • New Topic

End of my program  RSS feed

 
Ruth Jordan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm new with programming and I'm trying to do following transformation but I really find it very difficult, probably because I don't understand it:







This code works, but nevertheless the following one doesn't:




I'm sure this way is not the correct one to get out of my program, I think I need to follow/link the instruction of openTPV,



but I don't know how to do it, I have already checked it with,



but it doesn't work.



What is what I'm doing wrong?

Thank you for your help and explanation.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

This should not even compile.
The word continue is a keyword in Java. You can't use it as a variable name.

Ruth Jr wrote:(...) but it doesn't work.

Please, read this ItDoesntWorkIsUseless (← this is a link).
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Carefully think about the logic in this piece of code:

What does it say here:

If n is not equal to 'Y' and also not equal to 'y', then print " ---End of session ---".
Else, if n is not equal to 'N' and also not equal to 'n', then call openTPV().
Else, if n is not equal to 'N', and not equal to 'n', and not equal to 'Y', and not equal to 'y', print "You have pressed an incorrect key, please try again."

Especially think about the "not" that I have made bold in the description above. Is this really what you meant?

Imagine what would happen if someone entered 'Y' or 'N'.
 
Ruth Jordan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again,

Thank you very much for you reply.

Paweł Baczyński, yes it compiles because I have made a great error with translations (spanish into english), the real code I use is following:



I should translate seguir into follow instead of continue, sorry, I didn't realise.

And also sorry about using it doesn't work.


Jesper de Jong, thank you very very much, I'm also a little bit ashamed because, it's so easy but I could'n see that!. The result is:


I have changed the order of the if clause because I received ---End of session --- twice, so now I can go out of my program perfectly.


Thank you again for your help.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, that works. But the logic still looks a bit hard. Why not make it simple and straightforward? Like this:

See how much easier this is to understand, without all the "not"s:

If n is equal to 'Y' or 'y', then print " ---End of session ---".
Else, if n is equal to 'N' or 'n', then call openTPV().
Else (if n is something else), then print "You have pressed an incorrect key, please try again."

Note that in the last else, you do not need to check with another if-statement that n is not equal to 'Y', 'y', 'N' or 'n'. You already know that it's not equal to one of those letters, because those cases have already been handled by the other two ifs.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ruth Jordan,

Another way to approach could be to treat as a String

If you don't find this way clearer, at least you can know it as an existing optional way to solve it.
 
Ruth Jordan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again,

Thank you very much for your posts.

Liutauras Vilda wrote:

Another way to approach could be to treat as a String


Thank you very much, but I'm afraid I'm not yet capable of understanding the process of this structure, I will study that more carefully in the next weeks.

Jesper de Jong wrote:
Ok, that works. But the logic still looks a bit hard. Why not make it simple and straightforward?....



Jesper thank you very much, I have been trying it because the code looks really fantastic, but the point is that I have a problem when I execute this code:

With the last code, the hard one:



I receive following answers:

>>

7
¿Está seguro?(S/N)
s
--- Fin de la sesion ---
BUILD SUCCESSFUL (total time: 4 seconds)




>>

7
¿Está seguro?(S/N)
n

************************************************

* 1.-......

* 2.-......

* 7.-Terminar sesion
************************************************

>>

7
¿Está seguro?(S/N)
b
Ha marcado una opción incorrecta, por favor marque de nuevo.


************************************************

* 1.-......

* 2.-......

* 7.-Terminar sesion
************************************************

This is the correct structure of answers that I need.


Nevertheless if I try the new code with a logic that doesn't look so hard, I obtain following answers:




7
¿Está seguro?(S/N)
s
--- Fin de la sesion ---
BUILD SUCCESSFUL (total time: 4 seconds)

7
¿Está seguro?(S/N)
n

************************************************

* 1.-......

* 2.-......

* 7.-Terminar sesion
************************************************

>>

7
¿Está seguro?(S/N)
b
************************************************

* 1.-......

* 2.-......

* 7.-Terminar sesion
************************************************

That means that my last option , when I select another key (different from s or n), is not working as desired. So please, what is wrong here between these 2 structures?.


Thanks.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you should move all that code out of the case: It should be a method in its own right.Move all the codeout of the case: into the terminarProgram method. Now try another switch inside terminarProgram. Did you know that in Java7+ you can have switch statements using StringsNote I have broken line 12 into two lines otherwise it would have been too long. If you use two cases together you should comment that fallthrough is wanted.
 
Ruth Jordan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have tried it such way but I obtained following red exclamation mark:

string in switch are not supported in -source 1.5 (use -source7 or higher to enable strings in switch)

It refers to my netbeans version?.

Thanks.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that refers to your Java version, or more precisely the version of Java which Netbeans is using. I expect that Netbeans has a way for you to tell it what version of Java to use but not ever having used Netbeans I don't know what it is.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java5 was superseded 9 years ago. Why are you still using it?
 
Ruth Jordan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ups! First time I installed a program to work with java was, ...I think 4 years ago, eclipse. Then I left java..... 1 + 1/2 years ago I installed net beans, I thought the java version was also renewed with the new installation, I suppose it was not.

My computer has no acces to internet, it's only a little computer used for programming, how could I update the version of java?. Is it possible to download a new version from anywhere?.

Thanks.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ruth Jordan wrote:Is it possible to download a new version from anywhere?
Yes, you can download it from here (<-- link)
Which OS you're using?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!