I have a code that outputs a subsequence with the maximum sum, but there is a condition "if there are 2 or more sequences equal in sum, then output the longest of them" (everything needs to be done using a list). I do not understand how to display the one that is longer.In this code the output should be 100 90 8. (the code remembers the very first of these sequences)
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(-5);
list.add(6);
list.add(-3);
list.add(-13434);
list.add(99);
list.add(99);
list.add(-444);
list.add(-7444);
list.add(100);
list.add(90);
list.add(8);
if (list == null || list.size() == 0) {//проверка на пустоту листа
System.out.println("empty array");
return;
}
int maxSumStartIndex = 0;
int maxSumLastIndex = 0;
int maxSum = list.get(0);
int lastSumStartIndex = 0;
int lastSum = list.get(0);
for (int i = 1; i < list.size(); i++) {
lastSum += list.get(i);
if (lastSum < list.get(i)) {
lastSum = list.get(i);
lastSumStartIndex = i;
}
if (maxSum < lastSum) {
maxSumStartIndex = lastSumStartIndex;
maxSumLastIndex = i;
maxSum = lastSum;
}
}
System.out.println("sum( arr[" + maxSumStartIndex + "] .. arr[" + maxSumLastIndex + "] ) = " + maxSum);
for (int i = maxSumStartIndex; i <= maxSumLastIndex; i++) {
System.out.print(list.get(i) + " ");
}
}
}
This works but don't know if its the best approach
int maxSumStartIndex = 0;
int maxSumLastIndex = 0;
int maxSum = list.get(0);
int maxSumLength = 0;
int lastSumStartIndex = 0;
int lastSum = list.get(0);
for (int i = 1; i < list.size(); i++) {
lastSum += list.get(i);
if (lastSum < list.get(i)) {
lastSum = list.get(i);
lastSumStartIndex = i;
}
if (maxSum < lastSum) {
maxSumStartIndex = lastSumStartIndex;
maxSumLastIndex = i;
maxSumLength = maxSumLastIndex - maxSumStartIndex + 1;
maxSum = lastSum;
}
if (maxSum == lastSum) {
if (!(i - lastSumStartIndex + 1 > maxSumLength)) continue;
maxSumStartIndex = lastSumStartIndex;
maxSumLastIndex = i;
maxSumLength = maxSumLastIndex - maxSumStartIndex + 1;
}
}
Here is a code operating a simple clock (second chance) Page Replacement, my problem is that my math equation (double pager = fault/ref_len*100;) always outputs to 0 when i call it in my output line.
i wish to make my variable output the correct product but it always == to 0 when i output it on my terminal.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ClockReplacement {
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int frames, pointer = 0, hit = 0, fault = 0,ref_len;
int buffer[][];
int reference[];
int mem_layout[][];
int used_layout[][];
System.out.println("Please enter the number of Frames: ");
frames = Integer.parseInt(br.readLine());
System.out.println("Please enter the length of the Reference string: ");
ref_len = Integer.parseInt(br.readLine());
reference = new int[ref_len];
mem_layout = new int[ref_len][frames];
used_layout = new int[ref_len][frames];
buffer = new int[frames][2];
for(int j = 0; j < frames; j++)
{
buffer[j][0] = -1;
buffer[j][1] = 0;
}
System.out.println("Please enter the reference string: ");
for(int i = 0; i < ref_len; i++)
{
reference[i] = Integer.parseInt(br.readLine());
}
System.out.println();
for(int i = 0; i < ref_len; i++)
{
int search = -1;
for(int j = 0; j < frames; j++)
{
if(buffer[j][0] == reference[i])
{
search = j;
hit++;
buffer[j][1] = 1;
break;
}
}
if(search == -1)
{
while(buffer[pointer][1] == 1)
{
buffer[pointer][1] = 0;
pointer++;
if(pointer == frames)
pointer = 0;
}
buffer[pointer][0] = reference[i];
buffer[pointer][1] = 1;
fault++;
pointer++;
if(pointer == frames)
pointer = 0;
}
for(int j = 0; j < frames; j++)
{
mem_layout[i][j] = buffer[j][0];
used_layout[i][j] = buffer[j][1];
}
}
for(int i = 0; i < frames; i++)
{
for(int j = 0; j < ref_len; j++)
System.out.printf("%3d %d ",mem_layout[j][i],used_layout[j][i]);
System.out.println();
}
System.out.println("#Page Faults: " + fault);
double pager = fault/ref_len*100;
System.out.println("%page replacement: " + pager);
}
}
I'm trying to write a code that multiplies two strings of integers. I'm not too sure where it's going wrong... It works for some numbers, but is horribly wrong for others. I'm not asking for a full solution, but just a hint (I seriously appreciate any help possible) as to where I'm making the obviously silly mistake. Thanks in advance.
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Please enter a big integer. ");
String t = scan.nextLine();
System.out.print("And another. ");
String s = scan.nextLine();
BigInt a = new BigInt(t);
BigInt b = new BigInt(s);
System.out.println(a + " + " + b + " = " + a.add(b));
System.out.println(a + " - " + b + " = " + a.sub(b));
System.out.println(a + " * " + b + " = " + a.mul(b));
System.out.println(a + " / " + b + " = " + a.div(b));
}
}
class BigInt {
public BigInt() {
n = new int[1];
}
public BigInt(String s) {
n = new int[s.length()];
for (int i = 0; i < n.length; ++i) {
n[n.length - i - 1] = s.charAt(i) - '0' ;
}
}
private BigInt(int[] n) {
this.n = new int[n.length];
for (int i = 0; i < n.length; ++i) {
this.n[i] = n[i];
}
}
public String toString() {
String s = "";
for (int i : n) {
s = i + s;
}
return s;
}
public BigInt mul(BigInt o) {
int carry = 0;
int s = 0;
int digit;
int subtotal = 0;
int total = 0;
int max = n.length > o.n.length ? n.length : o.n.length;
int[] result = new int[n.length + o.n.length];
for (int i = 0; i < o.n.length; ++i) {
int bottom = i <= o.n.length ? o.n[i] : 0;
for (s = 0; s <= n.length; ++s){
int top = s < n.length ? n[s] : 0;
int prod = (top * bottom + carry);
if (s == (max-1)) {
total = Integer.valueOf((String.valueOf(prod) + String.valueOf(subtotal)));
carry = 0;
digit = 0;
subtotal = 0;
break;
}
if (prod < 10) {
digit = prod;
subtotal += digit;
carry = 0;
}
if (prod >= 10); {
digit = prod % 10;
carry = prod / 10;
subtotal += digit;
}
}
result[i] = total;
}
return new BigInt(trim(result));
}
private int[] trim(int[] nums) {
int size = nums.length;
for (int i = nums.length - 1; i > 0; --i) {
if (nums[i] != 0) {
break;
}
--size;
}
int[] res = new int[size];
for (int i = 0; i < size; ++i) {
res[i] = nums[i];
}
return res;
}
private int[] n;
}
A quick test using:
for (int x = 0; x < 10; x++) {
for (int y = 0; y < 10; y++) {
System.out.println(x + " * " + y + " = " + new BigInt(Integer.toString(x)).mul(new BigInt(Integer.toString(y))));
}
}
demonstrates that somehow your multiply of x * y is actually multiplying by 10x * y. That should give you a clear hint to the problem.
public static void main(String[] args) {
Scanner xis = new Scanner(System.in);
int H1 = 0;
int M1 = 0;
int H2 = 0;
int M2 = 0;
int[] numeros = new int[4];
System.out.println("Type the numbers.");
for(int i = 0; i <= numeros.length; i++)
{
numeros[i] = xis.nextInt();
H1 = H1 + numeros[0];
M1 = M1 + numeros[1];
H2 = H2 + numeros[2];
M2 = M2 + numeros[3];
}
System.out.println(H1);
System.out.println(H2);
int horaDuracao = (H2 - H1) * -1;
int minutoDuracao = (M2 - M1) * -1;
if(horaDuracao <= 0)
{
horaDuracao = horaDuracao + 24;
}
if (minutoDuracao <= 0)
{
minutoDuracao = minutoDuracao + 59;
horaDuracao = horaDuracao + -1;
}
}
When the user types the answer to this:
System.out.println("Type the numbers.");
It won't continue to the next part of my code.
Sorry if it's a repost, but I kept searching for some time for something like this, but couldn't find.
Your for loop goes for 5 iterations, not 4. You should remove the = from the terminating clause in order to get it working.
for(int i = 0; i < numeros.length; i++)
Edit: Yes, I know that ScanObj is not a proper name. And here is all of my code. It is not complete so there are some errors and some glitches, but you asked for it:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Scanner;
import javax.swing.JFrame;
public class MainClass {
static String[][] medium = new String[5][4];
static String names[] = { "Gatz ", "Cat ", "Women", "Mice ", "Robin",
"Romeo ", "Summer ", "Tempest", "Hamlet ", "Lear ",
"Titanic ", "StarWars", "DieHard ", "Elf ", "RED " };
static String lexile[] = { "L:1070", "L:123 ", "L:9254", "L:1234",
"L:534 ", "L:349", "L:632 ", "L:1097", "L:6453", "L:812 ",
"L:NA ", "L:NA ", "L:NA ", "L:NA ", "L:NA ", "L:NA " };
static String grade[] = { "G:9", "G:10", "G:12", "G:11", "G:9", "G:12",
"G:11", "G:9", "G:9", "G:12", "G:12", "G:11", "G:11", "G9", "G:9" };
static int max = 4;
char sorttype = (Character) null;
public static void main(String args[]) throws InterruptedException {
System.
out.println("Would you like to browse:\nB)Book C)CD or D)DVD");
Scanner ScanObj =
new Scanner(System.in);
String[] input =
new String[10];
input[1] = ScanObj.nextLine();
int store[] = { 6, 6, 6, 6, 6, 6 };
if (input[1].equals("B") || input[1].equals("Book")
|| input[1].equals(
"book") || input[1].equals("b")) {
String temp[][] =
new String[5][4];
int random[] = new int[5];
boolean repeat = true;
int nums[] = new int[5];
for (int i = 0; i < 5; i++) {
temp[i][0] =
"book";
temp[i][1] =
names[i];
temp[i][2] =
lexile[i];
temp[i][3] =
grade[i];
for (int qwop = 0; qwop < 4; qwop++) {
medium[i][qwop] = temp[i][qwop];
if (qwop == 0)
System.
out.printf(i + 1 + " | ");
else if (qwop < 3)
System.
out.printf(medium[i][qwop] + " | ");
else
System.
out.println(medium[i][qwop] + "\n");
Thread.sleep(150);
}
// System.out.println(temp[i][i]);
// random = store[i];
if (i == 4)
System.
out.printf("\n\n\n");
}
}
if (input[1].equals("C") || input[1].equals("c")
|| input[1].equalsIgnoreCase(
"CD") || input[1].equals("cd")) {
String temp[][] =
new String[5][4];
for (int i = 0; i < 5; i++) {
int random1 = (int) (Math.random() * max) + 6;
for (int p = 0; p < 4; p++) {
for (random1 = (int) (Math.random() * max) + 6; random1 == store[p];) {
random1 = (
int) (Math.random() * max) + 6;
}
}
temp[i][0] =
"CD";
temp[i][1] =
names[i + 5];
temp[i][2] =
lexile[i + 5];
temp[i][3] =
grade[i + 5];
for (int qwop = 0; qwop < 4; qwop++) {
medium[i][qwop] = temp[i][qwop];
if (qwop < 3)
System.
out.printf(medium[i][qwop] + " | ");
else
System.
out.println(medium[i][qwop] + "\n");
Thread.sleep(150);
}
// System.out.println(temp[i][i]);
random1 = store[i];
if (i == 4)
System.
out.printf("\n\n\n");
}
}
if (input[1].equalsIgnoreCase("D") || input[1].equalsIgnoreCase("DVD")) {
String temp[][] =
new String[5][4];
for (int i = 0; i < 5; i++) {
int random1 = (int) (Math.random() * max) + 6;
for (int p = 0; p < 4; p++) {
for (random1 = (int) (Math.random() * max) + 6; random1 == store[p];) {
random1 = (
int) (Math.random() * max) + 6;
}
}
temp[i][0] =
"DVD";
temp[i][1] =
names[i + 10];
temp[i][2] =
lexile[i + 10];
temp[i][3] =
grade[i + 10];
for (int qwop = 0; qwop < 4; qwop++) {
medium[i][qwop] = temp[i][qwop];
if (qwop < 3)
System.
out.printf(medium[i][qwop] + " | ");
else
System.
out.println(medium[i][qwop] + "\n");
Thread.sleep(150);
}
// System.out.println(temp[i][i]);
random1 = store[i];
if (i == 4)
System.
out.printf("\n\n\n");
}
}
// for loop goes through each of the parameters and compltes the
// instructions
System.
out
.println(
"How would you like to sort?\nA)Name B) Lexile C) Grade Level");
input[2] = ScanObj.nextLine();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
// if statement checks if the condition is true, then does the
// instructions if it is
// compareTo checks the ascii values of two strings. If the
// second
// is smaller (would come first in alphabetic order) it returns
// -1
boolean mismatch = true;
while (mismatch) {
if (input[2].equalsIgnoreCase("a")) {
mismatch =
false;
// makes a new temporary String
String temp1 =
null;
// Switches the String at position i with the one above
// it
for (int z = 0; z < medium.length - 1; z++) {
for (int x = z + 1; x < medium.length; x++) {
if (medium[z][1].compareTo(medium[x][1]) > 0) {
for (int qwop = 0; qwop < 4; qwop++) {
temp1 =
medium[z][qwop];
medium[z][qwop] = medium[x][qwop];
medium[x][qwop] = temp1;
}
mismatch =
true;
}
}
}
}
if (input[2].equalsIgnoreCase("b")) {
mismatch =
false;
// makes a new temporary String
String temp3 =
null;
// Switches the String at position i with the one above
// it
for (int z = 0; z < medium.length - 1; z++) {
for (int x = z + 1; x < medium.length; x++) {
if (medium[z][2].compareTo(medium[x][2]) > 0) {
for (int qwop = 0; qwop < 4; qwop++) {
temp3 =
medium[z][qwop];
medium[z][qwop] = medium[x][qwop];
medium[x][qwop] = temp3;
}
mismatch =
true;
}
}
}
}
if (input[2].equalsIgnoreCase("c")) {
mismatch =
false;
// makes a new temporary String
String temp3 =
null;
// Switches the String at position i with the one above
// it
for (int z = 0; z < medium.length - 1; z++) {
for (int x = z + 1; x < medium.length; x++) {
// Here I encounter an error because compareTo()
// sorts Lexicographically, meaning anything
// starting with a 1 comes before anything
// starting with a 2, 3, etc. This means that
// Grade 11 comes before grade 9, and simply
// comparing greater than does not work due to
// the fact that they are Strings.
if (medium[z][3].compareTo(medium[x][3]) > 0) {
for (int qwop = 0; qwop < 4; qwop++) {
temp3 =
medium[z][qwop];
medium[z][qwop] = medium[x][qwop];
medium[x][qwop] = temp3;
}
mismatch =
true;
}
}
}
}
}
}
}
Thread.sleep(1000);
for (int prt = 0; prt < medium.length; prt++) {
for (int qwop = 0; qwop < 4; qwop++) {
if (qwop < 3)
System.
out.printf(medium[prt][qwop] + " | ");
else
System.
out.println(medium[prt][qwop]);
}
}
Thread.sleep(1800);
for (int t = 0; t < 1000; t++) {
System.
out.println("\n\n\nTo edit a selection, type edit\n"
+
"To sort by a different category, type sort\n"
+
"To browse another medium, type browse\n"
+
"To search the " + medium[1][0] + "s, type search");
input[3] = ScanObj.nextLine();
if (input[3].equalsIgnoreCase("browse")) {
Thread.sleep(500);
System.out.println("What would you like to browse?:");
if (medium[1][0].equals("book")) {
System.out.println("CDs, or DVDs");
input[4] = ScanObj.nextLine();
}
if (medium[1][0].equals("CD")) {
System.out.println("Books, or DVDs");
input[4] = ScanObj.nextLine();
}
if (medium[1][0].equals("DVD")) {
System.out.println("Books, or CDs");
input[4] = ScanObj.nextLine();
}
if (input[4].equalsIgnoreCase("CD")
|| input[4].equalsIgnoreCase("DVD")
|| input[4].equalsIgnoreCase("Book")) {
for (int i = 0; i < 5; i++) {
String temp[][] = new String[5][4];
temp[i][0] =
"CD";
int addon = 0;
if (input[4].equals("CD"))
addon = 5;
if (input[4].equals("DVD"))
addon = 10;
temp[i][1] =
names[i + addon];
temp[i][2] =
lexile[i + addon];
temp[i][3] =
grade[i + addon];
for (int qwop = 0; qwop < 4; qwop++) {
medium[i][qwop] = temp[i][qwop];
if (qwop < 3)
System.
out.printf(medium[i][qwop] + " | ");
else
System.
out.println(medium[i][qwop] + "\n");
Thread.sleep(150);
}
}
}
}
if (input[3].equalsIgnoreCase("edit")) {
Thread.sleep(500);
System.
out.println("First type the number of the " + medium[1][0]
+
"\nyou want to edit");
int input4 = ScanObj.nextInt();
System.
out
.println(
"Now, type the number of the column you want to edit");
int input5 = ScanObj.nextInt();
System.
out.println("Now, make your edit:\n");
input[6] = ScanObj.nextLine();
medium[input4][input5] = input[6];
System.
out.println(medium[input4][input5]);
}
for (int y = 0; y < 10; y++) {
if (input[3].equalsIgnoreCase("search")) {
Thread.sleep(2000);
System.
out.println("Would you like to search by 1) Title or 2) Lexile #");
int input4 = 1;
input4 = ScanObj.nextInt();
Thread.sleep(2500);
System.
out.println("Now Type your search");
input[5] = ScanObj.nextLine();
Thread.sleep(2500);
for (int i = 0; i < 5; i++) {
System.
out.println(input4 + ", " + input[5]);
System.
out.println(medium[i][input4]);
if (input[5].compareToIgnoreCase(medium[i][input4]) == 0) {
y = 10;
System.
out.println("why do you lie");
for (int r = 0; r < 4; r++) {
if (r < 3)
System.
out.printf(medium[i][r] + " | ");
else
System.
out.println(medium[i][r]);
Thread.sleep(150);
}
}
else
System.
out.println(medium[i][input4]);
System.
out.println("Couldn't find item, please search again:");
}
}
}
}
}
}
Where ScanObj is my scanner. I'm just trying to get an Integer input, but it always returns null. What's wrong?
Here's an example using Scanner-
int choice;
do {
System.out.print("Would you like to search by (1) Title or (2) Lexile # : ");
choice = scan.nextInt();
} while(choice < 1 || choice > 2); // Keep asking the user for a valid integer
Above code will break, if the input is not an integer.
You can use nextLine-
Scanner scan = new Scanner(System.in);
String response;
do {
System.out.print("Would you like to search by (1) Title or (2) Lexile # : ");
response = scan.nextLine();
} while(!response.equals("1") && !response.equals("2"));
int choice = Integer.parseInt(response);
try {
System.out.print("Say cheeseeeee...");
Thread.sleep(3000);
} catch(InterruptedException e){
// handle exception
e.printStackTrace();
}
System.out.println("\nChoice : " + choice);