Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //==============================================================================
- // Parker McComb
- // Project 1 AP Comp Sci
- // Milestone 1 16 October 2012
- //
- // This program will take in integer with fractions tacked on and it will
- // Separate the integer the numerator and the denominator from each other.
- //==============================================================================
- import java.util.Scanner; // Scanner so we can get the data from the viewer.
- public class project13 {
- public static void main(String[] args) {
- Scanner console = new Scanner (System.in);
- while (true) {
- System.out.println("Please enter a fraction:"); // Viewers instructions.
- String input1 = console.nextLine();
- if (input1.equals("quit")) // Used to terminate the console when "quit" is typed.
- break;
- int wholeNum;
- int numerator;
- int denominator;
- int underScore = input1.indexOf('_'); // Define character that separates integer from fraction.
- int divide = input1.indexOf('/'); //Define character the separated numerator from denominator.
- String wholeNumstr = input1.substring(0, underScore); // Define where integer starts and ends.
- String numeratorstr = input1.substring(underScore+1, divide);// Define where numerator starts and ends.
- String denominatorstr = input1.substring(divide+1);// Define where denominator starts and ends.
- wholeNum = Integer.parseInt(wholeNumstr);
- numerator = Integer.parseInt(numeratorstr);
- denominator = Integer.parseInt(denominatorstr);
- if (input1.indexOf('_') == -1){
- wholeNum = 0; // Assign integer 0 if the if statement is correct
- }
- else {
- wholeNumstr = input1.substring(0,underScore); // Define where integer starts and ends.
- wholeNum = Integer.parseInt(wholeNumstr);
- input1.substring(underScore + 1);
- System.out.println(input1.substring(underScore + 1));
- }
- if (input1.indexOf('/') == -1){
- denominator = 1; // Assign 1 if index or "/" = -1
- }
- else {
- denominatorstr = input1.substring(divide+1);// Define where denominator starts and ends.
- denominator = Integer.parseInt(denominatorstr);
- input1.substring(0,divide);
- }
- String numeratorstr1 = input1.substring(underScore+1, divide);// Define where numerator starts and ends.
- numerator = Integer.parseInt(numeratorstr1);
- System.out.println ("<" + wholeNum + ">" + "_" + "<" + numerator + ">" + "/" + "<" + denominator + ">" );
- // Print the numbers with < and > on each side.
- }
- System.out.println("\nDone.");
- console.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement