Why will my java program not accept alphabetical input from the user? -
edit: problem has been partially fixed. able enter text, nothing reads after enter 'shiphalf' value. did construct if else statements incorrectly?
i attempting let people input coupon code unable figure out how let console know user inputting text. feel error somewhere in here:
system.out.println("enter coupon code: "); string ship = input.nextline(); but not entirely sure. here whole source code below:
package pseudopackage; import java.util.scanner; public class pseudocode { public static void main(string[] args) { scanner input = new scanner(system.in); int ship1 = 0; int ship2 = 6; int ship3 = 2; string shiphalf = null; system.out.print("how shipping cost you?"); system.out.println(); system.out.print("enter textbook cost in dollars without $: "); double cost = input.nextdouble(); if ( cost >= 100 ) { system.out.println("your shipping costs $0"); } else if ( cost < 100 && cost > 50 ) { system.out.println("your shipping cost $6"); } else if ( cost < 50 ) { system.out.println("your shipping cost $2"); } system.out.println("enter coupon code: "); string ship = input.nextline(); if ( ship == shiphalf && cost >= 100 ) { system.out.println("your shipping costs $0"); } else if ( ship == shiphalf && cost < 100 && cost >50 ) { system.out.println("your shipping costs $3 "); } else if ( ship == shiphalf && cost < 50 ) { system.out.println("your shipping costs $1"); } } }
you have 2 problems in code:
1. use input.next() instead of input.nextline().
2. change if conditional ship.equals("shiphalf"). remember use .equals() compare 2 strings.
here final code:
package pseudopackage; import java.util.scanner; public class pseudocode { public static void main(string[] args) { scanner input = new scanner(system.in); int ship1 = 0; int ship2 = 6; int ship3 = 2; string shiphalf = null; system.out.print("how shipping cost you?"); system.out.print("\nenter textbook cost in dollars without $: "); double cost = input.nextdouble(); if ( cost >= 100 ) { system.out.println("your shipping costs $0"); } else if ( cost < 100 && cost > 50 ) { system.out.println("your shipping cost $6"); } else if ( cost < 50 ) { system.out.println("your shipping cost $2"); } system.out.println("enter coupon code: "); string ship = input.next(); if ( ship.equals("shiphalf") && cost >= 100 ) { system.out.println("your shipping costs $0"); } else if ( ship.equals("shiphalf") && cost < 100 && cost >50 ) { system.out.println("your shipping costs $3 "); } else if ( ship.equals("shiphalf") && cost < 50 ) { system.out.println("your shipping costs $1"); } } }
Comments
Post a Comment