Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 28th, 2008, 04:32 PM
jaysee jaysee is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Posts: 1 jaysee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 26 sec
Reputation Power: 0
Java noob (first assignment) need scanner help

anyone know how to get input and output on the same line while using the Scanner? if not, is there an alternative where i can use the same input way, #/# , and get them into longs.

mine looks like

PI: 3.141592653589793
Current: 22/7
~ 3.142857142857143
Next: 179/57 ~ 3.1403508771929824

while on the assignment it says it's supposed to be

http://img2.freeimagehosting.net/uploads/c57d1b0dd1.jpg

aka
PI: 3.141592653589793
Current: 22/7 ~ 3.142857142857143
Next: 179/57 ~ 3.1403508771929824

(it has to look EXACTLY like that or i will get points taken away)


I am not using println, just print. we have to input like "355/34" and the only way i know is too use a scanner then use for loops to put like 355 and 34 into StringBuffers and then longs to divide into a double. if theres another way to have the input like "322/31" and still get the output without using a scanner, please enlighten me.




Code:
import java.util.*;

public class NeedHelp_jaysee
{
  public static void main(String[] args)
  {
    int i2 = 0;
    long numerator, denominator;
    double currentAnswer;
    final double EPSILON = Math.abs(Math.PI - 22.0/7.0);
    String current, numToString, denomToString;
    StringBuffer num = new StringBuffer(), denom = new StringBuffer();
    Scanner keyboard = new Scanner(System.in);
    
    
    System.out.println("PI:\t " + Math.PI);
    System.out.print("Current: ");
    current = keyboard.next();
    
    for(int i =0; i < current.length(); i++)
    {
      if(current.charAt(i) == '/')
      {
      i2 = i + 1;
      i = current.length();
      }
      else 
      num.append(current.charAt(i));
   }
    for(; i2 < current.length(); i2++)
    {
      denom.append(current.charAt(i2));
    }
    
    numToString = num.toString();
    denomToString = denom.toString();
    numerator = Long.parseLong(numToString);
    denominator = Long.parseLong(denomToString);
    currentAnswer = (double)numerator/denominator;
    System.out.print(" ~ " + currentAnswer);
    
    while(Math.abs(Math.PI - currentAnswer) >= EPSILON)
    {
      if(currentAnswer < Math.PI)
      {
        numerator++;
      }
      else
        denominator++;
      currentAnswer = (double)numerator/denominator;
    }
    System.out.print("\nNext:\t " + numerator + "/" + denominator + " ~ " + currentAnswer);
    
    
  }
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Java noob (first assignment) need scanner help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway