Results 1 to 8 of 8
- 09-21-2011, 04:47 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 5
- Rep Power
- 0
creating fraction class for school get weird problem
The process Completes and the user is prompted to enter a fractionJava Code://Colin Steele //CSC2310 import java.io.*; import java.util.*; import java.awt.*; import java.lang.*; public class Colinsteele { public static void main(String[] args) throws Exception { BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); int x = 0; System.out.println("Enter fraction in x/y format >"); String frac = in.readLine(); if(frac.indexOf("/")!=-1) { System.out.println(frac.charAt(0)); x = frac.charAt(0); System.out.println(x); System.out.println(); } else { System.out.println("You've not correctly input the fraction. Please try again."); } } }
so I input "2/3" at the prompt and i get the following print:
2
50
the first line prints 2 but the second line prints the value assigned to x, which should be the exact same value as frac.charAt(0); why does this happen?Last edited by sunde887; 09-21-2011 at 05:37 AM.
-
Re: creating fraction class for school get weird problem
frac is a String, not a number. The first char in the String is not 2 but is '2' -- that's a big difference.
For instance, what happens if you do this?
Java Code:System.out.println(frac.charAt(0)); x = frac.charAt(0); System.out.println((char)x); // *** note the difference! System.out.println();Last edited by Fubarable; 09-21-2011 at 05:00 AM.
- 09-21-2011, 05:10 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 5
- Rep Power
- 0
Re: creating fraction class for school get weird problem
Yes yes I see what you're saying
I've replaced the operational part of the code you referenced with this
String numerator = frac.charAt(0);
x = Integer.parseInt(numerator);
but it tells me that frac.charAt(0) and the String numerator are of incompatible types
how do I change the charAt(0) into an integer?
-
Re: creating fraction class for school get weird problem
Use a String rather than a char and Integer.parseInt(...) will work fine.
- 09-21-2011, 05:32 AM #5
Member
- Join Date
- Sep 2011
- Location
- Mumbai, India
- Posts
- 35
- Rep Power
- 0
Re: creating fraction class for school get weird problem
Firstly you can make your code a bit simplified
1) No need to import java.lang package as this package is imported by default
2)I don't know why are you importing the java.awt package when i can't see any graphics work
3)Why are you going for BufferedReader when you are expecting the user to input numbers because BufferedReader gives you String and then you have to
parse it and all that stuff instead, go for the Scanner class which is in java.util package, you can writeJava Code:Scanner s=new Scanner(System.in); int no=s.nextInt(); // no need to parse
- 09-21-2011, 05:40 AM #6
Member
- Join Date
- Sep 2011
- Posts
- 5
- Rep Power
- 0
Re: creating fraction class for school get weird problem
HA!
Fixed it
Had to do
char num = frac.charAt(0);
String numerator = Character.toString(num);
x = Integer.parseInt(numerator);
System.out.println(x);
System.out.println();
Woot! This should fix all the problems
- 09-21-2011, 05:40 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: creating fraction class for school get weird problem
I'd like to point out that the scanner class can cause some problems as well, luckily they are easily avoided. When using Scanner.nextInt(), you can get errors due to the eol character.
When using Scanner.nextInt(), be sure to make a subsequent call to Scanner.nextLine() to 'swallow' the eol token.
- 09-21-2011, 05:45 AM #8
Member
- Join Date
- Sep 2011
- Location
- Mumbai, India
- Posts
- 35
- Rep Power
- 0
Similar Threads
-
Creating a TD for school
By runningonclouds in forum New To JavaReplies: 2Last Post: 03-19-2011, 08:23 AM -
problem in creating a class that should always run in the server
By harsha.udupa2008 in forum New To JavaReplies: 1Last Post: 07-14-2010, 02:56 PM -
school assignment: creating a simple bank, need help
By runlos in forum New To JavaReplies: 4Last Post: 11-02-2009, 03:41 PM -
Fraction Problem
By Jakora33 in forum New To JavaReplies: 6Last Post: 09-16-2009, 03:51 PM -
Rediculous problem?! Assigning a fraction to a double variable
By Tzoshirzup in forum New To JavaReplies: 3Last Post: 11-24-2008, 07:01 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks