Results 1 to 3 of 3

Thread: Can't Input Last Character; Program Ends Abruptly

  1. #1

    Thread Starter
    Lively Member fundean's Avatar
    Join Date
    Apr 2001
    Posts
    98

    Can't Input Last Character; Program Ends Abruptly

    import java.io.*;

    public class Ascii {


    public static void main(String[] args) {

    TextReader UserInput = new TextReader(System.in);
    System.out.println ("Please type the start character");
    char startChar = UserInput.readChar();

    System.out.println ("Please type the last character");
    char lastChar = UserInput.readChar();

    if (lastChar < startChar){
    System.out.println ("Error, last character is before start character");
    }

    for (char ascii = startChar; ascii <= lastChar; ++ascii){

    System.out.print (ascii);
    }

    } // end of main

    } // end class


    The output of the above code gives me the following:

    Please type the start character
    a
    Please type the last character
    Error, last character is before start character
    Press any key to continue...

    The program doesn't give me time to input the last character. Any suggestions?

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Do you press return after entering the first character?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Try this........
    Code:
    
     import java.io.*; 
    
     public class A{ 
      public static void main(String[] args){
       try{
       BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
       System.out.println("Please enter your first character");
       String firstchar = buff.readLine();
       System.out.println("Please enter your last character"); 
       String lastchar = buff.readLine();
    
       char c1 = firstchar.charAt(0);
       char c2 = lastchar.charAt(0); 
      
       if(c1 > c2){
       System.out.println("First char comes after second char"); 
       }else{
           System.out.println("First character comes before last char"); 
       }
    
       for (char ascii = c1; ascii <= c2; ++ascii){ 
         System.out.print(ascii); 
       }  
      }catch(IOException e){System.err.println(e);}
     }
    }

Posting Permissions

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



Click Here to Expand Forum to Full Width