0

I need to create an add method to add the feet and inches that are inputted by the user. The user can input any integer in feet or inches, and get it back, but i need to add the values. I am not exactly sure how i can approach that. Any help would be great!

The following is my code:

import java.util.Scanner;
public class MixWithUnit_JM {
 Fraction_JM a;
int first;
 int second;

public String displayMixWithUnit_JM() {
  String str="";
  if (first == 0) str= first%second+"/"+second+"in";
  else if (first%second == 0) str= ""+first/second+"ft";
  else if (first < second) str=first+"/"+second+"in"; 
  else str= first/second+"ft" +" "+ first%second+"/"+second+"in"; 
  return str; 
}//display


public MixWithUnit_JM(String str) {
  int pos = str.indexOf("ft");
  int pos2 = str.indexOf("in"); 
  String a1 = "";
  String a2 ="";
  String b1 = "";
  String b2 ="";
  String b3 ="";

  if(pos == -1){
     a2 = str.substring(0,pos2 +2).trim();
     b2 = str.substring(0,pos2);
     b3 = b2;
  }
  else if(pos2 == -1){
     a1 = str.substring(0,pos +2);
     b1 = str.substring(0, pos);
     b3 = b1; 
  }
  else{
     a1 = str.substring(0,pos +2);
     b1 = str.substring(0, pos);
     a2 = str.substring(pos +2).trim();
     b2 = str.substring(pos+2, str.length()-2);
     b3 = b1 + b2; 
  }

  int[] iA= parse (b3);
  int top=iA[0]*iA[2]+iA[1];
  int bot= iA[2];
  a = new Fraction_JM (top,bot);
  int gcd = a.gcd(top,bot);
  first=top/gcd; 
  second =bot/gcd;
 }//Mix




 public static String get (){
  Scanner scan = new Scanner (System.in);
  String userInput = scan.nextLine();
  userInput =userInput.trim();
  return (userInput);

 } //get


 public static int[] parse (String userInput){
  int iNum = 0;
  int iTop = 0;
  int iBot = 1;

  userInput = userInput.trim();
  int pos = userInput.indexOf(" "); 
  int pos2 = userInput.indexOf("/");   

  if (pos == -1 && pos2 != -1){
     pos= userInput.indexOf("/"); 
     String sTop=userInput.substring(0,pos);
     iTop = Integer.parseInt(sTop);//second integer
     String sBot=userInput.substring(pos+1);
     iBot = Integer.parseInt(sBot);//third integer
     //case 2

     }
    else if (pos == -1 && pos2== -1) {
     iNum = Integer.parseInt(userInput);//first integer
     //case 1
     }
    else{
     String sNum=userInput.substring(0,pos);
     iNum = Integer.parseInt(sNum);//first integer

     String sNum2=userInput.substring(pos+1);
     pos= sNum2.indexOf("/");
     String sTop=sNum2.substring(0,pos);
     iTop = Integer.parseInt(sTop);//second integer

     String sBot=sNum2.substring(pos+1);
     iBot = Integer.parseInt(sBot);//third integer
   }
   int[] sA = {iNum,iTop,iBot};
   return (sA);        
 } //parse


 public static void main(String[] args) {
  System.out.print("Please enter a mixed-format number :");
  String userInput = MixWithUnit_JM.get();
  System.out.println("Input is: "+userInput);  
  MixWithUnit_JM s = new MixWithUnit_JM(userInput);
  s.displayMixWithUnit_JM();

  System.out.print("Please enter a mixed-format number :");
  userInput = MixWithUnit_JM.get();
  System.out.println("Input is: "+userInput);
  MixWithUnit_JM s2 = new  MixWithUnit_JM(userInput);  
  s2.displayMixWithUnit_JM();


 }//main
}//class

The program runs, all i need is an add method.

1 Answer 1

0

You should use the System.in.read() for read from the standard input. You can find more info in this post: What does System.in.read actually return?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.