Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. //==============================================================================
  2. // Parker McComb
  3. // Project 1 AP Comp Sci
  4. // Milestone 1 16 October 2012
  5. //
  6. // This program will take in integer with fractions tacked on and it will
  7. // Separate the integer the numerator and the denominator from each other.
  8. //==============================================================================
  9.  
  10. import java.util.Scanner; // Scanner so we can get the data from the viewer.
  11. public class project13 {
  12. public static void main(String[] args) {
  13. Scanner console = new Scanner (System.in);
  14. while (true) {
  15. System.out.println("Please enter a fraction:"); // Viewers instructions.
  16. String input1 = console.nextLine();
  17. if (input1.equals("quit")) // Used to terminate the console when "quit" is typed.
  18. break;
  19.  
  20. int wholeNum;
  21. int numerator;
  22. int denominator;
  23.  
  24. int underScore = input1.indexOf('_'); // Define character that separates integer from fraction.
  25. int divide = input1.indexOf('/'); //Define character the separated numerator from denominator.
  26.  
  27. String wholeNumstr = input1.substring(0, underScore); // Define where integer starts and ends.
  28. String numeratorstr = input1.substring(underScore+1, divide);// Define where numerator starts and ends.
  29. String denominatorstr = input1.substring(divide+1);// Define where denominator starts and ends.
  30.  
  31.  
  32. wholeNum = Integer.parseInt(wholeNumstr);
  33. numerator = Integer.parseInt(numeratorstr);
  34. denominator = Integer.parseInt(denominatorstr);
  35.  
  36. if (input1.indexOf('_') == -1){
  37. wholeNum = 0; // Assign integer 0 if the if statement is correct
  38. }
  39. else {
  40. wholeNumstr = input1.substring(0,underScore); // Define where integer starts and ends.
  41. wholeNum = Integer.parseInt(wholeNumstr);
  42. input1.substring(underScore + 1);
  43. System.out.println(input1.substring(underScore + 1));
  44. }
  45.  
  46. if (input1.indexOf('/') == -1){
  47. denominator = 1; // Assign 1 if index or "/" = -1
  48. }
  49. else {
  50. denominatorstr = input1.substring(divide+1);// Define where denominator starts and ends.
  51. denominator = Integer.parseInt(denominatorstr);
  52. input1.substring(0,divide);
  53.  
  54. }
  55.  
  56. String numeratorstr1 = input1.substring(underScore+1, divide);// Define where numerator starts and ends.
  57. numerator = Integer.parseInt(numeratorstr1);
  58.  
  59.  
  60. System.out.println ("<" + wholeNum + ">" + "_" + "<" + numerator + ">" + "/" + "<" + denominator + ">" );
  61. // Print the numbers with < and > on each side.
  62.  
  63. }
  64. System.out.println("\nDone.");
  65. console.close();
  66.  
  67.  
  68.  
  69. }
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement