8 Replies - 1357 Views - Last Post: 21 September 2011 - 12:09 PM Rate Topic: -----

#1 Gantz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 21-May 11

Console will not accept input / beginner level java problem

Posted 20 September 2011 - 10:35 AM

Here is the applicable code:
do{                                            // beginning of 'do-while' loop
		rectAcreate(xCordA, yCordA, widthA, heightA); //creating rectangles A & B
		rectBcreate(xCordB, yCordB, widthB, heightB); // 
		
		doesRectIntersPointorNotInters(rectA, rectB); //check to see if the rectangles do not intersect or if they intersect at point
		
		if (rectInterAtPoint == true)
			System.out.println("The rectangles intersect at the following point: " + pointIntersect);
		else
			rectInterAtPoint = false;
		
		if (rectDoesNotIntersect == true)
			System.out.println("The rectangles do not intersect.");
		else
			rectDoesNotIntersect = false;
		
		if ((rectInterAtPoint == false) && (rectDoesNotIntersect == false))
			rectIntersect(rectA, rectB); //calculating/printing the intersection
		
		rectUnion(rectA, rectB); // calculating/printing the union
		
		keepGoing = false;                                           
		System.out.print("Do you want to study two more rectangles>");      // *** here is the problem, don't know why can't input
		String response = scan.nextLine();									// wtf??
		
		
		if (response.startsWith("y"))
		{
		keepGoing = true;
		}
		if (response.startsWith("y"))
		{
		keepGoing = true;
		}
		
		
		
		}while(keepGoing == true);



Here is program output:

Quote

Please enter the x coordinate of the upper left corner>10
Please enter the y coordinate of the upper left corner>10
Please enter the x coordinate of the lower right corner>15
Please enter the y coordinate of the lower right corner>15

Please enter the x coordinate of the upper left corner>12
Please enter the y coordinate of the upper left corner>12
Please enter the x coordinate of the lower right corner>20
Please enter the y coordinate of the lower right corner>20
The intersection of these two rectangles is the rectangle: java.awt.Rectangle[x=12,y=12,width=13,height=13]
The union of these two rectangles is the rectangle: java.awt.Rectangle[x=10,y=10,width=22,height=22]
Do you want to study two more rectangles>


at this line:

Quote

Do you want to study two more rectangles>


The console will not accept my input.

I don't see the problem.

Is This A Good Question/Topic? 0
  • +

Replies To: Console will not accept input / beginner level java problem

#2 creative code monkey  Icon User is offline

  • New D.I.C Head

Reputation: 14
  • View blog
  • Posts: 45
  • Joined: 14-September 11

Re: Console will not accept input / beginner level java problem

Posted 20 September 2011 - 10:42 AM

I don't see how your scanner is declared. Could you share that code? I've never worked with the Scanner class, but this is what I see just looking at examples:

import java.util.Scanner;
...
...
Scanner in = new Scanner(System.in);
...
...
String response = in.nextLine();
in.close();



You are also not closing the scanner, which maybe it's required?

Also what are you typing as input? I notice you have two identical if statements looking for anything starting with the letter 'y'. Are you trying to input a 'no'?

This post has been edited by creative code monkey: 20 September 2011 - 10:44 AM

Was This Post Helpful? 0
  • +
  • -

#3 Gantz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 21-May 11

Re: Console will not accept input / beginner level java problem

Posted 20 September 2011 - 10:45 AM

static Scanner scan = new Scanner(System.in);



I've created a 'global variable scanner'. It seems to work fine in my other methods. Do you think I should try declaring a new scanner inside the do-while loop? I'm going to try that now.

Wow, that worked!! Why couldn't the 'global' scanner operate within the loop??
Was This Post Helpful? 0
  • +
  • -

#4 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Console will not accept input / beginner level java problem

Posted 20 September 2011 - 10:47 AM

You might have been trying to use it in a non static method. Also it isn't public so it might not have had access to it. Wouldn't know without seeing rest of code

This post has been edited by Fuzzyness: 20 September 2011 - 10:49 AM

Was This Post Helpful? 0
  • +
  • -

#5 creative code monkey  Icon User is offline

  • New D.I.C Head

Reputation: 14
  • View blog
  • Posts: 45
  • Joined: 14-September 11

Re: Console will not accept input / beginner level java problem

Posted 20 September 2011 - 10:51 AM

Are you stepping into the code to see what the 'response' string gets set to?

View PostGantz, on 20 September 2011 - 10:45 AM, said:

Wow, that worked!! Why couldn't the 'global' scanner operate within the loop??


Good to know! Without actually stepping through your code, it'd be hard to tell why it wasn't working. As a general rule of thumb, though, limit your global variables, as they will just cause problems, like the one you just saw.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Console will not accept input / beginner level java problem

Posted 20 September 2011 - 08:31 PM

When you read basic data type using a Scanner the scanner will not "eat" the <endOfLine>
This was done on purpose so if you:

System.out.print("Enter 4 numbers: ");
int x1 = scan.nextInt();
int x2 = scan.nxtInt();
int x3 = scan.nxtInt();
int x4 = scan.nxtInt();

The user can then enter:
1 2 3 4 // all on the same line
or
1 2
3
4

So the scanner does: I have a int to read see if I can read from the current line
if not, due to end of line, read the <endOfLine> and check on the following line.

So nextInt() does not read the <endOfLine> if it does not need to. Understood ?

So doing:
int x = scan.nextInt();           // reads an int but not the <eol>
String answer = scan.nextLine();  // just reads the <eol> that follows your last inputed int



So, when switching from nextInt() to nextLine() you have to
int x = scan.nextInt();           // reads an int but not the <eol>
scan.nextLine();                  // reads the <eol> that follows your last inputed int
String answer = scan.nextLine();  // reads next line



Hope this helps
Was This Post Helpful? 0
  • +
  • -

#7 iamnotgeek  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 27
  • Joined: 17-July 11

Re: Console will not accept input / beginner level java problem

Posted 21 September 2011 - 12:22 AM

actually scanner is not meant for taking input
since u r a beginner i wud rather advice u to use datainputstream
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Console will not accept input / beginner level java problem

Posted 21 September 2011 - 03:58 AM

View Postiamnotgeek, on 21 September 2011 - 03:22 AM, said:

actually scanner is not meant for taking input
since u r a beginner i wud rather advice u to use datainputstream

Beginning of the defintion of the Scanner class n the API

Quote

A simple text scanner which can parse primitive types and strings using regular expressions.

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.

For example, this code allows a user to read a number from System.in:
     Scanner sc = new Scanner(System.in);
     int i = sc.nextInt();
 

I don't know since when you program in Java, but when JRE 1.5 introduced the class Scanner, I was one of the few thousand who applaud to finaly a class who will permit us to perform terminal input without the use of try/catch clauses.
Was This Post Helpful? 0
  • +
  • -

#9 cmpshr  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 120
  • Joined: 22-August 11

Re: Console will not accept input / beginner level java problem

Posted 21 September 2011 - 12:09 PM

In case the user enters:
1 2 3 4 and this is a string

you should

int x1 = scan.nextInt();           
int x2 = scan.nextInt();           
int x3 = scan.nextInt();           
int x4 = scan.nextInt();           
String answer = scan.nextLine();
if(answer.length() == 0)
   answer = scan.nextLine();


that will work if the user enters the 5 elements on the same line or on different ones :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1