Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default creating fraction class for school get weird problem

    Java 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.");
        	}
        	
    }
    }
    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?
    Last edited by sunde887; 09-21-2011 at 05:37 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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.

  3. #3
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default 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?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: creating fraction class for school get weird problem

    Use a String rather than a char and Integer.parseInt(...) will work fine.

  5. #5
    Join Date
    Sep 2011
    Location
    Mumbai, India
    Posts
    35
    Rep Power
    0

    Default 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
    Java Code:
    Scanner s=new Scanner(System.in);
    int no=s.nextInt();   // no need to parse

  6. #6
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default 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

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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.

  8. #8
    Join Date
    Sep 2011
    Location
    Mumbai, India
    Posts
    35
    Rep Power
    0

    Default Re: creating fraction class for school get weird problem

    Quote Originally Posted by sunde887 View Post
    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?

Similar Threads

  1. Creating a TD for school
    By runningonclouds in forum New To Java
    Replies: 2
    Last Post: 03-19-2011, 08:23 AM
  2. problem in creating a class that should always run in the server
    By harsha.udupa2008 in forum New To Java
    Replies: 1
    Last Post: 07-14-2010, 02:56 PM
  3. Replies: 4
    Last Post: 11-02-2009, 03:41 PM
  4. Fraction Problem
    By Jakora33 in forum New To Java
    Replies: 6
    Last Post: 09-16-2009, 03:51 PM
  5. Replies: 3
    Last Post: 11-24-2008, 07:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •