Results 21 to 40 of 44
- 11-16-2009, 09:00 AM #21
Member
- Join Date
- Nov 2009
- Posts
- 23
- Rep Power
- 0
- 11-16-2009, 09:03 AM #22
one of my comments is a slider bar thats worse than WTF so sorry this sight is not perfect
- 11-16-2009, 09:03 AM #23
- 11-16-2009, 09:05 AM #24
- 11-16-2009, 09:08 AM #25
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
- 11-16-2009, 09:09 AM #26
The OP already said no try-catch, move on...
CodesAway - codesaway.info
writing tools that make writing code a little easier
- 11-16-2009, 09:12 AM #27
i just finally learned that OP = origial Poster
- 11-16-2009, 09:13 AM #28
at least it isnt oracle pathname
- 11-16-2009, 09:15 AM #29
Member
- Join Date
- Nov 2009
- Posts
- 23
- Rep Power
- 0
Hello CodesAway,
First, let me show you the code
Based on the following function, what happens is the program starts by asking the first thing in the loop.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
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. :(
- 11-16-2009, 09:24 AM #30
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-16-2009, 09:24 AM #31
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You want the user to enter 4 integers so you should only have 4 input.nextInt calls.
- 11-16-2009, 09:31 AM #32
you need to set the input requirements and if they don't fulfill the input requirements you neeed to re prompt them;
- 11-16-2009, 09:40 AM #33
i am going to use your code to show yo how to handle input error like chars
- 11-16-2009, 09:47 AM #34
Member
- Join Date
- Nov 2009
- Posts
- 23
- Rep Power
- 0
Thank you very much! By initializing int i to 0, the code worked fine!
Thanks a lot CodesAway, and sorry for the initial trouble.
- 11-16-2009, 09:50 AM #35
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
- 11-16-2009, 09:58 AM #36
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
- 11-16-2009, 10:01 AM #37
- 11-16-2009, 10:01 AM #38
you need an iterater for shell input
karat listenerLast edited by aaroncarpet; 11-16-2009 at 10:17 AM. Reason: c++ lingo
- 11-16-2009, 10:03 AM #39
alright compiling now
- 11-16-2009, 10:11 AM #40
Similar Threads
-
Error: unexpected type
By silvia in forum New To JavaReplies: 3Last Post: 02-05-2010, 09:54 PM -
Matlab data type implementation & speed
By uzil24 in forum Advanced JavaReplies: 3Last Post: 10-24-2009, 02:00 AM -
sychronized data type
By java girl in forum Threads and SynchronizationReplies: 3Last Post: 02-13-2009, 08:37 AM -
error while retrieving data from data base
By kirtesh4u in forum New To JavaReplies: 5Last Post: 11-15-2008, 04:10 PM -
Hibernate mapping error Provided id of the wrong type with composite id
By zigzag in forum JDBCReplies: 1Last Post: 11-11-2008, 08:18 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks