Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 44
  1. #21
    lithium002 is offline Member
    Join Date
    Nov 2009
    Posts
    23
    Rep Power
    0

    Default

    If you put the code and run it, and input a value, say 44, it gives you an error.
    If you input the value, say A, it gives you an error.

    Those two work perfect.

    However, if you input, say 4, it gives a blank line. Not really sure why

  2. #22
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    one of my comments is a slider bar thats worse than WTF so sorry this sight is not perfect

  3. #23
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Quote Originally Posted by lithium002 View Post
    Excuse me? I need help, I don't need anyone to finish my assignment. If I needed the former I wouldn't have spent this long trying to do it myself. :)

    I already explained what my problem is. I can't use prompt or catch and try or anything else because we haven't covered those things in our course.

    I'm having trouble with the code I already provided and need someone to tell me what I'm doing wrong. I know you're trying to help, I honestly appreciate it, but seriously, I'm not asking you to do my homework for me. That was arrogant and uncalled for in my opinion.
    Well, that's good, because you've been coming off like what I'm doing doesn't matter one bit.

    Like I asked, where are you having the problem? Your code runs fine, so if you need help, I need specific questions - what are you confused about?
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  4. #24
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Quote Originally Posted by lithium002 View Post
    If you put the code and run it, and input a value, say 44, it gives you an error.
    If you input the value, say A, it gives you an error.

    Those two work perfect.

    However, if you input, say 4, it gives a blank line. Not really sure why
    You changed the code from the previous versions - there no longer is a break statement for when the input is valid.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  5. #25
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    it has to do with iteration yyou can make your own class that extends Exception and try and catch alll of your crap......no assholeness intended

  6. #26
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    The OP already said no try-catch, move on...
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  7. #27
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    i just finally learned that OP = origial Poster

  8. #28
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    at least it isnt oracle pathname

  9. #29
    lithium002 is offline Member
    Join Date
    Nov 2009
    Posts
    23
    Rep Power
    0

    Default

    Hello CodesAway,

    First, let me show you the code

    Java Code:
    	private static void getInput() // ask for all the information you need from the user
    	{ 
    		int x;
    		
    		System.out.print("The x co-ordinate: ");
    		Scanner input = new Scanner(System.in);
    
    		while (input.hasNext())
    		{
    			if (input.hasNextInt())
    			{
    				x = input.nextInt();
    				if ( x > 40 || x < 0)
    				{
    					System.out.println("Wrong");
    					System.out.print("Try again: ");
    				}
    				
    				else
    				break;
    			}
    				
    			else
    			{
    				input.next();
    				System.out.println("This is not a valid value!");
    				System.out.print("The X co-ordinate: ");
    			}
    		}
    		
    		x = input.nextInt();
    
    		System.out.print("The Y co-ordinate: "); // the y-axis location on the graph
    		int y = input.nextInt();
    	    
    		System.out.print("Please enter a width for your rectangle: "); // the width of the rectangle to be plotted
    		int width = input.nextInt();
            
    		System.out.print("Now enter a height for your rectangle: "); // the height of the rectangle to be plotted
    		int height = input.nextInt();
    	    
    		drawGraph(width, height, x, y); // now draw the graph by calling the function based on the variables
    Based on the following function, what happens is the program starts by asking the first thing in the loop.

    If I input 44 or a character, the function works fine. If I enter a valid integer based on the restrictions, a blank line appears. That blank line, I think, is the "x = input.nextInt();" right after the while loop ends, which means the previous one is only stored within the loop and cannot be used outside..

    Now, if I remove "x = input.nextInt();" after the loop, the function "drawGraph" gives me an error saying "x" was not initiliazed.

    If I wanted to use the "int x" value within the function outside the function, how would I do it?

    That is my problem. :(

  10. #30
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Quote Originally Posted by lithium002 View Post
    Hello CodesAway,

    First, let me show you the code

    Java Code:
    	private static void getInput() // ask for all the information you need from the user
    	{ 
    		int x;
    		
    		System.out.print("The x co-ordinate: ");
    		Scanner input = new Scanner(System.in);
    
    		while (input.hasNext())
    		{
    			if (input.hasNextInt())
    			{
    				x = input.nextInt();
    				if ( x > 40 || x < 0)
    				{
    					System.out.println("Wrong");
    					System.out.print("Try again: ");
    				}
    				
    				else
    				break;
    			}
    				
    			else
    			{
    				input.next();
    				System.out.println("This is not a valid value!");
    				System.out.print("The X co-ordinate: ");
    			}
    		}
    		
    		x = input.nextInt();
    
    		System.out.print("The Y co-ordinate: "); // the y-axis location on the graph
    		int y = input.nextInt();
    	    
    		System.out.print("Please enter a width for your rectangle: "); // the width of the rectangle to be plotted
    		int width = input.nextInt();
            
    		System.out.print("Now enter a height for your rectangle: "); // the height of the rectangle to be plotted
    		int height = input.nextInt();
    	    
    		drawGraph(width, height, x, y); // now draw the graph by calling the function based on the variables
    Based on the following function, what happens is the program starts by asking the first thing in the loop.

    If I input 44 or a character, the function works fine. If I enter a valid integer based on the restrictions, a blank line appears. That blank line, I think, is the "x = input.nextInt();" right after the while loop ends, which means the previous one is only stored within the loop and cannot be used outside..

    Now, if I remove "x = input.nextInt();" after the loop, the function "drawGraph" gives me an error saying "x" was not initiliazed.

    If I wanted to use the "int x" value within the function outside the function, how would I do it?

    That is my problem. :(
    Oh, ok, thank you. No wonder I was so confused. It's not local to the function, nextInt ALWAYS reads another int value - it doesn't store one locally. So, since it's reading from the keyboard, when you call the method a second time, it expects you to input a second int value, and waits for you to do so.

    You should remove the second nextInt, like you thought. The value is stored, and can be used as you need it. The reason it's complaining is because "x" is not initialized.


    The compiler is complaining because, for example, if input.hasNext() were to return false, "x" would never be initialized. However, for my knowledge, this is not possible, but the compiler doesn't realize this. The compiler only knows that "in theory", as a boolean value, if it were false, then "x" would not be initialized, and thus it throws a compile error. If you initialize the value (to say 0), it will run fine.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  11. #31
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    You want the user to enter 4 integers so you should only have 4 input.nextInt calls.

  12. #32
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    you need to set the input requirements and if they don't fulfill the input requirements you neeed to re prompt them;

  13. #33
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    i am going to use your code to show yo how to handle input error like chars

  14. #34
    lithium002 is offline Member
    Join Date
    Nov 2009
    Posts
    23
    Rep Power
    0

    Default

    Thank you very much! By initializing int i to 0, the code worked fine!

    Thanks a lot CodesAway, and sorry for the initial trouble.

  15. #35
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Awesome, glad it works.

    Sorry about my behavior as well. I get WAY too emotional at times, and I got really confused as to where your problem was, so that made it even worse.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  16. #36
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    Java Code:
    x = input.nextInt();// this wont work because the chars will be transformed into integers  
      if(x == char || x == String)
           {recalll initial method} 
       else if x < manimun in t vakue  
    		System.out.print("The Y co-ordinate: "); // the y-axis location on the graph
    		int y = input.nextInt();
    	    
    		System.out.print("Please enter a width for your rectangle: "); // the width of the rectangle to be plotted
    		int width = input.nextInt();
            
    		System.out.print("Now enter a height for your rectangle: "); // the height of the rectangle to be plotted
    		int height = input.nextInt();
    	    
    		drawGraph(width, height, x, y); // now draw the graph by calling the function based on the variables 
     
    Based on the following function, what happens is the program starts by asking the first thing in the loop.
    Last edited by aaroncarpet; 11-16-2009 at 09:59 AM. Reason: decorum

  17. #37
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Quote Originally Posted by aaroncarpet View Post
    Java Code:
    x = input.nextInt();// this wont work because the chars will be transformed into integers  
      if(x == char || x == String)
           {recalll initial method} 
       else if x < manimun in t vakue  
    		System.out.print("The Y co-ordinate: "); // the y-axis location on the graph
    		int y = input.nextInt();
    	    
    		System.out.print("Please enter a width for your rectangle: "); // the width of the rectangle to be plotted
    		int width = input.nextInt();
            
    		System.out.print("Now enter a height for your rectangle: "); // the height of the rectangle to be plotted
    		int height = input.nextInt();
    	    
    		drawGraph(width, height, x, y); // now draw the graph by calling the function based on the variables 
     
    Based on the following function, what happens is the program starts by asking the first thing in the loop.
    The chars aren't converted to ints - they can't be. Everything is read as a String and then parsed - using regular expressions (check the code out).

    Can you give an example?
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  18. #38
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    you need an iterater for shell input

    karat listener
    Last edited by aaroncarpet; 11-16-2009 at 10:17 AM. Reason: c++ lingo

  19. #39
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    alright compiling now

  20. #40
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    Exception.Thrown...........who is going to catch it...... nulll poimter exceptiom.....input not an integer as expected....................ascii value od characters are nasty math let alone unicode

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Error: unexpected type
    By silvia in forum New To Java
    Replies: 3
    Last Post: 02-05-2010, 09:54 PM
  2. Matlab data type implementation & speed
    By uzil24 in forum Advanced Java
    Replies: 3
    Last Post: 10-24-2009, 02:00 AM
  3. sychronized data type
    By java girl in forum Threads and Synchronization
    Replies: 3
    Last Post: 02-13-2009, 08:37 AM
  4. error while retrieving data from data base
    By kirtesh4u in forum New To Java
    Replies: 5
    Last Post: 11-15-2008, 04:10 PM
  5. Replies: 1
    Last Post: 11-11-2008, 08:18 PM

Tags for this Thread

Posting Permissions

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