Results 1 to 9 of 9
  1. #1
    Maximuz313 is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default 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. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

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

    Default

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

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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. #6
    Maximuz313 is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    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. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    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);
        menuChoice(chosenCommand);
    } else {
        //error: bad input
    }

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

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Maximuz313 View Post
    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. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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?

Similar Threads

  1. How to use arrays and loops?
    By asadzarrar in forum New To Java
    Replies: 1
    Last Post: 10-31-2010, 11:04 PM
  2. Printing Two Dimensional Arrays with for loops
    By mcnam4119 in forum JCreator
    Replies: 3
    Last Post: 10-06-2010, 05:27 AM
  3. A few questions about arrays and loops
    By Jamison5213 in forum New To Java
    Replies: 1
    Last Post: 12-22-2009, 05:59 AM
  4. [SOLVED] Arrays/for-loops (easy) need help.
    By Zebra in forum New To Java
    Replies: 7
    Last Post: 04-24-2008, 04:03 AM
  5. Some problems with arrays and loops
    By BHCluster in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 12:24 PM

Posting Permissions

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