This program should have a menu:it can use a switch method ...

MENU:
[1]=ACCEPT;
[2]=DISPLAY THE SMALLEST;
[3]=DISPLAY THE LARGEST;
[4]=DISPLAY THE SUM;
[5]=DISPLAY ALL;
[6]=EXIT;

Could someone help me to this problem,and this program that i do i still encountering some errors..please do try to help me..

import java.util.*;

public class List3{
private final int [] arr;
private final int MAX=10;

    public List3(){
    arr=new int[MAX];
    }//ACCEPT 10 NUMBERS
    public void accept(){
    Scanner console=new Scanner(System.in);
    for(int i=0;i<arr.length;i++)
    arr[i]=console.nextInt();
    }
        public int smallest(){
        int smallest=0;
        for(int i=0;i<arr.length;i++)
        smallest=Math.min(smallest,arr[i]);
        return smallest;
        }   
            public int largest(){
            int largest=0;
            for(int i=0;i<arr.length;i++)
            largest=Math.max(largest,arr[i]);
            return largest;
            }
        public int sum(){
        int sum=0;
        for(int i=0;i<arr.length;i++)
        sum+=arr[i];
        return sum;
        }//main
        public static void main(String [] args){

    do 
    {   switch(userChoice){
        while( int i=0;i<arr.length;i++)
        }
    case 1:System.out.println("Enter the smallest number for list1");
             System.out.println("Enter the largest number for list1");
             break;
    case 2:System.out.println("Smallest of list1="+list1.smallest());
             System.out.println("Largest of list1="+list1.largest());
             break;
    case 3:System.out.println("Smallest of list2="+list2.smallest());
             System.out.println("Largest of list2="+list2.largest());
             break;
    case 4:public void display(){
             for(int i=0;i<arr.length;i++)
             System.out.println(arr[i]+"");
             System.out.println();
             }
    case 5:2:System.out.println("Smallest of list1="+list1.smallest());
             System.out.println("Largest of list1="+list1.largest());
             break;
             System.out.println("Smallest of list2="+list2.smallest());
             System.out.println("Largest of list2="+list2.largest());
             breal;

    case 6: //is should be the quit i dont how to make it

I hope someone can help me figured it out..
thanks all..
churva....

Recommended Answers

All 15 Replies

You need to take in some form of user input. Right now you are just doing it while populating the array. In your do while loop, ask the user to input a number, then use your switch statement on the number received and process accordingly. Also remember to use code blocks when posting code (Yours is terrible to try and read)!

sorry but can you please help how to make it right i really dont have any idea how to do it..
thanks

You already know how to read information from the console. Take and assign the number you receive into a local variable. Instead of using the while loop that you have:

while( int i=0;i<arr.length;i++)

Make it something like:

while(userChoice != EXIT_LOOP_NUMBER)

You also have some errors with your switch statement (as you have not included the full code.

its still have an error here...i dont why i already change it ..
check it..
please the code and post it here without and error because i was really stack in here..
thanks..

import java.util.*;

public class List3{
private final int [] arr;
private final int MAX=10;

    public List3(){
    arr=new int[MAX];
    }//ACCEPT 10 NUMBERS
    public void accept(){
    Scanner console=new Scanner(System.in);
    for(int i=0;i<arr.length;i++)
    arr[i]=console.nextInt();
    }
        public int smallest(){
        int smallest=0;
        for(int i=0;i<arr.length;i++)
        smallest=Math.min(smallest,arr[i]);
        return smallest;
        }   
            public int largest(){
            int largest=0;
            for(int i=0;i<arr.length;i++)
            largest=Math.max(largest,arr[i]);
            return largest;
            }
        public int sum(){
        int sum=0;
        for(int i=0;i<arr.length;i++)
        sum+=arr[i];
        return sum;
        }//main
        public static void main(String [] args){

    do 
    {        
    while((userChoice != EXIT_LOOP_NUMBER))

        switch(userChoice);
    case 1:System.out.println("Enter the smallest number for list1");
             System.out.println("Enter the largest number for list1");
             break;
    case 2:System.out.println("Smallest of list1="+list1.smallest());
             System.out.println("Largest of list1="+list1.largest());
             break;
    case 3:System.out.println("Smallest of list2="+list2.smallest());
             System.out.println("Largest of list2="+list2.largest());
             break;
    case 4:public void display(){
             for(int i=0;i<arr.length;i++)
             System.out.println(arr[i]+"");
             System.out.println();
             }
             break;

    case 5:2:System.out.println("Smallest of list1="+list1.smallest());
             System.out.println("Largest of list1="+list1.largest());
             break;
             System.out.println("Smallest of list2="+list2.smallest());
             System.out.println("Largest of list2="+list2.largest());
             break;

    case 6: //is should be the quit i dont how to make it

                    please check..and post the correct program...

The structure of your do while loop is incorrect.

do
{
   //execute code here
} while( boolean_condition )

So you would need to do something like

int userInput = -1;
do
{
   printChoicesMenu();
   userInput = value_received_from_menu;
   //execute code here
   switch(userInput)
   {
       ...
       default:
            break;
   }
} while(userInput != 6)

And please use code tags when posting code!!!!

please just do edit my program... i really dont have any idea what to do....i was trying to edit it using using idea but still maybe i was just wrong in placing it..please do help me...
thanks....

Sorry Cheif, we don't give out homework answers. The help I gave you should be enough to help you get the job done. Most of your code looks OK (from what I can tell without code tags?!?!), its just the section on the loop that you have some problems with.

All loops are essentially boolean conditions, or a finite number of times (you do have infinite loops but don't worry about those right now). Think about how you would approach this if you where to physically do it. Write down the steps you would follow, and try and get your program to follow similar logic.

If you are still stuck, post what you are stuck on, what you think is wrong or why it is wrong.

Why do you stop your code at case 6? Don't you close the main and class right parenthesis.
Anyway, given the instructions, place the switch statement, with the case block in the do while loop.

You will need to define the userChoice and give it value from the keyboard:

do {
 print_menu
 userChoice = take_from_keyboar

 switch () {

 }

} while();

yeah i know that,but what i just dont how put it in there..i dont how to arrange in that way.....please do help me...edit my code and post it...please i was really stack in here.....thanks....

import java.util.*;
 
public class List3{
private final int [] arr;
private final int MAX=10;

	public List3(){
	arr=new int[MAX];
	}//ACCEPT 10 NUMBERS
	public void accept(){
	Scanner console=new Scanner(System.in);
	for(int i=0;i<arr.length;i++)
	arr[i]=console.nextInt();
	}
		public int smallest(){
		int	smallest=0;
		for(int i=0;i<arr.length;i++)
		smallest=Math.min(smallest,arr[i]);
		return smallest;
		}	
			public int largest(){
			int largest=0;
			for(int i=0;i<arr.length;i++)
			largest=Math.max(largest,arr[i]);
			return largest;
			}
		public int sum(){
		int sum=0;
		for(int i=0;i<arr.length;i++)
		sum+=arr[i];
		return sum;
		}//main
		public static void main(String [] args){
		
 
		 do {
 print_menu
 userChoice = take_from_keyboar;

 switch (userChoice) {

 }


	case 1: System.out.println("Enter the smallest number for list1");
			 System.out.println("Enter the largest number for list1");
			 break;
	case 2: System.out.println("Smallest of list1="+list1.smallest());
			 System.out.println("Largest of list1="+list1.largest());
			 break;
	case 3: System.out.println("Smallest of list2="+list2.smallest());
			 System.out.println("Largest of list2="+list2.largest());
			 break;
	case 4: public void display(){
			 for(int i=0;i<arr.length;i++)
			 System.out.println(arr[i]+"");
			 System.out.println();
			 }
			 break;
			 
	case 5: System.out.println("Smallest of list1="+list1.smallest());
			 System.out.println("Largest of list1="+list1.largest());
			 break;
			 System.out.println("Smallest of list2="+list2.smallest());
			 System.out.println("Largest of list2="+list2.largest());
			 break;

	case 6: //is should be the quit i dont how to make it
	
					}
}

please people do check it.

yeah i know that,but what i just dont how put it in there..i dont how to arrange in that way.....please do help me...edit my code and post it...please i was really stack in here.....thanks....

The code is already edited:

do {
  // print the menu. Use System.out.println

  int userChoice = // use the scanner class to get the input
  
  switch (userChoice) {
    // add your code here
    // add the case statements you have written
  }

} while (userChoice != EXIT_LOOP_NUMBER)

If you get errors, which you will, post them here with your new code.
Since the methods were declared non static, you will need to create a new instance of the class:

main() {

List3 list3 = new List3();
// use the list3 instance to call the methods in the cases. The arr array declared and initialized in the constructor would be the you would be using with the methods. No need to create a new one inside the main.

do {
  // print the menu. Use System.out.println

  int userChoice = // use the scanner class to get the input
  
  switch (userChoice) {
    // add the case statements you have written
    case 1:
       ....
       break;
    ....
  }

} while (userChoice != EXIT_LOOP_NUMBER)

}

can some ont help me in this code i have posted i was really stuck in here....

import java.util.*;
 
public class List3{
private final int [] arr;
private final int MAX=10;

	public List3(){
	arr=new int[MAX];
	}//ACCEPT 10 NUMBERS
	public void accept(){
	Scanner console=new Scanner(System.in);
	for(int i=0;i<arr.length;i++)
	arr[i]=console.nextInt();
	}
		public int smallest(){
		int	smallest=0;
		for(int i=0;i<arr.length;i++)
		smallest=Math.min(smallest,arr[i]);
		return smallest;
		}	
			public int largest(){
			int largest=0;
			for(int i=0;i<arr.length;i++)
			largest=Math.max(largest,arr[i]);
			return largest;
			}
		public int sum(){
		int sum=0;
		for(int i=0;i<arr.length;i++)
		sum+=arr[i];
		return sum;
		}//main
		public static void main(String [] args){
		
 
		 do {
 print_menu
 userChoice = take_from_keyboar;

 switch (userChoice) {

 }


	case 1: System.out.println("Enter the smallest number for list1");
			 System.out.println("Enter the largest number for list1");
			 break;
	case 2: System.out.println("Smallest of list1="+list1.smallest());
			 System.out.println("Largest of list1="+list1.largest());
			 break;
	case 3: System.out.println("Smallest of list2="+list2.smallest());
			 System.out.println("Largest of list2="+list2.largest());
			 break;
	case 4: public void display(){
			 for(int i=0;i<arr.length;i++)
			 System.out.println(arr[i]+"");
			 System.out.println();
			 }
			 break;
			 
	case 5: System.out.println("Smallest of list1="+list1.smallest());
			 System.out.println("Largest of list1="+list1.largest());
			 break;
			 System.out.println("Smallest of list2="+list2.smallest());
			 System.out.println("Largest of list2="+list2.largest());
			 break;

	case 6: //is should be the quit i dont how to make it
	
					}
}

please people do check it.

print_menu and take_from_keyboar are not java commands. Use your imagination.
And post the errors that you get.

Thanks i think its a good help for me..i now get it...

but i just have to try it still, but atleast i have now the idea how to ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.