# Combination of loops, arrays, methods, anyone? need help badly!

Printable View

• 03-25-2011, 04:28 AM
Maximuz313
Combination of loops, arrays, methods, anyone? need help badly!
Here's the Problem...

First you will be ask to input 2 consecutive numbers.

This set of number will then be added to your array or list.

Once these numbers are added to the list.

You will be asked to choose from a list of options.(press v for view, a for add etc..)

A - Add numbers to the list. (note: once you add numbers, it will automatically be added on the top of the array[0] or list)
V - View the list.
D - Delete numbers from the list.
U - Update or make some changes in the list.

-----------------------------------------------------------------------------------------------------------------------------

Once you have chosen the view option. You will have another set of options from the menu.

A(add) - The two consecutive numbers that you've added to the list will produce an ouput of num1 + num2 = sum.

S(subtract) - The two consecutive numbers that you've added to the list will produce an ouput of num1 - num2 = difference.

M(multiply) - The two consecutive numbers that you've added to the list will produce an ouput of num1 * num2 = product.

D(divide) - The two consecutive numbers that you've added to the list will produce an ouput of num1 / num2 = quotient.

-----------------------------------------------------------------------------------------------------------------------------

Conditions:

1. You can't add once the list is full (max of 10 sets).

2. You can't delete from the list if it is empty.

3. You must use methods in accomplishing this program.

-----------------------------------------------------------------------------------------------------------------------------
• 03-25-2011, 04:52 AM
Eranga
Ok, so what you've done so far?

This is forum, not a place to get done your coursework. So you must show some effort here.
• 03-25-2011, 05:00 AM
sunde887
As eranga said, this forum is to help you, not do it for you. Show us what you have, and we will tell you if you are going in the right direction. State small, work your way up.
• 03-25-2011, 05:07 AM
Maximuz313
Eranga... I'm very sorry for posting such stupid question, I guess I'm really desperate!
I'm new to Java programming and I'm stuck in what I've been doing... Again, sorry for my noobness! Anyway, here's my program and to be specific I'm stuck in inputting char and comparison statement.

Code:

```import java.io.*; import java.util.*; import java.util.Scanner; import java.lang.*; public class Finals{         public static void main (String []args){                 BufferedReader dataIn = new BufferedReader (new InputStreamReader (System.in));                 Scanner  scan = new Scanner(System.in);                 Operations operate = new Operations();                                                 int[] num1 = new int[10];                  int[] num2 = new int[10];                                 char m;                 int viewchoice = 0;                        int deletechoice = 0;                 int updatechoice = 0;                 int x = 0;                 int y = 0;                 int w = 0;                 int z = 1;                                                                                 /*-----------------------STAR OF THE PROGRAM--------------------------------*/                                                                        System.out.print("Enter 1st number:");                         try{                                 num1[x]=Integer.parseInt (dataIn.readLine());                         }catch (IOException p) {                                 System.out.println ("ERROR");                         }                 System.out.print("Enter 2nd number:");                         try{                                 num2[y]=Integer.parseInt (dataIn.readLine());                         }catch (IOException p) {                                 System.out.println ("ERROR");                         }                                                                                                                                                                                        System.out.print ("Set1: " + num1[x] + " and " + num2[y]);                                                                                         while (menuchoice!= 'e'){      <---------ERROR HERE                                         System.out.println ("\n\t\tMenu");                         System.out.println ("\n[V]iew");                         System.out.println ("[A]dd");                         System.out.println ("[D]elete");                         System.out.println ("[U]pdate");                         System.out.println ("[E]XIT");                                 System.out.print ("Choose From the option: ");                                                 menuchoice = new Scanner(System.in).next().charAt(0); <--THIS ONE TOO----                                         if (menuchoie==v){ <-- another error here                         view method                       }                       if (menuchoie==a){ <-- another error here                         add method                         }                       if (menuchoie==u){ <-- another error here                         update method here                           }                       if (menuchoie==d){ <-- another error here                         delelte method here                         }                 }         } }```

------------------THE METHODS are not the actual methods, just pseudocode-----------------

I guess I really should go back to the basics and spend more time practicing Java.
• 03-25-2011, 05:14 AM
sunde887
Use only the scanner or the stream for getting user input(scanner is probably better)

When comparing strings you need to use .equals.

It looks like you are having a lot of trouble, what are you stuck on?
• 03-26-2011, 07:05 PM
Maximuz313
Thanks for the advice sunde... I'm stuck on the part where I have to get a char value from from the user and assign it the the menuchoice(char variable)... I'm also stuck on the part where I have to compare the menuchoice with the letter a -add, v-view, etc[if statements such as if(menuchoice==v)]... sorry for being such a noob, but I'm going to do my best to become a pro ^_^... Anyways, I'll do your advice. By the way, do I have to put apostrophe on the letter ex: 'v'.
• 03-26-2011, 07:28 PM
ozzyman
Code:

```//create a scanner object Scanner sc = new System.in; //ask user to type some input String userInput = sc.nextLine(); //some validation in case the user gives bad input boolean validInput = userInput.matches("[a-zA-Z]{1}"); if (validInput == true) {     char chosenCommand = userInput.charAt(0);     menuChoice(chosenCommand); } else {     //error: bad input }```

Code:

```public void menuChoice(char c) {     switch (c) {         case "A":             //add             break;         case "V":             //view             break;     } }```
• 03-28-2011, 12:03 PM
Eranga
Quote:

Originally Posted by Maximuz313
Eranga... I'm very sorry for posting such stupid question, I guess I'm really desperate!

Don't really worry about it, but that kind of things add a value to your post. And we can comment easily too.

Please use code tags also when you are posting again in the forum. If you don't know how to do that, check on my forum signature. You can find a relevant FAQ link.
• 03-28-2011, 12:06 PM
Eranga
Maximuz313, I can see in your post that you've mentioned something like 'ERROR HERE'. What that really means, you end-up with an error when you are executing your code?