The Artima Developer Community
Sponsored Link

Java Answers Forum
Manipoulating user input longs

2 replies on 1 page. Most recent reply: Aug 7, 2003 2:19 PM by Mik Lernout

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Philip Pompili

Posts: 12
Nickname: ppompili
Registered: Jul, 2003

Manipoulating user input longs Posted: Aug 6, 2003 3:36 PM
Reply to this message Reply
Advertisement
Hello I need to take a user input of a long and then make sure that it is only 8 chars long. I have tried converting them to strings but that messes up the number. Can someone help me count the numbers and chop the numbers off the end. Thanks


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Manipoulating user input longs Posted: Aug 7, 2003 11:45 AM
Reply to this message Reply
import java.text.*;
import java.io.*;
import java.util.*;
 
public class Test{
 
    public Test(){
        init();
        test();
    }
    
    public static void main(String[] args){
        new Test();
    }
    
    public void init(){
    
    }
    
    public void test(){
        String s = getInput();
        while (s.length() > 6){
            /* chop off left end */
            s = s.substring(1);
            /* chop off right end */
            //s = s.substring(0,s.length()-1);
        }
        long n = toLong(s);
        if (n != -1){
            System.out.println("six digit number: " + String.valueOf(n));
        }else{
            System.out.println("That was not a valid a six digit digit number");
        }
    }
    
    public String getInput(){
        String input = "";
        try{
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
            System.out.println("Enter a six digit digit number");
            input = br.readLine(); 
        }catch(IOException ioe){
            System.err.println("IOException: " + ioe.getMessage());
        }
        return input;
    }
    
    /** String to long. Returns -1 if string is not properly formatted. */
    private long toLong(String s){
        long n = -1;
        try{
            n = Long.parseLong(s);
        }catch(NumberFormatException nfe){
            System.err.println("NumberFormatException: " + nfe.getMessage());
        }
        return n;
    }
    
    /** long to String. */
    private String toString(long n){
        return String.valueOf(n);
    }
 
    
 
}  return n;
    }
    

Mik Lernout

Posts: 10
Nickname: miklernout
Registered: Jan, 2003

Re: Manipoulating user input longs Posted: Aug 7, 2003 2:19 PM
Reply to this message Reply
Or, to make things a bit easier:

public class Test{    
 
    public int MAX_NUMBER_OF_DIGITS = 8;
 
    public static void main(String[] args){        
 
        if (args.length != 1){
            System.out.println("You will have to call this class with exactly one argument, sorry!");
        }
        try{
            long theResultLong = getTrimmedLong(args[0]);
            System.out.println("We found the long " + theResultLong);
        } catch (NumberFormatException ex){
            System.out.println("This (" + args[0] + ") is not a long at all!!");
        }
    }
 
    public static long getTrimmedLong(String longAsString){
 
        if (longAsString.length() > MAX_NUMBER_OF_DIGITS){
            System.out.println("This (" + longAsString + ") is too big, let's trim it to " + MAX_NUMBER_OF_DIGITS + " digits");
            longAsString = longAsString.substring(0, MAX_NUMBER_OF_DIGITS > 1);
            System.out.println("The result of trimming is " + longAsString);
        }
        return Long.parseLong(longAsString); 
    }
}


Really try to use the method / utilities available in teh Java API itself, it will relieve you from a lot of stress :-)

Flat View: This topic has 2 replies on 1 page
Topic: java problem Previous Topic   Next Topic Topic: Applet - Dynamic Parameter

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use