nickerb2's Profile User Rating: -----

Reputation: 0 Apprentice
Group:
New Members
Active Posts:
10 (0.02 per day)
Joined:
22-February 13
Profile Views:
130
Last Active:
User is offline Mar 14 2013 08:10 AM
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   nickerb2 has not set their status

Posts I've Made

  1. In Topic: Syntax error in switch statement

    Posted 14 Mar 2013

    View Postpbl, on 14 March 2013 - 08:07 AM, said:

        Scanner kbReader = new Scanner(System.in);  
        Scanner kbReader1 = new Scanner(System.in);  
        Scanner kbContinue = new Scanner(System.in);  
    
    

    When you have a phone conversation with a friend do you hangup and redial after each sentence ?
    Have a single channel assigned to System.in at a time and use it every time



    I use nextLine with one and the other two im honestly not sure why i have two. :D
  2. In Topic: imbedding arrays, how to

    Posted 12 Mar 2013

    Okay thanks! and is it possible to not be limited just to holding ints and strings in one array,
    n say i was writing a program where I wanted to type in an employe's employe number and then it would come up with their name and how many hours they have worked? So the out put would be something like

    ENTER EMPLOYE #: (user enters number)2

    EMPLOYE SALLY JONES HAS WORKED 2 HOURS


    <the hours would be put in at the end of each work day or something and would stay in an array>
  3. In Topic: array, StringIndexOutOfBoundsException

    Posted 6 Mar 2013

    View Postpbl, on 06 March 2013 - 02:10 PM, said:

    Obviously the user simply enter a <ENTER> and the String length is 0 so getCharAt(0) fails

    And why 3 Scanner assigned to System.in ?
    When you talk on the phone with a friend do you hang up the phone and redial at each sentence ? Same thing here. Have only one Scanner with one channel assigned to System.in


    Well, for one reason thats all ive been taught, and two i beleive because i have all three scanners accepting input they should all be (System.in), correct?

    View Postjon.kiparsky, on 06 March 2013 - 09:29 AM, said:

    Quote

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(String.java:686)
    at menuW1.main(menuW1.java:70)


    Ah, thank you. This is useful. So you're trying to access the first character of some String, and it's out of bounds. This means that there's nothing in that String. This is happening at line 70 of your program, which is:

    choice = getChoice.charAt(0);
    
    



    So what is getChoice, and how did it get that way? Ah, this is a classic one - it's actually not likely you'd figure this out on your own.

    What is happening here is that the Scanner is playing you false. There's a subtle not-exactly-bug-but-not-what-you-expect in the "convenience" methods of Scanner which always trips people up.

    The nextInt method calls next() to get a token and then tries to parse that token as an int. If it manages to do so, it returns the int, otherwise and exception is thrown. So far, so good, but what you don't realize is that this only takes one token from the line you entered. The line you entered was something like 5\n, and the next() call took only the 5 part. The \n is still there.
    Now when you call nextLine(), that grabs everything up to the next newline character, discards the newline, and returns the rest. This means you get \n, minus the "\n" part: the result is "". So you have a String of length zero, and hence your exception.

    To kill this, call nextLine() and discard the result after you call nextInt(), if you're going to call a nextLine() on that scanner.

    Ugly, I know. I usually just avoid the "convenience" methods and parse the input myself - easier that way, I find.


    wow, i honestly cant believe mixed a "nextLine()" with a "nextInt()"
  4. In Topic: orphaned case

    Posted 25 Feb 2013

    View Postpbl, on 22 February 2013 - 09:20 AM, said:

    There can't be 36 possibilities for an orphan case. You have a case: statement outside a switch

    You have to be masochist not to split that long method between smaller ones. You just get what you desserve.

    Indent your code correctly and it will become obvious, I removed a lot of lines of code (which splitting into methods will have done) just to reveal the bad alignment and the extra break;

    import java.io.*;
    import java.util.*;
    
    public class menuW{
    	public static void main(String[] args){
    		Scanner kbReader1 = new Scanner(System.in);
    		char choice;
    
    
    		do{
    			System.out.println("");
    			System.out.println("");
    			getChoice = kbReader.nextLine();
    			choice = getChoice.charAt(0);
    
    			switch(choice){
    
    
    			case 'A':
    			case 'a':
    				System.out.println("");
    				getChoice1= kbReader1.nextInt();
    
    
    				switch(getChoice1){
    				case 1:
    					System.out.println("\n");
    					break;
    				case 2:
    					System.out.println("20 test");
    					myArray[0]=1;       
    					myArray[19]=33;
    					break;
    				case 3: 
    					System.out.println("User input");
    					while (!done1){		
    					}
    					break;
    				}
    
    			}    
    			System.out.print("Please Press <ENTER> to continue");
    			dummy = kbContinue.nextLine();
    			break;
    
    		case 'B':
    		case 'b':
    			System.out.println("");
    			dummy = kbContinue.nextLine();
    			break;
    
    		case 'C':
    		case 'c':
    			System.out.println("");
    			dummy = kbContinue.nextLine();
    			break;
    
    		case 'D':
    		case 'd':
    			System.out.println("");
    
    			System.out.print("Please Press <ENTER> to continue");
    			dummy = kbContinue.nextLine();
    			break;
    
    		case 'e':
    		case 'E':
    			System.out.println("");
    			break;
    
    		case 'f':
    		case 'F':
    			System.out.println("");
    			break;
    
    		case 'g':
    		case 'G':
    			System.out.println("");
    			break;
    
    		case 'h':
    		case 'H':
    			System.out.println("");
    			break;
    
    		case 'Z':
    		case 'z':
    			done = true;
    			break;
    
    		default:
    			System.out.println("");
    			dummy =kbContinue.nextLine();
    
    		}while(!done);
    		System.out.println("");
    		System.out.println("==========PROGRAM END==========");
    		System.out.println("");
    	}
    }
    
    



    Big thanks! Just as a question, is there any preferred way, one should indent, and space their code?

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Click here to e-mail me

Friends

nickerb2 hasn't added any friends yet.

Comments

nickerb2 has no profile comments yet. Why not say hello?