Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Cannot obtain ' and " in my program

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Cannot obtain ' and " in my program

    Hello everyone,
    Thanks for trying to help me.

    The program that I am working on is supposed to ask a person for their input in feet and inches

    2' 2 2/2" <--- (This format)

    However, I cannot come up with a way that enables such kind of user input.

    The program is composed of three classes

    Fraction

    Pair

    Mix

    <!-- Java: ... -->


     import java.util.Scanner;
        class Mix extends Fraction{
     
          public Mix ( int n, int m) 
          {
          super (n,m);
          }
     
          public String displayMix() 
          {
          String str="";
          if (first < second) 
          str=first+ "'" + first%second +"/"+second+"\"";
          else 
          str=first+"'" +(first+second)+"/"+second+"\"";
     
          return str; 
          } //display
     
        public static String get ()
        {
          Scanner scan = new Scanner(System.in);
          System.out.print("Please enter a mixed-format number:");
          String userInput = scan.nextLine();
          userInput = userInput.trim();
          System.out.println("Input is: "+ userInput);
          return (userInput);
        } //get
     
     
          public static void main(String [] args)
          {
          String userInput;
          int[]iA = {0,0,0,1};
     
          userInput = Mix.get();
          iA=parse(userInput);
          Mix f = new Mix ( iA[0] , (iA[1]*iA[2])+(iA[3]) );
          System.out.println("Number = " + f.displayMix());
     
          userInput = Mix.get();
          iA=parse(userInput);
          Mix g = new Mix ( iA[0] , iA[1] * iA[3] + iA[2]);
          System.out.println("Number = " + g.displayMix());
     
          Mix h = Mix.add (f,g);
          System.out.println("Sum ="+ h.displayMix());
          } // main
     
     
          public static Mix add (Mix f, Mix g){
          int gtop=       f.first+g.first;
     
          int gbottom=    f.second+g.second;
          return(new Mix ( gtop, gbottom));
          } //add
     
     
        public static int[] parse (String userInput){
        int [] sA = {0,0,0,1};
        int pos = userInput.indexOf("'");
     
        String sNum0 = userInput.substring(0,pos);
        pos = userInput.indexOf("'");
        sA[0]= Integer.parseInt(sNum0);
     
        String sNum = userInput.substring(0,pos);
        pos = sNum.indexOf("  ");
        sA[1]= Integer.parseInt(sNum);
     
        String sNum2 = userInput.substring(pos + 1);
        pos = sNum2.indexOf("/");
     
        String sTop= sNum2.substring (pos +3);
        pos = sNum2.indexOf("\"");
        sA[2] = Integer.parseInt(sTop);
     
        String sBot = sNum2.substring(pos +1);
     
        sA[3] = Integer.parseInt(sBot);
     
        // 2' 2 2/2
        return (sA);
        }} //parse

    I have managed to get result after inputting

    2' 2 2/2"1

    I don't know what I am doing wrong, but something is off since i need a number after the " to get a result back.

    I am pretty sure that something is wrong with the parsing method, i just dont know where it is.

    If you test the program you will see that the results for the numbers are not correct, I am aware of this issue, but I want to nail down the input
    so that later on i can deal with the computational issues with more ease.

    This is the Fraction class

       public class Fraction extends Pair{
        //attributes: NONE
     
     
        public Fraction(int n, int m) {
        super(n,m);
        int g=gcd(n,m);
        first = first;
        second=second/g;
        }//Fraction
     
     
        public void display2() {
        System.out.print(first);
        System.out.print("/");
        System.out.println(second);
        }//display
     
     
        public Fraction add (Fraction f1, Fraction f2){
        int gtop=f1.first * f2.second
        + f2.first * f1.second;
        int gbottom= f1.second * f2.second;
        return (new Fraction(gtop,gbottom));
        }
     
     
        public static int gcd (int n, int m){
        while ( n!=m) {
        if (n>m) n=n-m;
        else m=m-n;
        }//while
        return (n);
        } //gcd
        } //class

    This is the third class

        public class Pair {
        int first;
        int second;
     
        public Pair(int n, int m) {
        first=n;
        second=m;
        }//Pair
     
     
        public void display() {
        //pseudo_code is here
        System.out.print("First integer="+first);
        System.out.println();
        System.out.println("Second integer="+second);
        }//display
     
     
        public static void main(String[] args) {
        //pseudo-code is here
        Pair f= new Pair(2,4);
        f.display();
        }//main
        } //class


  2. #2
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    384
    My Mood
    Angry
    Thanks
    46
    Thanked 63 Times in 63 Posts

    Default Re: Cannot obtain ' and " in my program

    Did you mean:

    2", 2', 2"/2' ?

    You could do this:

    String parseMe = input.nextLine();

    String[] inputs = parseMe.split(" ");

    Now, you should have

    inputs[0] = 2'
    inputs[1] = 2
    inputs[2] = 2/2"

    Also, to parse the last one, use the String method valueOf(char) to get around the usual possible syntax errors with those pesky double quotes.

    i.e.

    String quotes = String.valueOf('"');


    To further break down the 2/2", you could do a further split on it

    String[] inputs2 = inputs[2].split("/");


    I'm afraid I don't understand what all of the stuff means. Particularly what 2/2"1 means. However, as long as there are only spaces between the different types of input

    i.e.

    2' 2 2/2" 2/2"1 then it should work with my String split(" ") method call in separating them all.

    Again, I'm not quite sure what 2/2" and 2/2"1 represent so I can't be for certain on what to do, but did I help at all?

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Cannot obtain ' and " in my program

    Cannot get the meant results in feet and inches

    Duplicate post
    Improving the world one idiot at a time!

Similar Threads

  1. Replies: 2
    Last Post: July 15th, 2012, 10:19 AM
  2. "Program execution skips the "if" statement"!
    By antony1925 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2012, 08:15 AM
  3. Find the important class in ".mse" file using Java program
    By ashwarth21 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 13th, 2012, 08:43 PM
  4. Replies: 21
    Last Post: May 29th, 2011, 01:48 PM
  5. Creating a java program to "Beat" Snake web app?
    By AkOndray in forum AWT / Java Swing
    Replies: 2
    Last Post: December 5th, 2010, 12:36 AM

Tags for this Thread