Can't get this Java program to work


Can't get this Java program to work



Okay changed the code a bit, but the program still doesn't work properly. I want to be able to enter type of product (fruit) in the java console when program is run, enter any type of fruit (bananas, apples or oranges) and then enter a qty.


import java.util.*;

public class demo {

public static void main(String args) {

Scanner sc = new Scanner(System.in);

String str = { "Bananas", "Apples", "Oranges" };
double price = { 2.09, 2.59, 2.25 };
int i = 0;
int j = 0;

System.out.print("Enter type of product: ");
String string = sc.nextLine();
if ("fruit".equals(string)) {
while (i < str.length) {
while (j < price.length) {
System.out.print(str[i++] + ": " + "£" + (price[j++]) + "p per bag n");

}
}
}
System.out.print("n");
System.out.print("Enter which type of " + string + ": ");

String string1 = sc.nextLine();

boolean strs = "bananas".equals(string1);
boolean strs1 = "apples".equals(string1);
boolean strs2 = "oranges".equals(string1);
if (strs) {
System.out.print("Enter qty of " + str[0] + " (by bag): ");
}

if (strs1) {
System.out.print("Enter qty of " + str[1] + " (by bag): ");

}
if (strs2) {
System.out.print("Enter qty of " + str[2] + " (by bag): ");
}

int qty = sc.nextInt();
int a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int h = 1;
if ((a[h] == (qty)) && (strs) || (strs1) || (strs2)){
System.out.print("n");
System.out.print(qty + " bag(s) of " + string1 + " have been added to your basket, " + "total costing £"
+ (qty) * price[0] + "p");

}
}
}



Any more ideas?





What does the error exactly say?
– wdc
Jul 1 at 7:40





I put a catch Exception e, which allowed me to out print my own error message (if that makes sense)
– hm087ster
Jul 1 at 7:44





String string = sc.next("fruit"); is in error fix this
– Jesse
Jul 1 at 7:47





You want to print e, not ignore it just to print error. You can either put e in the print message or use e.printstacktrace(). This will give you more information as to what’s going on.
– CeePlusPlus
Jul 1 at 7:51




4 Answers
4



You have mistake is Reading strings.



Replace


String string = sc.next("fruit"); // Line 17
String string1 = sc.next("bananas"); // Line 26
String string2 = sc.next("apples"); // Line 30
String string3 = sc.next("oranges"); // Line 35



With


String string = sc.nextLine(); // Line 17
String string1 = sc.nextLine(); // Line 26
String string2 = sc.nextLine(); // Line 30
String string3 = sc.nextLine(); // Line 35



If you want to check the String entered , then make use of following code:-


String


if(string.equals("fruit")){
// statements
}





Thank you. Had a sleepless night over that one...
– hm087ster
Jul 1 at 7:55



Error is at String string = sc.next("fruit"); either change it to sc.nextLine() or Something like below


public static void main(String args) {

Scanner sc = new Scanner(System.in);

String str = { "Bananas", "Apples", "Oranges" };
double stk = { 1.09, 1.59, 1.25 };
int i = 0;
int j =0;

try {
System.out.print("Enter type of product: ");
String string = sc.next();

while (i < str.length) {
while (j < stk.length) {
System.out.print(str[i++] + ": " + "£" + (stk[j++]) + "p per bag n");

}
}
System.out.print("n");
System.out.print("Enter which type of "+string+": ");
String string1 = sc.next();
if(string1 != null) {
System.out.print("Enter qty of "+string1+ "(per bag) n");
}
String string2 = sc.next();
if(string2 != null) {
System.out.print("Enter qty of " +string2+ "(in lbs) n");

}
String string3 = sc.next();
if (string3 != null) {
System.out.print("Enter qty of " +string3+ "(in lbs) n");

}

} catch (Exception e) {
System.out.println("Eror");
e.printStackTrace();
}
}



Your scanning of input line is wrong.To scan input data in Java you should use


Scanner in = new Scanner(System.in);
int a = in.nextInt(); // To input integer
char ch = in.nextChar(); // To input character
String str = in.nextLine(); // To get a complete line of input
String s = in.next(); //To get a single string



Replace your inputing line according to the above given.



Cheers :)



You are getting a InputMismatchException that, according to the JavaDoc, is


InputMismatchException



thrown by a Scanner to indicate that the token retrieved does not
match the pattern for the expected type, or that the token is out of
range for the expected type.


Scanner



So if you don't write "fruit" as you requested (String string = sc.next("fruit")), the execution terminates with that exception. So, to fix your code, you should replace


"fruit"


String string = sc.next("fruit")


String string = sc.next("fruit");



with something like


String string = sc.nextLine();
if ("fruit".equalsIgnoreCase(string)) { /* design your control flow as you want */



You can apply the same rule to the other sc.next() present in your code.


sc.next()



A side note: never swallow exceptions into a catch block; always log them.


catch





doesn't work :(
– hm087ster
Jul 1 at 8:05





Obviusly you have to adjust the logic of the code according with your needs; I added a if only to demonstrate that you can design the flow you want.
– Robert Hume
Jul 1 at 8:08


if






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API