8 Replies - 186 Views - Last Post: 09 March 2013 - 05:50 AM Rate Topic: -----

#1 NSammut  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-March 13

Making An Error Message?

Posted 08 March 2013 - 07:42 PM

private static void getNumberOfRunes() throws IOException{ //gets number of runes you have
		System.out.println("Please type the number runes you would like to make into a runeword:");
		Scanner scan = new Scanner(System.in);
		numberOfRunes = scan.nextInt();	
		if(numberOfRunes <= 0){
			System.out.println("You cannot make a runeword with less than 1 rune...");
			System.exit(0);
		}
		else if(numberOfRunes >= 7){
			System.out.println("You cannot make a runeword with more than 6 runes...");
			System.exit(0);
			}
	}


This is my code, I have three questions.
1. How do I make an if statement/something that gives the user an error message after typing an int or string for numberOfRunes?
2. Is there a way to clear the screen during the program? Kind of like cls or clear in command prompt?
3. And how could I make one of the if statements loop back to one of the methods, or a certain part of a method.

Thanks, -Nick

Is This A Good Question/Topic? 0
  • +

Replies To: Making An Error Message?

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,422
  • Joined: 29-July 11

Re: Making An Error Message?

Posted 08 March 2013 - 07:48 PM

Use a while loop or do while until a certain answer is received to loop thee question.

There is no clear screen. The best you can do ise use a loop to println() a bunch of times.
Was This Post Helpful? 1
  • +
  • -

#3 NSammut  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-March 13

Re: Making An Error Message?

Posted 08 March 2013 - 07:58 PM

View Postfarrell2k, on 08 March 2013 - 07:48 PM, said:

Use a while loop or do while until a certain answer is received to loop thee question.

There is no clear screen. The best you can do ise use a loop to println() a bunch of times.

With such little description I managed to make what you said, and still got this...
Posted Image
When typing a character or string, I want the program to say,
"Must enter an integer value of 1-6."
Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,422
  • Joined: 29-July 11

Re: Making An Error Message?

Posted 08 March 2013 - 08:04 PM

pseudo code:

do {
propmt for input

{
while( answer is not >= 1 and not <= 6 )
Was This Post Helpful? 0
  • +
  • -

#5 Flukeshot  Icon User is online

  • A little too OCD
  • member icon

Reputation: 401
  • View blog
  • Posts: 999
  • Joined: 14-November 12

Re: Making An Error Message?

Posted 08 March 2013 - 08:04 PM

When using nextInt, the input MUST be an int or you'll receive that exception. To achieve what you want to, you could catch that exception:

while(noValidInput) {
   try {
      numberOfRunes = scan.nextInt();
      if (numberOfRunes > 6 || numberOfRunes < 1) {
        System.out.println("You may only use between 1 and 6 runes");
      } else {
        noValidInput = false;
      }
   } catch(InputMismatchException e) {
      System.out.println("Must enter an integer value of 1-6.");
   }
}



Disclaimer: I'm not on my main computer right now so I can't test this for accuracy :)

This post has been edited by Flukeshot: 08 March 2013 - 08:05 PM

Was This Post Helpful? 1
  • +
  • -

#6 NSammut  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-March 13

Re: Making An Error Message?

Posted 08 March 2013 - 08:11 PM

View Postfarrell2k, on 08 March 2013 - 08:04 PM, said:

pseudo code:

do {
propmt for input

{
while( answer is not >= 1 and not <= 6 )

Is this possible? like !>= ? ..or is there something like it?
Was This Post Helpful? 0
  • +
  • -

#7 Flukeshot  Icon User is online

  • A little too OCD
  • member icon

Reputation: 401
  • View blog
  • Posts: 999
  • Joined: 14-November 12

Re: Making An Error Message?

Posted 08 March 2013 - 08:21 PM

To ask the computer if something is "not greater than or equal to one", just ask it if it "is less than 1"

I'll get an Oscar for that logic..

This post has been edited by Flukeshot: 08 March 2013 - 08:22 PM

Was This Post Helpful? 0
  • +
  • -

#8 NSammut  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-March 13

Re: Making An Error Message?

Posted 08 March 2013 - 08:27 PM

View PostFlukeshot, on 08 March 2013 - 08:21 PM, said:

To ask the computer if something is "not greater than or equal to one", just ask it if it "is less than 1"

I'll get an Oscar for that logic..

Not completely...if there is an and or or in there like

greater than 5 and less than 10
only 6 7 8 9 can be possible
not greater than 5 or not less than 10
6 7 8 9 is possible but since chars and strings can still be "typed in" the not would make sense
Was This Post Helpful? 0
  • +
  • -

#9 Flukeshot  Icon User is online

  • A little too OCD
  • member icon

Reputation: 401
  • View blog
  • Posts: 999
  • Joined: 14-November 12

Re: Making An Error Message?

Posted 09 March 2013 - 05:50 AM

So like I said before, catch the input mismatch exception.

An exception doesn't necessarily mean your program has crashed, it only means that the program has been given something it can't handle.

So, in your catch clause, you can handle the bad input by ignoring it, telling the user why it was ignored and then asking them for input again.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1