Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Nov 2010
    Location
    canada
    Posts
    131
    Thanks
    51
    Thanked 0 Times in 0 Posts

    integers inputed for hundred - if else statements

    Ok i got my integers to work to the hundreds.

    I like to be able to input numbers under 100 and still keep program running.

    AT this time when i run the program as output, it runs well if both numbers i enter are 100 or more when under a hundred i get errors.I have got program to work, but it is still not working if a number under 100 is inputted.

    here is my program. without changes

    Code:
    import java.util.Scanner;              // Scanner class 
    
    
    public class gradingform               		// name of application    
    { 
      static String first;                // Student's first name
      static String last;                 // Student's last name
     
      
      static void printFinalCaculations()            //  printFinalCaculations() for more output
      
      {
        Scanner inData;      // A class that reads input 
        inData = new Scanner(System.in);
    	System.out.println("Enter 1st integer "); 			// Input of first string
    	first = inData.nextLine();  								 // Get first name
    	
    	System.out.println("Enter a 2nd integer "); 			// Input of second string
    	last = inData.nextLine();  									 // Get last name
    	
    	
    	
    
    	int second = ((first.length()) - 3);						// calculation of first string
    	int length = ((last.length()) - 3);							// calculation of last string
    	
    	if(second < 100)
    	System.out.println("your first integer is under 100");
    	
    	else if (second >100)
    		System.out.println("your first integer is over 100");
    	else
    		System.out.println("that is a good answer");
    	
    	if(length < 100)
    		System.out.println("your last integer is under 100");
    		else if (length >100)
    			System.out.println("your last integer is over 100");
    		else
    			System.out.println("another good answer");
    	
    			
    	
    	 System.out.println('\n');  																										// Space between lines
    	 System.out.println("The hundredth digit is: " + first.charAt(second));  															// output from input of first integer
    	 System.out.println("The hundredth digit is: " + last.charAt(length)); 																// output from input of last integer
    	 System.out.println('\n');  																										// Space between lines
    	 System.out.println("The first original digit is " + first + " To which the hundredth digit of it is: " + first.charAt(second)); 	// output from input of first integer
    	 System.out.println("The second original digit is " + last + " To which the hundredth digit of it is: " + last.charAt(length));     // output from input of last integer
    	 System.out.println('\n');  																									    // Space between lines
    	
    	}
      
      
    
    
    
    
    
    
     // End of Name class
        
      public static void main(String[] args) 													 // Main is where execution starts.  It declares variables, instantiates
      {
    	  printFinalCaculations();			 													//  method  printFinalCaculations() to print the final 
    	
    	System.out.println('\n');  																// Space between lines
    	System.out.println("Assignment of PostLab Activities page 45");        						// Output of assignment
    	System.out.println('\n');  																// Space between lines
    	System.out.println("TME2");  																// Output of assignment 
    	System.out.println('\n');																	// Space between lines
    	System.out.println('\n');																	// Space between lines
    	System.out.println("Part of Project:"); 													// Output of project name
        System.out.println('\n'); 																	// Space between lines
        
             
      }
    }				// End of Name class
    Works fine till a number under 100 is inputed

    Heres the code I been trying to work with. to get the under 100 integers to work

    using if else statements

    heres my if else statements
    Code:
     int second = ((first.length()) - 3);                  // calculation of first string
       int length = ((last.length()) - 3);                     // calculation of last string
       
       if(second < 100)
       System.out.println("your first integer is under 100");
       
       else if (second >100)
          System.out.println("your first integer is over 100");
       else
          System.out.println("that is a good answer");
       
       if(length < 100)
          System.out.println("your last integer is under 100");
          else if (length >100)
             System.out.println("your last integer is over 100");
          else
             System.out.println("another good answer");

    When i run the proper integers over 100 i am still getting "System.out.println("your last integer is under 100");" in my answers i want this to only come when i enter a number under 100

    when i run a number under 100 i am still getting error messages in output. what do you all experts see as problem?


    The error message i get is this "Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.charAt(String.java:686)
    at gradingform.printFinalCaculations(gradingform.java:89)
    at gradingform.main(gradingform.java:109)"

    Thanks for your help

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    These calculations aren't quite right.
    When you're checking your ifs, you are checking the length of the characters and not the size; in order for them to change you need to change the length of the input number to 100 or more (which is ridiculously large of course).
    Write a simple method:
    PHP Code:
    public int getHundreds(String num)
    {
        
    int result = -1;
        if (
    num.length() >= 3)
        {
            
    result String.parseInt(num.charAt(num.length() - 3));
        }
        return 
    result;
    }

    // used
    public static void main(String[] argv)
    {
        
    int iTmp;
        ...
        if ((
    iTmp getHundreds(first)) > -1)
        {
            
    System.out.println("The first number is " first ", and its hundreds number is " iTmp);
        }
        else
        {
            
    System.out.println("The first number " first " does not have a hundreds character");
        } 

    And repeat for each.
    There are other routes to do this as well, but given what you have posted I assume that looping, arrays and exceptions are out of the question?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder
    Join Date
    Nov 2010
    Location
    canada
    Posts
    131
    Thanks
    51
    Thanked 0 Times in 0 Posts
    So am i only changing in my calculation area

    such as here
    Code:
    int second = ((first.length()) - 3);                  // calculation of first string
       int length = ((last.length()) - 3);                     // calculation of last string
       
       if(second < 100)
       System.out.println("your first integer is under 100");
       
       else if (second >100)
          System.out.println("your first integer is over 100");
       else
          System.out.println("that is a good answer");
       
       if(length < 100)
          System.out.println("your last integer is under 100");
          else if (length >100)
             System.out.println("your last integer is over 100");
          else
             System.out.println("another good answer");
    or do i need to rewrite this whole part?
    cheers

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I'd rewrite the whole part. The method is only to simplify; you can create the same with using only if's for each of the input numbers. You'll still need to scan in the input from the user.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Tags for this Thread

    Posting Permissions

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