Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #30059 > unrolled thread
| Started by | Abraar Ahmed <abraar93@gmail.com> |
|---|---|
| First post | 2014-10-05 15:40 -0700 |
| Last post | 2014-10-10 16:01 +0000 |
| Articles | 9 — 6 participants |
Back to article view | Back to comp.lang.java.programmer
Help writing a program using a switch statement Abraar Ahmed <abraar93@gmail.com> - 2014-10-05 15:40 -0700
Re: Help writing a program using a switch statement Knute Johnson <eternal@knutejohnson.com> - 2014-10-05 17:18 -0700
Re: Help writing a program using a switch statement Arne Vajhøj <arne@vajhoej.dk> - 2014-10-10 22:02 -0400
Re: Help writing a program using a switch statement Knute Johnson <eternal@knutejohnson.com> - 2014-10-10 20:27 -0700
Re: Help writing a program using a switch statement Arne Vajhøj <arne@vajhoej.dk> - 2014-10-11 09:43 -0400
Re: Help writing a program using a switch statement Knute Johnson <eternal@knutejohnson.com> - 2014-10-11 07:59 -0700
Re: Help writing a program using a switch statement rossum <rossum48@coldmail.com> - 2014-10-06 09:38 +0100
Re: Help writing a program using a switch statement Roedy Green <see_website@mindprod.com.invalid> - 2014-10-06 20:25 -0700
Re: Help writing a program using a switch statement Henry Reardon <No_contact@example.com> - 2014-10-10 16:01 +0000
| From | Abraar Ahmed <abraar93@gmail.com> |
|---|---|
| Date | 2014-10-05 15:40 -0700 |
| Subject | Help writing a program using a switch statement |
| Message-ID | <8dcbb682-a1c4-4e43-b506-093de204ae87@googlegroups.com> |
Write a program using switch statement to allow users to choose an item of a menu. For example, the menu looks like the following.
File menu
1. Open
2. Close
3. Save
4. Exit
Enter your choice:
If you type 1, then your program should print out "Open is selected" (double quotes not included).
Notice:
(1) Using Scanner to get user choice;
(2) If the number user input is not in {1,2,3,4}, your program should let user know that.
Here's what I have:
public static void main (String[] args) {
int user = 1
switch (1) {
case value: 1
System.out.println ("Open is selected");
break;
case value: 2
System.out.println ("Close is selected");
break;
case value: 3
System.out.println ("Save is selected")
break;
System.out.println ("Exit is selected")
default:
values_not_caught_above;
}
Can someone tell me if I'm headed in the right direction?
Any guidance would be much appreciated, thank you.
[toc] | [next] | [standalone]
| From | Knute Johnson <eternal@knutejohnson.com> |
|---|---|
| Date | 2014-10-05 17:18 -0700 |
| Message-ID | <m0sn3e$33q$1@dont-email.me> |
| In reply to | #30059 |
On 10/5/2014 15:40, Abraar Ahmed wrote:
> Write a program using switch statement to allow users to choose an item of a menu. For example, the menu looks like the following.
>
> File menu
> 1. Open
> 2. Close
> 3. Save
> 4. Exit
> Enter your choice:
> If you type 1, then your program should print out "Open is selected" (double quotes not included).
>
> Notice:
> (1) Using Scanner to get user choice;
> (2) If the number user input is not in {1,2,3,4}, your program should let user know that.
>
> Here's what I have:
>
> public static void main (String[] args) {
>
> int user = 1
>
> switch (1) {
> case value: 1
> System.out.println ("Open is selected");
> break;
> case value: 2
> System.out.println ("Close is selected");
> break;
> case value: 3
> System.out.println ("Save is selected")
> break;
> System.out.println ("Exit is selected")
> default:
> values_not_caught_above;
>
> }
>
> Can someone tell me if I'm headed in the right direction?
>
> Any guidance would be much appreciated, thank you.
>
Since this is homework, you need to look at the Java Language
Specification, chapter 14.11 Switch Statement. Everything you need to
know is there.
http://docs.oracle.com/javase/specs/jls/se8/html/index.html
knute...
--
Knute Johnson
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2014-10-10 22:02 -0400 |
| Message-ID | <54388fd2$0$292$14726298@news.sunsite.dk> |
| In reply to | #30060 |
On 10/5/2014 8:18 PM, Knute Johnson wrote:
> On 10/5/2014 15:40, Abraar Ahmed wrote:
>> Write a program using switch statement to allow users to choose an
>> item of a menu. For example, the menu looks like the following.
>> switch (1) {
>> case value: 1
>> System.out.println ("Open is selected");
>> break;
>> case value: 2
>> System.out.println ("Close is selected");
>> break;
>> case value: 3
>> System.out.println ("Save is selected")
>> break;
>> System.out.println ("Exit is selected")
>> default:
>> values_not_caught_above;
>>
>> }
> Since this is homework, you need to look at the Java Language
> Specification, chapter 14.11 Switch Statement. Everything you need to
> know is there.
>
> http://docs.oracle.com/javase/specs/jls/se8/html/index.html
JLS is good for many things, but learning basic Java syntax is not
one if them.
The Java tutorial would be a lot easier to learn from.
http://docs.oracle.com/javase/tutorial/java/index.html
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
Arne
[toc] | [prev] | [next] | [standalone]
| From | Knute Johnson <eternal@knutejohnson.com> |
|---|---|
| Date | 2014-10-10 20:27 -0700 |
| Message-ID | <m1a82e$c31$1@dont-email.me> |
| In reply to | #30073 |
On 10/10/2014 19:02, Arne Vajhøj wrote:
> On 10/5/2014 8:18 PM, Knute Johnson wrote:
>> On 10/5/2014 15:40, Abraar Ahmed wrote:
>>> Write a program using switch statement to allow users to choose an
>>> item of a menu. For example, the menu looks like the following.
>
>>> switch (1) {
>>> case value: 1
>>> System.out.println ("Open is selected");
>>> break;
>>> case value: 2
>>> System.out.println ("Close is selected");
>>> break;
>>> case value: 3
>>> System.out.println ("Save is selected")
>>> break;
>>> System.out.println ("Exit is selected")
>>> default:
>>> values_not_caught_above;
>>>
>>> }
>
>> Since this is homework, you need to look at the Java Language
>> Specification, chapter 14.11 Switch Statement. Everything you need to
>> know is there.
>>
>> http://docs.oracle.com/javase/specs/jls/se8/html/index.html
>
> JLS is good for many things, but learning basic Java syntax is not
> one if them.
>
> The Java tutorial would be a lot easier to learn from.
>
> http://docs.oracle.com/javase/tutorial/java/index.html
>
> http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
>
> Arne
>
>
>
There are some good examples in the JLS for the switch statement though.
That's why I told him to look there.
--
Knute Johnson
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2014-10-11 09:43 -0400 |
| Message-ID | <543933f5$0$301$14726298@news.sunsite.dk> |
| In reply to | #30074 |
On 10/10/2014 11:27 PM, Knute Johnson wrote:
> On 10/10/2014 19:02, Arne Vajhøj wrote:
>> On 10/5/2014 8:18 PM, Knute Johnson wrote:
>>> On 10/5/2014 15:40, Abraar Ahmed wrote:
>>>> Write a program using switch statement to allow users to choose an
>>>> item of a menu. For example, the menu looks like the following.
>>
>>>> switch (1) {
>>>> case value: 1
>>>> System.out.println ("Open is selected");
>>>> break;
>>>> case value: 2
>>>> System.out.println ("Close is selected");
>>>> break;
>>>> case value: 3
>>>> System.out.println ("Save is selected")
>>>> break;
>>>> System.out.println ("Exit is selected")
>>>> default:
>>>> values_not_caught_above;
>>>>
>>>> }
>>
>>> Since this is homework, you need to look at the Java Language
>>> Specification, chapter 14.11 Switch Statement. Everything you need to
>>> know is there.
>>>
>>> http://docs.oracle.com/javase/specs/jls/se8/html/index.html
>>
>> JLS is good for many things, but learning basic Java syntax is not
>> one if them.
>>
>> The Java tutorial would be a lot easier to learn from.
>>
>> http://docs.oracle.com/javase/tutorial/java/index.html
>>
>> http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
>
> There are some good examples in the JLS for the switch statement though.
It has 3 examples:
* some code that works in C/C++ but does not work in Java
* some code showing fall through
* some normal code
Compare that to the tutorial that has 5 examples:
* some normal code
* some code showing how it would look like using if else if
* some code showing fall through
* some code showing multi labels
* some code showing switch on strings
Arne
[toc] | [prev] | [next] | [standalone]
| From | Knute Johnson <eternal@knutejohnson.com> |
|---|---|
| Date | 2014-10-11 07:59 -0700 |
| Message-ID | <m1bgl2$m76$1@dont-email.me> |
| In reply to | #30075 |
On 10/11/2014 06:43, Arne Vajhøj wrote:
> On 10/10/2014 11:27 PM, Knute Johnson wrote:
>> On 10/10/2014 19:02, Arne Vajhøj wrote:
>>> On 10/5/2014 8:18 PM, Knute Johnson wrote:
>>>> On 10/5/2014 15:40, Abraar Ahmed wrote:
>>>>> Write a program using switch statement to allow users to choose an
>>>>> item of a menu. For example, the menu looks like the following.
>>>
>>>>> switch (1) {
>>>>> case value: 1
>>>>> System.out.println ("Open is selected");
>>>>> break;
>>>>> case value: 2
>>>>> System.out.println ("Close is selected");
>>>>> break;
>>>>> case value: 3
>>>>> System.out.println ("Save is selected")
>>>>> break;
>>>>> System.out.println ("Exit is selected")
>>>>> default:
>>>>> values_not_caught_above;
>>>>>
>>>>> }
>>>
>>>> Since this is homework, you need to look at the Java Language
>>>> Specification, chapter 14.11 Switch Statement. Everything you need to
>>>> know is there.
>>>>
>>>> http://docs.oracle.com/javase/specs/jls/se8/html/index.html
>>>
>>> JLS is good for many things, but learning basic Java syntax is not
>>> one if them.
>>>
>>> The Java tutorial would be a lot easier to learn from.
>>>
>>> http://docs.oracle.com/javase/tutorial/java/index.html
>>>
>>> http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
>>
>> There are some good examples in the JLS for the switch statement though.
>
> It has 3 examples:
> * some code that works in C/C++ but does not work in Java
> * some code showing fall through
> * some normal code
>
> Compare that to the tutorial that has 5 examples:
> * some normal code
> * some code showing how it would look like using if else if
> * some code showing fall through
> * some code showing multi labels
> * some code showing switch on strings
>
> Arne
OK I concede, you should have answered him not me.
--
Knute Johnson
[toc] | [prev] | [next] | [standalone]
| From | rossum <rossum48@coldmail.com> |
|---|---|
| Date | 2014-10-06 09:38 +0100 |
| Message-ID | <o1l43a5npu7tr7ke9u3fgac3r8arl3ap7e@4ax.com> |
| In reply to | #30059 |
On Sun, 5 Oct 2014 15:40:47 -0700 (PDT), Abraar Ahmed
<abraar93@gmail.com> wrote:
> Write a program using switch statement to allow users to choose an item of a menu. For example, the menu looks like the following.
>
>File menu
>1. Open
>2. Close
>3. Save
>4. Exit
>Enter your choice:
>If you type 1, then your program should print out "Open is selected" (double quotes not included).
>
>Notice:
>(1) Using Scanner to get user choice;
>(2) If the number user input is not in {1,2,3,4}, your program should let user know that.
>
>Here's what I have:
>
>public static void main (String[] args) {
>
>int user = 1
>
>switch (1) {
>case value: 1
>System.out.println ("Open is selected");
>break;
>case value: 2
>System.out.println ("Close is selected");
>break;
>case value: 3
>System.out.println ("Save is selected")
>break;
>System.out.println ("Exit is selected")
>default:
>values_not_caught_above;
>
>}
>
>Can someone tell me if I'm headed in the right direction?
>
>Any guidance would be much appreciated, thank you.
Read the question you are answering. It says:
(1) Using Scanner to get user choice;
Your answer does not, so far, use Scanner. That is one error.
As Knute has pointed out, there are errors in your use of the switch
statement. To see one of them, run through your code on paper to see
what happens if the user selects '2'. Be careful to follow exactly
what your code says, not what you meant it to say. A great many bugs
live in the difference between the two.
rossum
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2014-10-06 20:25 -0700 |
| Message-ID | <f8n63al6rk735tr1pcuf5ipd5l9k0ve7v7@4ax.com> |
| In reply to | #30059 |
On Sun, 5 Oct 2014 15:40:47 -0700 (PDT), Abraar Ahmed <abraar93@gmail.com> wrote, quoted or indirectly quoted someone who said : >case value: 1 there is no such keyword as value. See http://mindprod.com/jgloss/switch.html for an example. -- Roedy Green Canadian Mind Products http://mindprod.com There are only two industries that refer to their customers as users. ~ Edward Tufte (born: 1942 age: 72)
[toc] | [prev] | [next] | [standalone]
| From | Henry Reardon <No_contact@example.com> |
|---|---|
| Date | 2014-10-10 16:01 +0000 |
| Message-ID | <XnsA3C27A63A34B0Nocontactexamplecom@213.239.209.88> |
| In reply to | #30059 |
Abraar Ahmed <abraar93@gmail.com> wrote in
news:8dcbb682-a1c4-4e43-b506-093de204ae87@googlegroups.com:
> Write a program using switch statement to allow users to choose an
> item of a menu. For example, the menu looks like the following.
>
> File menu
> 1. Open
> 2. Close
> 3. Save
> 4. Exit
> Enter your choice:
> If you type 1, then your program should print out "Open is selected"
> (double quotes not included).
>
> Notice:
> (1) Using Scanner to get user choice;
> (2) If the number user input is not in {1,2,3,4}, your program should
> let user know that.
>
> Here's what I have:
>
> public static void main (String[] args) {
>
> int user = 1
>
> switch (1) {
> case value: 1
> System.out.println ("Open is selected");
> break;
> case value: 2
> System.out.println ("Close is selected");
> break;
> case value: 3
> System.out.println ("Save is selected")
> break;
> System.out.println ("Exit is selected")
> default:
> values_not_caught_above;
>
> }
>
> Can someone tell me if I'm headed in the right direction?
>
> Any guidance would be much appreciated, thank you.
A better place to ask homework questions might be comp.lang.java.help.
There is a lot of spam there these days but I *think* a few of the Java
gurus still hang out there to help beginners with their questions.
~~
Henry
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web