Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Making a Java program Repeat.

    I have been tasked by my Professor to create a program in java that counts strings of input. It also must distinguish between White spaces and characters. And give 3 different outputs.

    I have created the class, and its parent class for reading the input and parsing the values.

    both classes compile and run perfectly. But I cannot get the process to repeat until the user ends it. What am I missing? I know it is blatantly obvious, but I cannot see it. I really feel like an idiot for having to come here and ask, but I am drawing a blank. I will go back and reference my book while I wait for a response.


    Here is the code for the Input.class

    PHP Code:

    import java
    .io.*;
    import java.lang.NumberFormatException;

    public class 
    Input
    {
        public static 
    String readString()
        {
            
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 1);
            
    String str "";

            try
            {
                
    str br.readLine();
            }
            catch (
    IOException ioe) { System.out.println(ioe);
            }

            return 
    str;
        }

        public static 
    int readInt()
        {
            return 
    Integer.parseInt(readString());
        }

        public static 
    byte readByte()
        {
            return 
    Byte.parseByte(readString());
        }

        public static 
    short readShort()
        {
            return 
    Short.parseShort(readString());
        }

        public static 
    long readLong()
        {
            return 
    Long.parseLong(readString());
        }

        public static 
    float readFloat()
        {
            return 
    Float.parseFloat(readString());
        }

        public static 
    double readDouble()
        {
            return 
    Double.parseDouble(readString());
        }

        public static 
    char readChar()
        {
            return 
    readString().charAt(0) ;
        }

        public static 
    boolean readBoolean()
        {
            return 
    Boolean.valueOf(readString()).booleanValue();
        }

        public static 
    String replace(String sint ichar c)
        {
            return ( 
    s.substring(0,i) + s.substring(i+1) );
        }

        public static 
    boolean isInteger(String s)
        {
            try
            {
                
    Integer.parseInt(s);
                return 
    true;
            }
            catch (
    NumberFormatException e)
            {
                return 
    false;
            }
        }

    And here is the Code for the WhiteSpace.java class.

    PHP Code:
    public class WhiteSpace
    {
        public static 
    void main(String [] args)
        {
            
    System.out.print("Enter a string: ");
            
    String str Input.readString();
            
    int length str.length();
            
    String trimmedStr str.trim();
            
    int trimmedLength trimmedStr.length();

            
    int nonBlankCount 0;
            for(
    int i 0trimmedLengthi++)
            {
                if (
    trimmedStr.charAt(i) != ' ')
                
    nonBlankCount++;
            }

            
    System.out.println("The string, "str);
            
    System.out.println ("has " length " characters, including all blanks,");
            
    System.out.println ("and " trimmedLength " characters, trimmed of all leading and trailing blanks,");
            
    System.out.println ("and " nonBlankCount " non blank characters.");
        }



  • #2
    New Coder
    Join Date
    Feb 2011
    Posts
    48
    Thanks
    1
    Thanked 2 Times in 2 Posts
    This modification will read repeatedly, until the user enters "end" and closes br when you don't need it anymore:

    Code:
    public static String readString() 
        { 
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 1); 
            String str = ""; 
    
         do {
            try 
            { 
                str = br.readLine(); 
            } 
            catch (IOException ioe) { System.out.println(ioe); 
            } 
         while (str.equals("end") == false);
         br.close();
    
            return str; 
        }

  • #3
    New Coder
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by DistantJob View Post
    This modification will read repeatedly, until the user enters "end" and closes br when you don't need it anymore:

    Code:
    public static String readString() 
        { 
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 1); 
            String str = ""; 
    
         do {
            try 
            { 
                str = br.readLine(); 
            } 
            catch (IOException ioe) { System.out.println(ioe); 
            } 
         while (str.equals("end") == false);
         br.close();
    
            return str; 
        }
    Thanks, I had just found it in my Java book. But you code seems to be more streamlined than the code I created.

    Thanks again.

    Edit: I tried compiling after adding the code, and now I get 3 errors. I will see what I can do.. It is not recognizing the while statement. and is wanting a ) and a } at the end of the code now?? I will Go through and try to find the errors.

    Here is the code as it stands now.

    PHP Code:
    import java.io.*;
    import java.lang.NumberFormatException;

    public class 
    Input
    {
        public static 
    String readString()
            {
                
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 1);
                
    String str "";

             do {
                try
                {
                    
    str br.readLine();
                }
                catch (
    IOException ioe) { System.out.println(ioe);
                }
             while (
    str.equals("end") == false);
             
    br.close();

                return 
    str;
        }
        
        public static 
    int readInt()
        {
            return 
    Integer.parseInt(readString());
        }

        public static 
    byte readByte()
        {
            return 
    Byte.parseByte(readString());
        }

        public static 
    short readShort()
        {
            return 
    Short.parseShort(readString());
        }

        public static 
    long readLong()
        {
            return 
    Long.parseLong(readString());
        }

        public static 
    float readFloat()
        {
            return 
    Float.parseFloat(readString());
        }

        public static 
    double readDouble()
        {
            return 
    Double.parseDouble(readString());
        }

        public static 
    char readChar()
        {
            return 
    readString().charAt(0) ;
        }

        public static 
    boolean readBoolean()
        {
            return 
    Boolean.valueOf(readString()).booleanValue();
        }

        public static 
    String replace(String sint ichar c)
        {
            return ( 
    s.substring(0,i) + s.substring(i+1) );
        }

        public static 
    boolean isInteger(String s)
        {
            try
            {
                
    Integer.parseInt(s);
                return 
    true;
            }
            catch (
    NumberFormatException e)
            {
                return 
    false;
            }
        }

    And here are the compiling errors.

    Code:
    C:\Users\LeeSchool\TTC\3rd Trimester\Data Structures(Java)\Assignments\Chapter 8\Input.java:31: while expected
    	public static int readInt()
            ^
    C:\Users\LeeSchool\TTC\3rd Trimester\Data Structures(Java)\Assignments\Chapter 8\Input.java:88: '(' expected
    }
    ^
    C:\Users\LeeSchool\TTC\3rd Trimester\Data Structures(Java)\Assignments\Chapter 8\Input.java:89: '}' expected
    ^
    3 errors
    
    Tool completed with exit code 1
    Last edited by bookittysdad; 02-24-2011 at 05:38 PM. Reason: Coding error.

  • #4
    New Coder
    Join Date
    Feb 2011
    Posts
    48
    Thanks
    1
    Thanked 2 Times in 2 Posts
    You are right, my friend, a } is missing, so here's the updated code:

    Code:
        public static String readString()
            {
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 1);
                String str = "";
    
             do {
                    try
                    {
                       str = br.readLine();
                    }
                    catch (IOException ioe) { System.out.println(ioe);}
                 }
             while (str.equals("end") == false);
             br.close();
    
             return str;
        }


  •  

    Posting Permissions

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