1. Member
Join Date
Mar 2011
Posts
3
Rep Power
0

## 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.

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

2. 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.

3. 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.

4. Member
Join Date
Mar 2011
Posts
3
Rep Power
0
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.

Java Code:
```import java.io.*;
import java.util.*;
import java.util.Scanner;
import java.lang.*;

public class Finals{

public static void main (String []args){
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{
}catch (IOException p) {
System.out.println ("ERROR");
}
System.out.print("Enter 2nd number:");
try{
}catch (IOException p) {
System.out.println ("ERROR");
}

System.out.print ("Set1: " + num1[x] + " and " + num2[y]);

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
}
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.
Last edited by Eranga; 03-28-2011 at 12:04 PM. Reason: code tags added

5. 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?

6. Member
Join Date
Mar 2011
Posts
3
Rep Power
0
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'.
Last edited by Maximuz313; 03-26-2011 at 06:09 PM.

7. Java 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);
} else {
}```

Java Code:
```public void menuChoice(char c) {
switch (c) {
case "A":
break;
case "V":
//view
break;
}
}```

8. 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.

9. 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?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•