Results 1 to 10 of 10

Thread: HW help

  1. #1
    ryn21 is offline Member
    Join Date
    Oct 2008
    Posts
    16
    Rep Power
    0

    Default HW help

    I have a hw assignment that i have been working on for days and can't figure out what to do. I am reading a text with numbers with the scanner Filereader and i can't figure out how to add the numbers and so on. Here are the instructions i was givin. Any help would be very appreciated.

    The program will read the file and then present the user with a menu. The user will then make a menu choice. Be sure to validate the user's input. The user can continue to use the program until the user opts to end the program. Ending the program is a menu choice.

    Here is the output:

    Make a selection from the list
    1. Get the largest value
    2. Get the smallest value
    3. Get the sum of the values
    4. Get the average
    5. Get the number of values entered
    6. End this program

    Enter your choice --> 1
    The largest value is 1000

  2. #2
    ryn21 is offline Member
    Join Date
    Oct 2008
    Posts
    16
    Rep Power
    0

    Default

    this is what i got so far
    Java Code:
    #                                     
    # import java.util.*;  
    # import java.io.*;  
    #   
    # public class Lab5  
    # {  
    #       
    #             static Scanner console = new Scanner(System.in);  
    #       
    #     public static void main ( String[] args)  
    #                                     throws FileNotFoundException  
    #   
    #                                                                                       
    #      {  
    #   
    #          Scanner inFile = new Scanner(new FileReader("C:\\Random.txt"));  
    #            
    #          int a;  
    #          String menu;  
    #            
    #             menu = ("Make a selection from the list" + "/n"  
    #             +"1. Get the largest value" + "/n"  
    #             +"2. Get the smallest value" + "/n"  
    #             +"3. Get the sum of values" + "/n"  
    #             +"4. Get the average" + "/n"  
    #             +"5. Get the number of values entered" + "/n"  
    #             +"6. End this program" + "/n"  
    #             +"Enter your choice --->" + "/n");  
    #               
    #             a = console.nextInt();  
    #            
    #            while (inFile.hasNext())  
    #              {  
    #                 switch (a)  
    #                 {  
    #                   case 1:  
    #                     
    #                case 2:  
    #                       
    #                     break;  
    #           
    #                     case 4:  
    #                        
    #                     case 5:  
    #                        
    #                     case 6:  
    #                         System.out.print("Goodbye");   
    #                         System.exit(0);// Exit  
    #                       
    #                     break;  
    #                       
    #                     default:  
    #                      System.out.print("Invalid Option");  
    #                 }  
    #             }  
    #           
    #           
    #             inFile.close();  
    #        }                                                    
    # }
    Last edited by ryn21; 10-03-2008 at 08:35 PM.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default

    Do you have a problem, error or question?

    Please use CODE tags when posting code. One of the icons above the input window will put them in the window.

  4. #4
    ryn21 is offline Member
    Join Date
    Oct 2008
    Posts
    16
    Rep Power
    0

    Default

    oh sorry, ya im not sure how to do the operations on the menu options i have with the numbers from the file.

  5. #5
    ryn21 is offline Member
    Join Date
    Oct 2008
    Posts
    16
    Rep Power
    0

    Default

    please someone help, i have no idea what to do next and its due soon.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default

    Start with a single step and add to it.
    Where does your program read the contents of the file? Is it supposed to read all of the file at once? That will take a loop.
    present the user with a menu
    How is that to be done? Printed to STD output or ?
    How will the program get the user's choice?

    Add println()s to the code to see the values of variables and to follow the program flow.
    Last edited by Norm; 10-03-2008 at 10:24 PM.

  7. #7
    ryn21 is offline Member
    Join Date
    Oct 2008
    Posts
    16
    Rep Power
    0

    Default

    yes i have done that so far, here is what i have come up with. it is giving me an error on the while loop i tried to nest in the switch. I can not figure out how to do the math with the numbers from the text

    Java Code:
    		
    								  
    import java.util.*;
    import java.io.*;
    
    public class Lab5
    {
    	
    			static Scanner console = new Scanner(System.in);
    	
        public static void main ( String[] args)
    	 								throws FileNotFoundException
    
    																					
         {
    
    		 Scanner inFile = new Scanner(new FileReader("Random.txt"));
    		 
    		 int a;
    		 int number = inFile.nextInt();
    		 int sum = 0;
    			
    		 
    		 System.out.println("1. Get the largest value");
    		 System.out.println("2. Get the smallest value"); 
    		 System.out.println("3. Get the sum of values"); 
    		 System.out.println("4. Get the average"); 
    		 System.out.println("5. Get the number of values entered"); 
    		 System.out.println("6. End this program"); 
    		 System.out.println("Enter your choice --->"); 
    		 
    		 a = console.nextInt();
    
    		   while (inFile.hasNextInt())
    			 {if (a <= 6)
    			 
    				switch (a)
    				{
    				   case 1:  
    				  
             	   case 2:
      					
      					case 3:
    					
    					  while (num != 515)
    					  {
    					  	sum += num;
    						num = inFile.nextInt();
      					  }
    					 System.out.println("Sum = " + sum);
    					  
      					case 4:
      					 
      					case 5:
    						
    					
      					 break;
    					 
      					case 6:
    						System.out.print("Goodbye"); 
    					 	System.exit(0);// Exit
      					
      					break;
    					
      					default:
    			 		 System.out.print("Invalid Option");
    				}
    			}
    		
    		
    			inFile.close();
    	   }                                                  
    }

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default

    it is giving me an error
    Where did you post the error? I don't see it!

    to do the math with the numbers from the text
    If you are reading in text that consists of numeric digits, you can convert them to int by using a parse method of the Integer class.

    Otherwise please explain.

  9. #9
    ryn21 is offline Member
    Join Date
    Oct 2008
    Posts
    16
    Rep Power
    0

    Default

    i have figured out the error but it does not need to be parsed. How do i make it return to the beginning of the program so the println menu returns after they perform an option.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default

    make it return to the beginning of the program
    Sounds like you need a loop. Look at the while statement.

Posting Permissions

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