# creating fraction class for school get weird problem

• 09-21-2011, 04:47 AM
creating fraction class for school get weird problem
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
{

int x = 0;

System.out.println("Enter fraction in x/y format >");
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.");
}

}
}

The process Completes and the user is prompted to enter a fraction
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?
• 09-21-2011, 04:57 AM
Fubarable
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?

Code:

System.out.println(frac.charAt(0));
x = frac.charAt(0);
System.out.println((char)x); // *** note the difference!
System.out.println();

• 09-21-2011, 05:10 AM
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?
• 09-21-2011, 05:27 AM
Fubarable
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
Rameshwar Soni
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 write
Code:

Scanner s=new Scanner(System.in);
int no=s.nextInt();  // no need to parse

• 09-21-2011, 05:40 AM
Re: creating fraction class for school get weird problem
HA!
Fixed it

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
sunde887
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
Rameshwar Soni
Re: creating fraction class for school get weird problem
Quote:

Originally Posted by sunde887
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.

Can you give an small example?