Problems with interactive program

Problems with interactive program

Page 1 of 1

5 Replies - 1690 Views - Last Post: 03 June 2006 - 10:36 PM Rate Topic: -----

#1 lahuerita2004  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 19-February 06

Problems with interactive program

Posted 02 June 2006 - 08:45 PM

Hello,

I have just began with Java and I am having a concept problem with an interactive program that I have developed. I have written a program that prompts a user to enter an integer character and a string character. Ex. 47 Red

After I implement the appropriate scanner features I would code.
[/code]

System.out.println( " Please enter number and color");
int num = stdin.nextInt();
String color = stdin.next();
[code]


I am having problems with fixing this to prevent any errors. For example my program would crash if someone were to not enter a space between the two entries. Such as 45Red.
Is there a way to fix this?

I would appreciate any help.

This post has been edited by lahuerita2004: 02 June 2006 - 08:46 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Problems with interactive program

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Problems with interactive program

Posted 03 June 2006 - 12:25 AM

first your code [] segments are backwards :)

you have a few choices, you can either write cases to handle all the possibilities or create errors of your own type to handle the occurance of an error. writting your own errors (which i recommend) has a code snippet.
Was This Post Helpful? 0
  • +
  • -

#3 lahuerita2004  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 19-February 06

Re: Problems with interactive program

Posted 03 June 2006 - 02:33 PM

Thanks for the advise. I am just not sure how to start it.
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Problems with interactive program

Posted 03 June 2006 - 03:27 PM

You should begin be defining exactly what behaviour you widh to elicit. Do you want the user to have to enter one input at a time, followed by a press of the enter key? Do you want the user to be able to enter multiples pieces of information at the same time?
Was This Post Helpful? 0
  • +
  • -

#5 lahuerita2004  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 19-February 06

Re: Problems with interactive program

Posted 03 June 2006 - 03:39 PM

actually I am suppose to have the user enter both entries at one time with a space between. However, I am not sure how to put a check point if they ignore the white space.

I wanted to put an if loop to check with a .hasInt() for the first entry. However, that does not work.
Was This Post Helpful? 0
  • +
  • -

#6 lahuerita2004  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 19-February 06

Re: Problems with interactive program

Posted 03 June 2006 - 10:36 PM

I got a great suggestion of maybe using try/catch. I believe that will work.
Thanks for all the help.

;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1