473,403 Members | 2,183 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Help getting user input into my fraction calculating program.

evilmonkey
I am very new to programming as well as Java and this is my first post so please forgive me if this is not quite posted correctly. My Problem is that I have only been using scanner to get user input into most of the exercises I have done. This exercise is asking for a user to enter two fractionslike "1/3" or "5/8". Scanner doesn't work and I don't know of another way to get this done. I think that I will have to somehow strip the "/" out and use the integers for a numerator and denominator. I've been able to make it work by hard coding the variables into the main method. Any help with this would be very much appreciated.

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. public class Main {
  3.  
  4.     /** Creates a new instance of Main */
  5.     public Main() {
  6.     }
  7.  
  8.     /**
  9.      * @param args the command line arguments
  10.      */
  11.     public static void main(String[] args) {
  12.     Scanner input = new Scanner(System.in);
  13.  
  14.  
  15.  
  16.     Fraction f1 = new Fraction(3,5);
  17.  
  18.     Fraction f2 = new Fraction(1,6);
  19.  
  20.     System.out.println();        
  21.     System.out.println(" This program preforms arithmetic" +
  22.                        " operations on two fractions."); 
  23.     System.out.println(); 
  24.     System.out.println(); 
  25.     System.out.println("Enter first fraction: "); 
  26.     //f1 = input.nextInt();
  27.     System.out.println(f1);
  28.  
  29.     System.out.println("Enter second fraction: "); 
  30.     //f2 = input.nextInt();
  31.     System.out.println(f2); 
  32.  
  33.     Fraction f3= f1.add(f2);
  34.     System.out.println("Sum: "+f3);
  35.     Fraction f4= f1.subtract(f2);
  36.     System.out.println("Difference: "+f4);
  37.     Fraction f5= f1.multiply(f2);
  38.     System.out.println("Product: "+f5);
  39.     Fraction f6= f1.divide(f2);
  40.     System.out.println("Quotient: "+f6);
  41.     System.out.println(); 
  42.     }
  43.  
  44.  
  45.  
  46.  
  47.  }// end Main 
  48.  
  49. // Separate fraction class
  50. public class Fraction {
  51.  
  52.         private int numerator;
  53.         private int denominator;
  54.  
  55.  
  56.     public Fraction(int num, int denom) {
  57.     numerator = num;
  58.     denominator= denom;       
  59.     }
  60.  
  61.     public Fraction(Fraction f)
  62.     {
  63.     numerator = f.numerator;
  64.     denominator = f.denominator;
  65.     }
  66.  
  67.     public int getNumerator(){
  68.     return numerator;
  69.     }
  70.  
  71.     public int getDenominator(){
  72.     return denominator;
  73.     }
  74.  
  75.     public void setNumerator(int num){
  76.     numerator = num;
  77.     }
  78.  
  79.     public void setDenominator(int denom){
  80.     denominator= denom;
  81.     }
  82.  
  83.     public Fraction add(Fraction f){
  84.     int num = numerator  * f.denominator + f.numerator * denominator;
  85.     int denom = denominator * f.denominator;
  86.     return new Fraction(num,denom);
  87.     }
  88.  
  89.     public Fraction subtract(Fraction f){
  90.     int num = numerator  * f.denominator - f.numerator * denominator;
  91.     int denom = denominator * f.denominator;
  92.     return new Fraction(num,denom);
  93.     }
  94.  
  95.     public Fraction multiply( Fraction f ){
  96.     return new Fraction ( numerator * f.numerator, 
  97.                           denominator * f.denominator);
  98.     }
  99.  
  100.     public Fraction divide( Fraction f) {
  101.     int temp = f.numerator;
  102.     f.numerator = f.denominator;
  103.     f.denominator = temp;
  104.     return multiply(f);
  105.     }
  106.  
  107.     public String toString(){
  108.     return numerator + "/" + denominator;
  109.     }
  110.  
  111.     public Fraction mult(Fraction f) {
  112.     numerator *= f.numerator;
  113.     denominator *= f.denominator;
  114.  
  115.     return this;
  116.     }
  117.  
  118.  
  119. }
  120.  
Feb 17 '07 #1
6 13365
horace1
1,510 Expert 1GB
you could set the Scanner delimiter to / then read the numerator, reset the delimiter to whitespace then read the denominator, e.g.
Expand|Select|Wrap|Line Numbers
  1.         Pattern whitespace=s.delimiter();       
  2.         s.useDelimiter("/");                            // set delimiter as /
  3.         int i = s.nextInt();                            // read numerator
  4.         String test = s.findWithinHorizon(".",0);       // read the /
  5.         s.useDelimiter(whitespace);                     // reset delimeter to whitespace
  6.         int j = s.nextInt();                            // read the denominator
  7.  
Feb 17 '07 #2
you could set the Scanner delimiter to / then read the numerator, reset the delimiter to whitespace then read the denominator, e.g.
Expand|Select|Wrap|Line Numbers
  1.         Pattern whitespace=s.delimiter();       
  2.         s.useDelimiter("/");                            // set delimiter as /
  3.         int i = s.nextInt();                            // read numerator
  4.         String test = s.findWithinHorizon(".",0);       // read the /
  5.         s.useDelimiter(whitespace);                     // reset delimeter to whitespace
  6.         int j = s.nextInt();                            // read the denominator
  7.  
Thank you for the help horace1,
If I understand correctly I can set the delimeter in the Scanner.
Should it look like this
Expand|Select|Wrap|Line Numbers
  1. Scanner input = new Scanner( / , System.in ); 
Or am I way off track?
Feb 17 '07 #3
horace1
1,510 Expert 1GB
Thank you for the help horace1,
If I understand correctly I can set the delimeter in the Scanner.
Should it look like this
Expand|Select|Wrap|Line Numbers
  1. Scanner input = new Scanner( / , System.in ); 
Or am I way off track?
I managed to miss the declaration of Scanner, try this program
Expand|Select|Wrap|Line Numbers
  1. // read a fraction in form 6/7
  2.  
  3. import java.util.Scanner;
  4. import java.util.regex.*;
  5.  
  6. public class ScanFraction {
  7.     public static void main(String[] args)  {
  8.         Scanner s = new Scanner(System.in);
  9.         Pattern whitespace=s.delimiter();       
  10.         s.useDelimiter("/");                            // set delimiter as /
  11.         int i = s.nextInt();                            // read numerator
  12.         String test = s.findWithinHorizon(".",0);       // read the /
  13.         s.useDelimiter(whitespace);                     // reset delimeter to whitespace
  14.         int j = s.nextInt();                            // read the denominator
  15.         System.out.println("i = " + i + " j = " + j);
  16.     }
  17. }
  18.  
  19.  
Feb 17 '07 #4
I managed to miss the declaration of Scanner, try this program
Expand|Select|Wrap|Line Numbers
  1. // read a fraction in form 6/7
  2.  
  3. import java.util.Scanner;
  4. import java.util.regex.*;
  5.  
  6. public class ScanFraction {
  7.     public static void main(String[] args)  {
  8.         Scanner s = new Scanner(System.in);
  9.         Pattern whitespace=s.delimiter();       
  10.         s.useDelimiter("/");                            // set delimiter as /
  11.         int i = s.nextInt();                            // read numerator
  12.         String test = s.findWithinHorizon(".",0);       // read the /
  13.         s.useDelimiter(whitespace);                     // reset delimeter to whitespace
  14.         int j = s.nextInt();                            // read the denominator
  15.         System.out.println("i = " + i + " j = " + j);
  16.     }
  17. }
  18.  
  19.  
I've got to take my wife to dinner but I wanted to thank you for your reply again I'll be trying this when I return home. thanks again horace1, I think I should have tried posting a long time ago.
Feb 17 '07 #5
I've got to take my wife to dinner but I wanted to thank you for your reply again I'll be trying this when I return home. thanks again horace1, I think I should have tried posting a long time ago.

Ha! Success!
Feb 18 '07 #6
Ganon11
3,652 Expert 2GB
You might even say, "Victory is mine!"

Glad to see you got the solution.
Feb 18 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Tim M | last post by:
Hi Does anyone know if its possible to create and email HTML forms. Email recipients would then input information to these forms in the same way that they would on a web page. Pressing the...
4
by: Don W. | last post by:
Is it possible to open a browser window from a javascript for the purpose of gathering user input on a form and then use data from the form in the javascript? How do you get the data from the form...
1
by: CAD Fiend | last post by:
Hello, I am making a form that will contain address fields for a land owner, but sometimes the land owner does not live on the same property they own (for instance, they may be renting it out to...
7
by: JT | last post by:
I have a web page with 5 text boxes. I have an access database with 5 fields that correspond to the text boxes. I need to put the information that the user inputs to the text boxes in my...
3
by: dei3cmix | last post by:
Hey, I am having a problem with a program I am working on. Basically, the first part of the program gets input from a file using cin.getline. Then the second part, (still in the same main as the...
0
by: paultawk | last post by:
I have to write a c++ program for sound synthesis and rather complicated,i don't really know from where to start: My program is to be used in robotics. It should read an input from an mp3 file. It...
1
by: yodadbl07 | last post by:
hey ive been messing around with this program to take a user inputed number, put it into an array and then reverse the number. I also want it to omit unecessary 0's at the begging. for example if...
1
by: shaunhh | last post by:
Hi All, I am trying to use the user input in my form which is using a stored query. Here is the stored query: SELECT Accounts.balance, Accounts.transactionDate, ...
10
by: Shoaib Muhammad | last post by:
Hi every one, I am new in perl. I m using Active Perl 5.8.7 Window based. When i use <STDIN> for getting user input in my perl code, after executing i dont get any output on the screne. Following is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.