Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,194,222 members, 7,953,832 topics. Date: Friday, 20 September 2024 at 07:27 AM

Java: How To Go To A Line If A Wrong Value Is Inputted - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Java: How To Go To A Line If A Wrong Value Is Inputted (1331 Views)

Java: How Can Write A File on Internet To A SQL Blob Field Directly? / Who Has Dietel's Java How To Program 10th Edition Online Chapters? / Who Has "Java How To Program 10th Edition " By Paul Dietel? (2) (3) (4)

(1) (Reply) (Go Down)

Java: How To Go To A Line If A Wrong Value Is Inputted by abumeinben(m): 2:25pm On Jul 21, 2016
Hi hackers!

Ive been having this little issue on this code. I'm new to programming.
If a wrong value if inputted, I want the program to run until the right value is inputted.

Kindly assist.

Java code:




package class3;

import java.util.Scanner;

public class dayName {

public static void main(String args[]){

Scanner input = new Scanner(System.in);
System.out.println("\nEnter any of the following values from 0 to 7 and \nI'll tell you the corresponding day.\n"wink;
int day = input.nextInt();

switch (day){

case 0: day = 0;
if (day == 0)
System.out.println("\nThat would be Sunday!\n"wink;
break;
case 1: day = 1;
if (day == 1)
System.out.println("\nThat would be Monday!\n"wink;
break;
case 2: day = 2;
if (day == 2)
System.out.println("\nThat would be Tuesday!\n"wink;
break;
case 3: day = 3;
if (day == 3)
System.out.println("\nThat would be Wednesday!\n"wink;
break;
case 4: day = 4;
if (day == 4)
System.out.println("\nThat would be Thursday!\n"wink;
break;
case 5: day = 5;
if (day == 5)
System.out.println("\nThat would be Friday!\n"wink;
break;
case 6: day = 6;
if (day == 6)
System.out.println("\nThat would be Saturday!\n"wink;
break;
default: if(day < 0 && day > 6);
System.out.println("\nThere's no day as such!\n"wink;
System.exit(0);

}
}

}

Thanks

cc:

Emmach10
Dreadlord
larisoft
lekropasky
DonSegmond
codemarshal08
omoelu1
Re: Java: How To Go To A Line If A Wrong Value Is Inputted by Emmach10(m): 3:17pm On Jul 21, 2016
Code doesnt look good here. So send me the code to my mail. Meanwhile, you can use a do-while or while loop to run the code till the user gets the right input!
Re: Java: How To Go To A Line If A Wrong Value Is Inputted by Emmach10(m): 3:34pm On Jul 21, 2016
Thats simple. First of all you have to handle the exception that will occur if the user did not enter an integer then you have to validate the integer as the user enters it to make sure if its within 0 and 6 before you pass on the integer to the switch case to determine the date. This could be done with an if statement inside a while statement or with a do-while statement.

Another alternative will be to put a code snipet inside the default case that returns the user the begining of the code thanks. I could have edited the code for you but am using a poor browser that doesnt support javascript so cant select code view.
Re: Java: How To Go To A Line If A Wrong Value Is Inputted by Emmach10(m): 4:03pm On Jul 21, 2016
abumeinben:
Hi hackers!

Ive been having this little issue on this code. I'm new to programming.
If a wrong value if inputted, I want the program to run until the right value is inputted.

Kindly assist.

Java code:



Thanks

cc:

Emmach10
Dreadlord
larisoft
lekropasky
DonSegmond
codemarshal08
omoelu1

Input this before the switch statement.


int repeat;
do{
if(day >=0 && <= 6){
repeat = 0;
}
else{
repeat = 1;
input.nextLine();
System.out.print("Wrong Input, Please try again"wink;
day = input.nextInt();
}
}while(repeat = 1)

1 Like

Re: Java: How To Go To A Line If A Wrong Value Is Inputted by Emmach10(m): 4:03pm On Jul 21, 2016
abumeinben:
Hi hackers!

Ive been having this little issue on this code. I'm new to programming.
If a wrong value if inputted, I want the program to run until the right value is inputted.

Kindly assist.

Java code:



Thanks

cc:

Emmach10
Dreadlord
larisoft
lekropasky
DonSegmond
codemarshal08
omoelu1

Input this before the switch statement.



int repeat;
do{
if(day >=0 && <= 6){
repeat = 0;
}
else{
repeat = 1;
input.nextLine();
System.out.print("Wrong Input, Please try again"wink;
day = input.nextInt();
}
}while(repeat = 1)

Re: Java: How To Go To A Line If A Wrong Value Is Inputted by abumeinben(m): 6:42pm On Jul 21, 2016
Emmach10:


Input this before the switch statement.



int repeat;
do{
if(day >=0 && <= 6){
repeat = 0;
}
else{
repeat = 1;
input.nextLine();
System.out.print("Wrong Input, Please try again"wink;
day = input.nextInt();
}
}while(repeat = 1)


Will try as soon as I ..tnx
Re: Java: How To Go To A Line If A Wrong Value Is Inputted by Emmach10(m): 7:09pm On Jul 21, 2016
abumeinben:


Will try as soon as I ..tnx

You are welcomed.
Note: i didnt test the code so try it and modify to you own satisfaction!
Re: Java: How To Go To A Line If A Wrong Value Is Inputted by Nobody: 7:15pm On Jul 21, 2016
thank God my name was not mentioned, trolls away silently to the backbench watching the JAVA dudes at work. . .
Re: Java: How To Go To A Line If A Wrong Value Is Inputted by Emmach10(m): 9:28pm On Jul 21, 2016
dhtml18:
thank God my name was not mentioned, trolls away silently to the backbench watching the JAVA dudes at work. . .

Lolzz. What languages can you program in and what are your skills?
Re: Java: How To Go To A Line If A Wrong Value Is Inputted by Nobody: 10:28pm On Jul 21, 2016
Emmach10:


Lolzz. What languages can you program in and what are your skills?
E gba mi o / O'di egwu / osanobuame / santa maria!! See me see wahala. Okay, i ain't got a skillset other than TROLLING.
Re: Java: How To Go To A Line If A Wrong Value Is Inputted by Emmach10(m): 10:54am On Jul 22, 2016
dhtml18:

E gba mi o / O'di egwu / osanobuame / santa maria!! See me see wahala. Okay, i ain't got a skillset other than TROLLING.

Lolzz, i dont mean any insult ooh!
Re: Java: How To Go To A Line If A Wrong Value Is Inputted by Nobody: 12:07pm On Jul 22, 2016
me i just dey play play ni o
Re: Java: How To Go To A Line If A Wrong Value Is Inputted by lekropasky(m): 3:36pm On Jul 23, 2016
Am sorry for replying this late.
Ok...to the real business.

I can see some error here, you are already overriding the input entered by the user inside your switch statement.

int day = input.nextInt();
//loop indefinitelly
while(true){
//put your switch statement here.
Switch(day){
case 0:
System.out.print("The day is Sunday"wink;
break;
//test other cases here....
default:
System.out.print("you haven't entered a correct value. Please Try again"wink;
}
//then, outside your switch statement, put this,
day = input.nextInt();
//to break of this loop, simply put a condition here...e.g, if you want to break when the user enter 9.
if(day==9){
System.out.print("Thank You for using this program"wink;
break;
}
}
Re: Java: How To Go To A Line If A Wrong Value Is Inputted by Emmach10(m): 12:50am On Jul 25, 2016
lekropasky:
Am sorry for replying this late.
Ok...to the real business.

I can see some error here, you are already overriding the input entered by the user inside your switch statement.

int day = input.nextInt();
while(true){
//put your switch statement here.
Switch(day){
case 0:
System.out.print("The day is Sunday"wink;
break;
//test other cases here....
default:
System.out.print("you haven't entered a correct value. Please Try again"wink;
}
//then, outside your switch statement, put this,
day = input.nextInt();
}

this will definitely result to a logical error called infinite loop
Re: Java: How To Go To A Line If A Wrong Value Is Inputted by yorex2011: 1:48pm On Jul 25, 2016
This is one way of doing it

i'm just a beginner too so i'm just trying to be clever here grin



import java.util.Scanner;

public class Dialog {

public static void main(String args[]){
String[] days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

int sentinelCheck = 0;
int wrongEntryCount = 0;

while(sentinelCheck==0){
Scanner input = new Scanner(System.in);
if(wrongEntryCount ==0){
System.out.println("\nPlease enter any of the following values from 0 to 6\n" );
}
else if(wrongEntryCount > 0 && wrongEntryCount < 3){
System.out.println("\nMr User I expect a value from 0 to 6\n" );
}
else if(wrongEntryCount >= 3 && wrongEntryCount < 5 ){
System.out.println("\n Ogbeni pick either 0 ,1, 2 , 3 , 4, 5, or 6\n" );
}
else{
System.out.println("\nI'm not doing again go and die!!\n" );
break;
}
int day = input.nextInt();
if(day < 0 || day > 6){
sentinelCheck = 0;
wrongEntryCount++;
}else{
String dayOfWeek = days[day];
System.out.printf("\nThat would be %s!\n", dayOfWeek);
sentinelCheck = 1;
wrongEntryCount = 0;
}
}



}
}

Re: Java: How To Go To A Line If A Wrong Value Is Inputted by Emmach10(m): 9:26am On Jul 26, 2016
yorex2011:
This is one way of doing it

i'm just a beginner too so i'm just trying to be clever here grin



import java.util.Scanner;

public class Dialog {

public static void main(String args[]){
String[] days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

int sentinelCheck = 0;
int wrongEntryCount = 0;

while(sentinelCheck==0){
Scanner input = new Scanner(System.in);
if(wrongEntryCount ==0){
System.out.println("\nPlease enter any of the following values from 0 to 6\n" );
}
else if(wrongEntryCount > 0 && wrongEntryCount < 3){
System.out.println("\nMr User I expect a value from 0 to 6\n" );
}
else if(wrongEntryCount >= 3 && wrongEntryCount < 5 ){
System.out.println("\n Ogbeni pick either 0 ,1, 2 , 3 , 4, 5, or 6\n" );
}
else{
System.out.println("\nI'm not doing again go and die!!\n" );
break;
}
int day = input.nextInt();
if(day < 0 || day > 6){
sentinelCheck = 0;
wrongEntryCount++;
}else{
String dayOfWeek = days[day];
System.out.printf("\nThat would be %s!\n", dayOfWeek);
sentinelCheck = 1;
wrongEntryCount = 0;
}
}



}
}



You are definitely learning with Java How To Program by Paul Deitel and Associates Right?
Re: Java: How To Go To A Line If A Wrong Value Is Inputted by yorex2011: 9:39am On Jul 26, 2016
Emmach10:


You are definitely learning with Java How To Program by Paul Deitel and Associates Right?
Sentinel definitely gave me away lol
Re: Java: How To Go To A Line If A Wrong Value Is Inputted by lekropasky(m): 11:18am On Jul 26, 2016
Emmach10:


this will definitely result to a logical error called infinite loop
Nope, I have not finished it. the line // day = input.nextInt(); // will cause the loop to wait for user input before continuing to the next Iteration.
see the modified version to break out of the loop

(1) (Reply)

Complete Primary And Secondary Website Needed / Forloop Valley For Programmers / Pls Where Can I Get Html5 Video's In .iso Format

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 52
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.