These two cases have an array of integers the first one from the user the second
ID: 3553636 • Letter: T
Question
These two cases have an array of integers the first one from the user the second from the text file
Currently they don't work with negative values for instance -5,3,5,2=10 which is incorrect but
5,3,5,2=15, so modify these two so that the negative inputs can occur
case 1:
System.out.print("Enter list of comma-delimeted integers: ");
//Read a line of user decimal input
//BufferedReader buffer=new BufferedReader(new InputStreamReader(System.in));
Scanner scan = new Scanner(System.in);
String input2=scan.next();
// String input2=buffer.readLine();
//handles the commas in the input
String[] num = input2.split(",");
//puts the numbers into an array of doubles
int[] a= new int[num.length];
//for loop to find the length of the array given
for (int i = 0; i < num.length; i++) {
a[i] = Integer.parseInt(num[i]);
}
maxsum(a);
case 2:
BufferedReader reader = null;
Scanner sc = null;
String line = "";
try {
reader = new BufferedReader(new FileReader("integers.txt"));
sc =new Scanner(new BufferedReader(new FileReader("integers.txt")));
while (sc.hasNext()) {
line = sc.next();
}
String s[] = line.split(",");
a = new int[s.length];
for (int i = 0; i < s.length; i++) {
a[i] = Integer.parseInt(s[i]);
}
}
finally {
if (reader != null) {
reader.close();
}
}
System.out.println(line);
maxsum(a);
Explanation / Answer
Use
Scanner scan = new Scanner(System.in);
String input2=scan.nextLine(); //NextLine should work. All the best!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.