dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: [RESOLVED] strange loop issue after .read()

  1. #1
    Join Date
    Jul 2016
    Posts
    3

    [RESOLVED] strange loop issue after .read()

    Easiest way to explain the problem is to show it.

    Java version information:
    Code:
    Java -version
    java version "1.8.0_92"
    Java(TM) SE Runtime Environment (build 1.8.0_92-b14)
    Java HotSpot(TM) 64-Bit Server VM (build 25.92-b14, mixed mode)
    Simple sloppy newbie program:
    work() and brake() methods not included as they print the word "work" and "break" respectively and are functioning. After calling one of those methods, the default: still runs twice... :
    Code:
    import java.io.*;
    
    public class TimeTracker {
    	public static void main(String args[]) throws IOException {
    		InputStreamReader cin = null;
    	try {
    		cin = new InputStreamReader(System.in);
    		System.out.println("Status: working \n");
    		char c;
    		do {
    			c = (char) cin.read();
    			switch (c){
    				case 'w':
    					work();
    					break;
    				case 'b':
    					brake();
    					break;
    				default:
    					System.out.println("Please enter one of the following:\n  w - work begin / end\n  b - break begin / end\n  q - quit\n");
    			};
    		} while (c != 'q'); 
    	}
    	finally {
    		if(cin != null) {
    			cin.close();
    		}
    	}
    	}
    		public static void work(){
    		System.out.println("work");
    		}
    		public static void brake(){
    			System.out.println("break");
    		}
    }
    The problem:
    Code:
    java TimeTracker
    Status: working
    
    k
    Please enter one of the following:
      w - work begin / end
      b - break begin / end
      q - quit
    
    Please enter one of the following:
      w - work begin / end
      b - break begin / end
      q - quit
    
    Please enter one of the following:
      w - work begin / end
      b - break begin / end
      q - quit
    
    w
    work
    Please enter one of the following:
      w - work begin / end
      b - break begin / end
      q - quit
    
    Please enter one of the following:
      w - work begin / end
      b - break begin / end
      q - quit
    
    q
    Please enter one of the following:
      w - work begin / end
      b - break begin / end
      q - quit
    As you can see, the default case is being run 3 times whenever input is received. It still runs 2 times even when another case is selected. It even runs once when the exit input is entered (granted I anticipated this as the while check is after the default case, I planned on fixing that particular issue later on via a case 'q':). I have no idea what is going on and I apologize if this is is a simple little thing. I am (obviously) new to java and was trying to make a little time tracking program for practice, as you can see, I haven't gotten very far.

    If you wish to give advice on how to more correctly go about the process of collecting a single letter user input and running a method based on the letter selected, I'd love to receive it.

    I'm more looking for a little explanation as to why the .read() seems to be spitting out junk 2 times after a real user input was made (or what stupid mistake it is I made to cause that). I changed the program to have a counter to only display the instructional message once each input, but that wasn't a satisfying fix as it didn't help me understand what was going on at all.

    Thank you!

  2. #2
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: strange loop issue after .read()

    Think of what the program is reading. You type a single letter and then the enter/return key. The program sees two additional characters after the letter you typed corresponding to the enter key (assuming you are running it in Windows): a newline (0x0d) and a line feed (0x0a).

  3. #3
    Join Date
    Jul 2016
    Posts
    3

    Re: strange loop issue after .read()

    I did not honestly realize this was a thing, lol. Thanks for clearing that up for me!

    Now for me to work out a solution to this now simple problem. Thank you again! I'm very new to this level of programming, I'm used to much more newbie friendly things like VBA for excel and the occasional basic level LUA. I'm hoping to slowly work my way down in programming levels to assembly, mostly for my own knowledge as this is a hobby for me.
    I'
    Anyway, thanks again! I'll let you know if I run into any issues fixing this. I'll also repost with the solved code once I'm done and mark resolved.

  4. #4
    Join Date
    Jul 2016
    Posts
    3

    Re: strange loop issue after .read()

    Ok, problem solved!!!!

    Basically I changed from using only the InputStreamReader to using a scanner. I have the scanner read each line, then pulling from the first character, set it to my char c and go from there. This removes the annoying false read of the return key characters and works beautifully while also avoiding printing the default case for every character within the line (annoying thing from initial attempts at using a scanner).

    Fixed blocks.
    Code:
    import java.util.*
    --------------------------------------------------------------------------------------------
    	Scanner sc = new Scanner(System.in).useDelimiter("");
    	try {
    		System.out.println("Status: working \n");
    		char c;
    ---------------------------------------------------------------
    	finally {
    		sc.close();
    	}
    Full code working nicely.
    Code:
    import java.io.*;
    import java.util.*;
    
    public class TimeTracker {
    	public static void main(String args[]) throws IOException {
    	Scanner sc = new Scanner(System.in);
    	try {
    		System.out.println("Status: working \n");
    		char c;
    		do {
    			c =  sc.nextLine().charAt(0);
    			switch (c){
    				case 'w':
    					work();
    					break;
    				case 'b':
    					brake();
    					break;
    				default:
    						System.out.println("Please enter one of the following:\n  w - work begin / end\n  b - break begin / end\n  q - quit\n");
    			};
    		} while (c != 'q'); 
    	}
    	finally {
    		sc.close();
    	}
    	}
    		public static void work(){
    		System.out.println("work");
    		}
    		public static void brake(){
    			System.out.println("break");
    		}
    }
    Last edited by syphontwo; July 11th, 2016 at 08:56 AM.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)