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. 6 13365
you could set the Scanner delimiter to / then read the numerator, reset the delimiter to whitespace then read the denominator, e.g. -
Pattern whitespace=s.delimiter();
-
s.useDelimiter("/"); // set delimiter as /
-
int i = s.nextInt(); // read numerator
-
String test = s.findWithinHorizon(".",0); // read the /
-
s.useDelimiter(whitespace); // reset delimeter to whitespace
-
int j = s.nextInt(); // read the denominator
-
you could set the Scanner delimiter to / then read the numerator, reset the delimiter to whitespace then read the denominator, e.g. -
Pattern whitespace=s.delimiter();
-
s.useDelimiter("/"); // set delimiter as /
-
int i = s.nextInt(); // read numerator
-
String test = s.findWithinHorizon(".",0); // read the /
-
s.useDelimiter(whitespace); // reset delimeter to whitespace
-
int j = s.nextInt(); // read the denominator
-
Thank you for the help horace1,
If I understand correctly I can set the delimeter in the Scanner.
Should it look like this - Scanner input = new Scanner( / , System.in );
Or am I way off track?
Thank you for the help horace1,
If I understand correctly I can set the delimeter in the Scanner.
Should it look like this - Scanner input = new Scanner( / , System.in );
Or am I way off track?
I managed to miss the declaration of Scanner, try this program -
// read a fraction in form 6/7
-
-
import java.util.Scanner;
-
import java.util.regex.*;
-
-
public class ScanFraction {
-
public static void main(String[] args) {
-
Scanner s = new Scanner(System.in);
-
Pattern whitespace=s.delimiter();
-
s.useDelimiter("/"); // set delimiter as /
-
int i = s.nextInt(); // read numerator
-
String test = s.findWithinHorizon(".",0); // read the /
-
s.useDelimiter(whitespace); // reset delimeter to whitespace
-
int j = s.nextInt(); // read the denominator
-
System.out.println("i = " + i + " j = " + j);
-
}
-
}
-
-
I managed to miss the declaration of Scanner, try this program -
// read a fraction in form 6/7
-
-
import java.util.Scanner;
-
import java.util.regex.*;
-
-
public class ScanFraction {
-
public static void main(String[] args) {
-
Scanner s = new Scanner(System.in);
-
Pattern whitespace=s.delimiter();
-
s.useDelimiter("/"); // set delimiter as /
-
int i = s.nextInt(); // read numerator
-
String test = s.findWithinHorizon(".",0); // read the /
-
s.useDelimiter(whitespace); // reset delimeter to whitespace
-
int j = s.nextInt(); // read the denominator
-
System.out.println("i = " + i + " j = " + j);
-
}
-
}
-
-
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.
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!
You might even say, "Victory is mine!"
Glad to see you got the solution.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |