Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8801349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:48:42+00:00 2026-06-14T00:48:42+00:00

I am a beginner at programming Java looking for some advice. I have created

  • 0

I am a beginner at programming Java looking for some advice.
I have created a very simple program/application asking users to enter commands which in turn display basic shapes on a graphics screen at the user specified location, size and color.

I have used the scanner class to get users input from the keyboard (e.g. user types move 100 150 and the graphics screen pen moves to X = 100, Y = 150 or types circle 100 to display a circle radius 100 at the specified x,y co-ordinate)

I want to return an error message if the user inputs an incorrect command or tried to enter nothing or random keystrokes (e.g. if they misspelled a command or did not specify enough values for the command)

At the moment the program crashes and must be restarted

import java.util.Scanner;


public class Assign1 {


public final static void main(String [] args) {

    System.out.println("Let's draw something on the screen!");

    GraphicsScreen graphics = new GraphicsScreen();

Scanner input = new Scanner(System.in); // used to read the keyboard

    String next;    // stores the next line input
    String[] one;

    do {
        System.out.print("Enter a command (\"stop\") to finish : ");
        System.out.print("Type 'help' for a list of commands ");
        next = input.nextLine();
        one = next.split(" ");

        String command = one[0];

        if(next.contains("help")) {
            System.out.println("Type 'move' followed by an X and Y co-ordinate to move the graphical pointer.");
            System.out.println("Type 'circle' followed by a radius value to output a circle.");
            System.out.println("Type 'line' followed by an X and Y co-ordinate to draw a line.");
            System.out.println("Type 'clear' to reset the graphical canvas.");
        }


        if(next.contains("move")) {

            int x = 0;

            int y = 0;

            x = Integer.parseInt(one[1]);

            y= Integer.parseInt(one[2]);

            graphics.moveTo(x, y);

        } 



        if( command.equalsIgnoreCase("circle")) {


            int radius = 0;


            radius = Integer.parseInt(one[1]);


            graphics.circle(radius);

        } 


        if(command.equalsIgnoreCase("line")) {

            int x = 0;
            int y = 0;

            x = Integer.parseInt(one[1]);

            y= Integer.parseInt(one[2]);

            graphics.lineTo(x,y);

        }


        if(next.contains("clear")) {
            graphics.clear();
        }

    } while ( next.equalsIgnoreCase("stop") == false );

    System.out.println("You have decided to stop entering commands. Program terminated!");

    graphics.close();

I have all the necessary code for the graphics screen in another document called

graphicscreen.java

I am just looking for suggestion of how I can validate the users text input to give an error message if anything but one of my specific commands are typed.

I have tried using if statements and while loops and others I have found on various web pages but none have yet worked.

Any suggestions much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:48:43+00:00Added an answer on June 14, 2026 at 12:48 am

    Use else if and else. An else if condition only gets checked if all the if and else if conditions above it evaluated to false. An else block only gets run if all the if and else if conditions above it evaluated to false.

    if(next.contains("move")) {
    } 
    else if( command.equalsIgnoreCase("circle")) {
    } 
    else if(command.equalsIgnoreCase("line")) {
    }
    else if(next.contains("clear")) {
    }
    else {
     //input invalid
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a beginner in programming Java Desktop Application interacting with databases. My goal
I'm very beginner of Websphere MQ classes for Java programming. I've decided to follow
I am beginning Java programming, and I have written a program which tests how
I have some simple java code to call a method with a value to
I'm having trouble writing a simple Python program for a beginner programming class. If
I'm a beginner in Java programming language, and I have a problem that I
OK guys, up till now (and since I'm a beginner) I was programming Java
I am a beginner when it comes to programming with Android/Java, and I am
I am a beginner to android. in java and other programming languages we get
I'm a beginner in programming. I've just made a program called Guessing Game. And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.